# dulyhao/alphaforge architecture

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

- Interactive diagram: https://gitdiagram.com/dulyhao/alphaforge
- Repository: https://github.com/dulyhao/alphaforge

## Overview

##### Main Components
a. Data Collection & Preparation:
- Data Collection: Represent the "data_collection" directory. Note that users execute scripts like fetch_baostock_data.py and qlib_dump_bin.py to fetch and prepare raw stock data.
- Data Storage & Qlib Integration: Indicate the integration with Qlib (external dependency) and point out that paths in gan/utils/data.py need to point to the local Qlib data.
b. Alpha Factor Mining and Generation:
- Alphagen Module: Include the alphagen directory, which contains:
- Configurations (config.py),
- Data processing (calculator.py, expression.py, tokens.py, tree.py),
- Model components for alpha pool and general factor modeling (models/alpha_pool.py, models/model.py).
- Generic and Qlib Specific Modules: Outline alphagen_generic and alphagen_qlib directories to show how they extend or modify factor mining functionality.
c. Reinforcement Learning (RL) and Policy:
- RL Environment: Show the alphagen/rl directory with its “env” (core.py, wrapper.py) and policy (policy.py) files, indicating reinforcement learning environment and policy selection used during training.
d. Deep Search Optimization (DSO):
- DSO Module: Represent the “dso” folder that contains various submodules for configuration, core logic (core.py, execute.py), policy and policy optimizers (policy, policy_optimizer), regression tasks (regression), and training utilities. These files indicate another method for alpha factor generation or evaluation using different approaches.
e. Generative Adversarial Networks (GAN):
f. Genetic Programming (gplearn):
- GP Module: Illustrate the gplearn directory containing the core program, genetic operations (fitness.py, functions.py, genetic.py) and its test suite.
g. Training and Experiment Scripts:
- High-level Scripts: At the top-level, include scripts such as train_AFF.py, train_DSO.py, train_GP.py, train_RL.py that orchestrate training for different models.
##### Relationships and Data Flows
- That the training scripts (train_AFF.py, train_DSO.py, etc.) use the data from the prepared sources, and their outputs (mined alpha factors) are later combined or evaluated (through combine_AFF.py and experiment notebooks).
- Interdependency between the generic modules (alphagen_generic) and the core modules (alphagen, alphagen_qlib): these extend functionality or offer additional operators and features.
- For the RL components, show that the RL environment interacts with the policy modules to adjust the factor generation based on dynamic market data.
##### Architecturally Important Patterns and Principles
- Multi-Stage Pipeline: Emphasize the three-stage process (mining, combining, evaluation) with clear separation between each stage.
- Modularity: Each directory (alphagen, dso, gan, gplearn) represents a distinct experimental setup or modeling approach, following modular design principles.
- Extensibility: The presence of similar patterns (e.g., policy optimizers in DSO, testing suites in gplearn) indicates that the project is designed to allow plugging in alternative models.
##### Technologies, Frameworks, and External Dependencies
- External tools and libraries: Qlib for data management; baostock as a data source; Python libraries such as PyTorch (noted in pytorch_utils.py) and possibly scikit-learn (used in regression via sklearn.py).
- Cython components (e.g., dso/cyfunc.pyx) that offer performance optimizations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Stage 1: Factor Mining
    subgraph "Stage 1: Factor Mining"
        DC["Data Collection"]:::data
        QI["Qlib Integration"]:::data

        subgraph "Alpha Factor Mining"
            AG["Alphagen Module"]:::module
            AC["Alphagen Config"]:::module
            ADP["Alphagen Data Processing"]:::module
            AM["AlphaGen Models"]:::module
            AGG["Alphagen Generic Module"]:::module
            AQ["Alphagen Qlib Module"]:::module
            RL["RL Environment"]:::module
        end

        DSO["DSO Module"]:::module
        GAN["GAN Module"]:::module
        GP["GP Module"]:::module
    end

    %% Stage 2: Training & Integration
    subgraph "Stage 2: Training & Integration"
        TS["Training Scripts"]:::controller
        CA["Combine AFF"]:::controller
    end

    %% Stage 3: Experiment Evaluation
    subgraph "Stage 3: Experiment Evaluation"
        EN["Experiment Notebooks"]:::output
    end

    %% Data Flow from Data Collection to Factor Mining Modules
    DC -->|"raw-data"| QI
    QI -->|"feeds data"| AG
    QI -->|"feeds data"| AGG
    QI -->|"feeds data"| AQ
    QI -->|"feeds data"| DSO
    QI -->|"feeds data"| GAN
    QI -->|"feeds data"| GP

    %% Internal Alphagen Flow
    AG --> AC
    AG --> ADP
    AG --> AM
    AG --> RL

    %% Factor Mining to Training
    AG --> TS
    AGG --> TS
    AQ --> TS
    RL --> TS
    DSO --> TS
    GAN --> TS
    GP --> TS

    %% Training to Combine and then to Experiments
    TS --> CA
    CA --> EN

    %% Click Events
    click DC "https://github.com/dulyhao/alphaforge/tree/master/data_collection"
    click QI "https://github.com/dulyhao/alphaforge/blob/master/gan/utils/data.py"
    click AG "https://github.com/dulyhao/alphaforge/tree/master/alphagen"
    click AC "https://github.com/dulyhao/alphaforge/blob/master/alphagen/config.py"
    click ADP "https://github.com/dulyhao/alphaforge/tree/master/alphagen/data"
    click AM "https://github.com/dulyhao/alphaforge/tree/master/alphagen/models"
    click AGG "https://github.com/dulyhao/alphaforge/tree/master/alphagen_generic"
    click AQ "https://github.com/dulyhao/alphaforge/tree/master/alphagen_qlib"
    click RL "https://github.com/dulyhao/alphaforge/tree/master/alphagen/rl"
    click DSO "https://github.com/dulyhao/alphaforge/tree/master/dso"
    click GAN "https://github.com/dulyhao/alphaforge/tree/master/gan"
    click GP "https://github.com/dulyhao/alphaforge/tree/master/gplearn"
    click TS "https://github.com/dulyhao/alphaforge/blob/master/train_AFF.py"
    click CA "https://github.com/dulyhao/alphaforge/blob/master/combine_AFF.py"
    click EN "https://github.com/dulyhao/alphaforge/blob/master/exp_AFF_calc_result.ipynb"

    %% Styles
    classDef data fill:#cce5ff,stroke:#007bff,stroke-width:2px;
    classDef module fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef controller fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef output fill:#f8d7da,stroke:#721c24,stroke-width:2px;
```
