# ibaihuo/chanvis architecture

> Architecture diagram of the GitHub repository ibaihuo/chanvis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ibaihuo/chanvis
- Repository: https://github.com/ibaihuo/chanvis

## Overview

##### Project Type and Purpose
- This is a full-stack, data-visualization application tailored to geometric (缠论) trading analysis.
- The front end is a Vue.js SPA that embeds TradingView’s local SDK for interactive K-line charts and custom drawing tools.
- The back end is a Flask-based Python API server exposing endpoints to load raw K-line data and structural analysis results (natural chan theory, “NLChan”).
- MongoDB serves as the time-series and analysis-result datastore.
- ETL scripts in the `hetl/` folder ingest raw exchange or JQ data into MongoDB.

##### Main Components
a. Frontend (ui/)
- Vue.js + TypeScript app (src/)
- TradingView charting library & datafeed JS (public/)
- Custom Vue components (ChanContainer.vue, ChanApp.vue) for drawing tools, indicators, and playback controls
b. Backend API (api/)
- Flask application (`chanapi.py`)
- Symbol metadata loader (`symbol_info.py`)
- Configuration (`comm/conf.py`) for database URIs, API settings
c. Data Store
- MongoDB instance holding:
- Raw K-line collections (`nlchan`, `stock`)
- Replay and config collections (`config`)
d. ETL Layer (hetl/)
- Mongo restore scripts (`hmgo/restore_chanvis_mongo.sh`)
- Data-fetch and conversion scripts (`selcoin/`, `stock/get_jqdata.py`)
e. Utilities (utils/)
- Date helpers (`dtlib.py`)
- Natural-Chan analysis helpers (`nlchan.py`)

##### Interactions and Data Flows
1. ETL scripts pull data from external sources (Binance, JQData), transform it into BSON/JSON, and load to MongoDB.
2. The Flask API reads from MongoDB, applies any NLP/NLChan algorithms via `utils/nlchan.py`, and returns time-series or analysis results as JSON.
3. The Vue client initializes TradingView, fetches historical bars or indicator data via HTTP calls to Flask endpoints, and renders them.
4. User interactions (drawing, custom indicators) trigger additional API calls or local state updates in the UI.

##### Architectural Patterns and Principles
- Clear separation of concerns:
- ETL layer —> Data ingestion
- API layer —> Data access + business logic
- UI layer —> Presentation + interaction
- Modular, folder-based structure maps directly to system components.
- Stateless REST API ensures horizontal scalability of the back end.
- Use of external SDK (TradingView) makes the front end lightweight and focused on integration.

##### Technologies and Frameworks
- Frontend: Vue.js, TypeScript, TradingView Local Charting Library
- Backend: Python, Flask, PyMongo
- Database: MongoDB
- ETL: Python scripts, shell scripts, BSON dumps
- Utilities: Custom Python modules

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Data Sources
    Binance["Binance (External)"]:::external
    JQData["JQData (External)"]:::external

    %% ETL Layer
    subgraph "ETL Services" 
        direction TB
        Restore["restore_chanvis_mongo.sh"]:::etl
        Selcoin["selcoin/"]:::etl
        JQDataScript["get_jqdata.py"]:::etl
    end

    %% Data Store
    MongoDB["MongoDB"]:::datastore

    %% Backend API
    subgraph "Flask API Server"
        direction TB
        ChanAPI["chanapi.py"]:::service
        SymbolInfo["symbol_info.py"]:::service
        Conf["conf.py"]:::service
        Dtlib["dtlib.py"]:::service
        NLChanUtil["nlchan.py"]:::service
    end

    %% Frontend UI
    subgraph "Vue.js Client"
        direction TB
        ChanApp["ChanApp.vue"]:::client
        ChanContainer["ChanContainer.vue"]:::client
        MainJS["main.js"]:::client
        MainCSS["main.css"]:::client

        subgraph "TradingView SDK"
            direction TB
            ChartLib["put-charting-library-here"]:::client
            DataFeeds["put-datafeeds-here"]:::client
            IndexHTML["index.html"]:::client
        end
    end

    %% Flows
    Binance -->|"raw data"| Restore
    JQData -->|"raw data"| JQDataScript
    Restore -->|load BSON| MongoDB
    JQDataScript -->|load JSON| MongoDB
    Selcoin -->|transform & load| MongoDB

    MongoDB <-->|"reads/writes"| ChanAPI
    ChanAPI -->|"calls symbol loader"| SymbolInfo
    ChanAPI -->|"uses config"| Conf
    ChanAPI -->|"applies analysis"| NLChanUtil
    ChanAPI -->|"uses date helpers"| Dtlib

    ChanApp -->|"/history"| ChanAPI
    ChanApp -->|"/symbols"| ChanAPI
    ChanApp -->|"/indicator"| ChanAPI
    ChanContainer -->|interaction events| ChanApp

    ChanApp -->|"loads JS libs"| ChartLib
    ChanApp -->|"loads datafeeds"| DataFeeds
    ChanApp -->|"loads page"| IndexHTML

    %% Click Events
    click Restore "https://github.com/ibaihuo/chanvis/blob/main/hetl/hmgo/restore_chanvis_mongo.sh"
    click Selcoin "https://github.com/ibaihuo/chanvis/tree/main/hetl/selcoin/"
    click JQDataScript "https://github.com/ibaihuo/chanvis/blob/main/hetl/stock/get_jqdata.py"

    click ChanAPI "https://github.com/ibaihuo/chanvis/blob/main/api/chanapi.py"
    click SymbolInfo "https://github.com/ibaihuo/chanvis/blob/main/api/symbol_info.py"
    click Conf "https://github.com/ibaihuo/chanvis/blob/main/comm/conf.py"

    click ChanApp "https://github.com/ibaihuo/chanvis/blob/main/ui/src/ChanApp.vue"
    click ChanContainer "https://github.com/ibaihuo/chanvis/blob/main/ui/src/components/ChanContainer.vue"
    click MainJS "https://github.com/ibaihuo/chanvis/blob/main/ui/src/main.js"
    click MainCSS "https://github.com/ibaihuo/chanvis/blob/main/ui/src/main.css"
    click ChartLib "https://github.com/ibaihuo/chanvis/tree/main/ui/public/put-charting-library-here"
    click DataFeeds "https://github.com/ibaihuo/chanvis/tree/main/ui/public/put-datafeeds-here"
    click IndexHTML "https://github.com/ibaihuo/chanvis/blob/main/ui/public/index.html"

    click Dtlib "https://github.com/ibaihuo/chanvis/blob/main/utils/dtlib.py"
    click NLChanUtil "https://github.com/ibaihuo/chanvis/blob/main/utils/nlchan.py"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px
    classDef etl fill:#ddd,stroke:#666,stroke-width:1px
    classDef datastore fill:#ccf,stroke:#00f,stroke-width:2px
    classDef service fill:#cfc,stroke:#080,stroke-width:2px
    classDef client fill:#fcf,stroke:#f08,stroke-width:2px
```
