# myhhub/stock architecture

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

- Interactive diagram: https://gitdiagram.com/myhhub/stock
- Repository: https://github.com/myhhub/stock

## Overview

##### Main Components
- Data Acquisition & Crawling: Represent the set of “crawling” modules under instock/core/crawling (which fetch stock details, ETF data, historical trade dates, etc.) to show how the system ingests raw market data.
- Data Processing & Analysis: Include components for indicator calculations (in instock/core/indicator), K-line and pattern recognition (instock/core/kline and instock/core/pattern), and backtest modules (instock/core/backtest). Also illustrate the “strategy” submodule that encapsulates various stock selection logics.
- Web Presentation: Represent the front-end web module (instock/web) that includes static resources, templates, and the web service handler. This component exposes dashboards and data views to the user.
- Database & Storage: Include a database/data persistence layer that is accessed via modules in instock/lib/database and configured under instock/config. This component stores historical data, indicators, and logs.
- External Dependencies & Deployment:
- Docker and Containerization: Outline the “docker” folder and related Dockerfiles plus GitHub workflows, indicating that the system is deployed in Docker containers (with links for development and deployment).
- TA-Lib and Third-Party Libraries: Represent external libraries such as TA-Lib, pandas, and the various Python dependencies.
- Broker/Trading Client Integration: Show the integration point for the trading broker (configured with trade_client.json and corresponding trade services).
- Logging & Supervisor: Mention that there are separate log files for data jobs (stock_execute_job.log), web services (stock_web.log), and trade services (stock_trade.log). Also, supervisor configurations are present to manage daemon processes.

##### Relationships and Interactions
- That the Job Scheduler (cron jobs and job scripts) triggers the Data Acquisition and Data Processing components periodically.
- That the Web Presentation layer retrieves data from the Database for visualization.
- Integration lines from the entire system to the external Docker/Deployment environment, emphasizing that the system is containerized and uses CI/CD workflows (.github/workflows).
- Optionally, include a feedback loop from logging and monitoring components back to the system infrastructure for error handling and debugging.

##### Architectural Patterns and Design Principles
- Separation of concerns: Different folders and modules clearly separate data acquisition, processing, trading, and user interface.
- Modular design: Each functionality (e.g., crawling, indicators, strategies) is encapsulated in its own submodule – an important pattern making the system scalable.
- Batch processing: The job scheduling and cron script implementation are a key pattern for periodic processing.
- Multi-threading and singleton design: Note in the README that the system employs multi-thread and singleton patterns for efficiency; these can be noted near the processing modules.
- Containerization and deployment automation using Docker and GitHub workflows.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Backend Processing & Data Services"
        DC["Data Crawler"]:::core
        subgraph "Data Processing"
            IE["Indicator Engine"]:::core
            KL["K-Line Engine"]:::core
            PR["Pattern Recognition"]:::core
            BE["Backtesting Engine"]:::core
            SM["Strategy Module"]:::core
        end
        subgraph "Trading and Execution"
            TR["Trade Robot"]:::trade
            TS["Trade Strategies"]:::trade
            TSrv["Trade Service"]:::trade
        end
    end

    subgraph "Scheduling & Automation"
        JS["Job Scheduler"]:::job
        subgraph "Cron Scripts"
            HC["Hourly Cron"]:::job
            MC["Monthly Cron"]:::job
            WC["Workday Cron"]:::job
        end
    end

    subgraph "Database & Storage"
        DB["Database Engine"]:::db
        CN["Configuration"]:::db
    end

    subgraph "Web Presentation"
        WI["Web UI"]:::web
    end

    subgraph "External Dependencies & Deployment"
        DCK["Dockerfile"]:::external
        BS["Build Script"]:::external
        CI["CI/CD Workflows"]:::external
        SUP["Supervisor Config"]:::external
    end

    %% Relationships and Data Flow
    JS -->|"triggers"| DC
    HC -->|"invokes"| JS
    MC -->|"invokes"| JS
    WC -->|"invokes"| JS

    DC -->|"raw_data"| IE
    DC -->|"raw_data"| KL
    DC -->|"raw_data"| PR
    DC -->|"raw_data"| BE
    DC -->|"raw_data"| SM

    IE -->|"computed"| DB
    KL -->|"computed"| DB
    PR -->|"computed"| DB
    BE -->|"computed"| DB

    SM -->|"signals"| TR
    SM -->|"signals"| TS
    SM -->|"signals"| TSrv

    DB -->|"fetch"| WI
    CN --> WI

    %% External Integration (dashed lines indicate deployment/integration links)
    DCK --- DC
    SUP --- DC
    CI --- JS
    BS --- WI

    %% Click Events
    click DC "https://github.com/myhhub/stock/tree/master/instock/core/crawling"
    click IE "https://github.com/myhhub/stock/tree/master/instock/core/indicator"
    click KL "https://github.com/myhhub/stock/tree/master/instock/core/kline"
    click PR "https://github.com/myhhub/stock/tree/master/instock/core/pattern"
    click BE "https://github.com/myhhub/stock/tree/master/instock/core/backtest"
    click SM "https://github.com/myhhub/stock/tree/master/instock/core/strategy"
    click JS "https://github.com/myhhub/stock/tree/master/instock/job"
    click HC "https://github.com/myhhub/stock/blob/master/cron/cron.hourly"
    click MC "https://github.com/myhhub/stock/blob/master/cron/cron.monthly"
    click WC "https://github.com/myhhub/stock/blob/master/cron/cron.workdayly"
    click TR "https://github.com/myhhub/stock/tree/master/instock/trade/robot"
    click TS "https://github.com/myhhub/stock/tree/master/instock/trade/strategies"
    click TSrv "https://github.com/myhhub/stock/blob/master/instock/trade/trade_service.py"
    click WI "https://github.com/myhhub/stock/tree/master/instock/web"
    click DB "https://github.com/myhhub/stock/blob/master/instock/lib/database.py"
    click CN "https://github.com/myhhub/stock/tree/master/instock/config"
    click DCK "https://github.com/myhhub/stock/tree/master/docker/Dockerfile"
    click BS "https://github.com/myhhub/stock/blob/master/docker/build.sh"
    click CI "https://github.com/myhhub/stock/tree/master/.github/workflows"
    click SUP "https://github.com/myhhub/stock/blob/master/supervisor/supervisord.conf"

    %% Style Definitions
    classDef core fill:#AED581,stroke:#333,stroke-width:2px;
    classDef trade fill:#FF8A65,stroke:#333,stroke-width:2px;
    classDef job fill:#4DD0E1,stroke:#333,stroke-width:2px;
    classDef db fill:#FFD54F,stroke:#333,stroke-width:2px;
    classDef web fill:#CE93D8,stroke:#333,stroke-width:2px;
    classDef external fill:#90A4AE,stroke:#333,stroke-width:2px;
```
