# trademaster-ntu/trademaster architecture

> Architecture diagram of the GitHub repository trademaster-ntu/trademaster, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/trademaster-ntu/trademaster
- Repository: https://github.com/trademaster-ntu/trademaster

## Overview

#### Purpose and Entry Points
TradeMaster is a Python quantitative-trading research platform for training, evaluating, and deploying RL agents across algorithmic trading, high-frequency trading, order execution, and portfolio management. Primary CLI entry points are task-specific training scripts such as `tools/algorithmic_trading/train.py`, `tools/portfolio_management/train.py`, and `tools/order_execution/train_eteo.py`; each consumes a composed experiment config under `configs/`. Auxiliary workflows include market-dynamics labeling (`tools/market_dynamics_labeling/run.py`), missing-data imputation (`tools/missing_value_imputation/run.py`), and FinAgent execution (`tools/finagent/main.py`).

#### Config-Driven Assembly and Registries
Experiment configs select dataset, environment, agent, network, optimizer, loss, trainer, and transition implementations. Base configuration fragments define reusable component choices in `configs/_base_/agents/`, `configs/_base_/environments/`, and `configs/_base_/trainers/`; concrete runs combine them, e.g. `configs/portfolio_management/portfolio_management_dj30_eiie_eiie_adam_mse.py`. Runtime construction is mediated by domain builders/registries such as `trademaster/agents/builder.py`, `trademaster/datasets/builder.py`, and `trademaster/trainers/builder.py`, creating an extensibility boundary for new algorithms or task variants.

#### Data Preparation and Dataset Boundary
Raw and prepared financial data are stored locally under `data/`, organized by task and split into train/validation/test CSVs, e.g. `data/algorithmic_trading/BTC/train.csv` and `data/portfolio_management/dj30/test.csv`. Preprocessing supports Yahoo Finance ingestion through `trademaster/preprocessor/yfinance_preprocessor/processor.py`, invoked by scripts such as `tools/data_preprocessor/yahoofinance/dj30.py`. Task datasets normalize these files into trainer-facing samples and state features: `trademaster/datasets/algorithmic_trading/dataset.py`, `trademaster/datasets/order_execution/dataset.py`, and `trademaster/datasets/portfolio_management/dataset.py`.

#### Trading Simulation Environments
Datasets feed task-specific, Gym-like market simulators that convert actions into positions, rewards, and next observations. The main environment boundary is `trademaster/environments/builder.py`, with implementations for algorithmic trading (`trademaster/environments/algorithmic_trading/environment.py`), high-frequency trading (`trademaster/environments/high_frequency_trading/environment.py`), and portfolio management (`trademaster/environments/portfolio_management/environment.py`). Specialized execution and portfolio methods use dedicated environments, including `trademaster/environments/order_execution/eteo_environment.py` and `trademaster/environments/portfolio_management/deeptrader_environment.py`.

#### RL Learning Stack
Agents own policy/action and replay behavior; networks provide PyTorch model architectures; trainers coordinate rollout, optimization, validation, checkpointing, and evaluation. Representative implementations are `trademaster/agents/high_frequency_trading/ddqn.py`, `trademaster/nets/dqn.py`, and `trademaster/trainers/high_frequency_trading/trainer.py`. The platform also implements task-specific agents such as ETEO and EIIE in `trademaster/agents/order_execution/eteo.py` and `trademaster/agents/portfolio_management/eiie.py`. Shared losses, optimizers, and transitions are pluggable via `trademaster/losses/`, `trademaster/optimizers/`, and `trademaster/transition/`.

#### Market-Dynamics and Data-Quality Stages
Market-dynamics labeling/simulation is a separate pre-training data transformation: labeled regime slices and metrics are persisted alongside datasets, while the core model is `trademaster/evaluation/market_dynamics_labeling/model/slice_and_merge_model.py`. Generated labels can become inputs to downstream task configurations, as seen in `configs/_base_/market_dynamics_model/`. Diffusion-based missing-value imputation is another optional upstream stage, implemented in `trademaster/imputation/missing_value_imputation/imputation.py` and launched through `tools/missing_value_imputation/run.py`.

#### Extended Runtimes, Services, and Deployment
The repository runs primarily as Python/PyTorch experiments, with Docker support in `Dockerfile`; classic RL configurations reference Ray/RLlib. A deployment client/service pair exists in `deploy/backend_client.py` and `deploy/backend_service.py`. Real-time collection infrastructure integrates Binance and asynchronous messaging through `trademaster/collector/binance.py`, `trademaster/collector/helper/celery_app.py`, and `trademaster/collector/helper/pika_connection.py`. FinAgent is a parallel LLM-assisted trading subsystem with provider abstractions, FAISS-backed memory, external market/news downloaders, and trading environment logic in `finagent/provider/provider.py`, `finagent/memory/faiss.py`, and `finagent/environment/trading.py`; configured providers include OpenAI settings in `configs/finagent/openai_config.json`.

Main entry points: [`trademaster/agents/builder.py`](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/agents/builder.py) (Builders and registries), [`trademaster/environments/builder.py`](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/environments/builder.py) (Trading simulators), [`data/algorithmic_trading/BTC/train.csv`](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/data/algorithmic_trading/BTC/train.csv) (Local market data), [`trademaster/trainers/high_frequency_trading/trainer.py`](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/trainers/high_frequency_trading/trainer.py) (RL trainers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_assembly["Experiment Assembly"]
  node_cli["Task training CLIs<br/>Python entry points<br/>[train.py]"]
  node_configs["Composed experiment configs<br/>Python configs"]
  node_base_configs["Base component configs<br/>Config fragments<br/>[deepscalper.py]"]
  node_registries{{"Builders and registries<br/>Extension boundary<br/>[builder.py]"}}
end

subgraph group_data["Data Pipeline"]
  node_raw_data[("Local market data<br/>CSV datasets<br/>[train.csv]")]
  node_yfinance["Yahoo Finance preprocessing<br/>Data ingestion<br/>[processor.py]"]
  node_imputation["Missing-value imputation<br/>Diffusion stage<br/>[imputation.py]"]
  node_market_dynamics["Market-dynamics labeling<br/>Regime labeling"]
  node_datasets["Task datasets<br/>Dataset adapters<br/>[dataset.py]"]
end

subgraph group_learning["RL Runtime"]
  node_environments["Trading simulators<br/>Gym-like environments<br/>[builder.py]"]
  node_execution_env["Specialized execution env<br/>ETEO environment"]
  node_agents["RL agents<br/>Policies and replay<br/>[ddqn.py]"]
  node_networks["PyTorch networks<br/>Model architectures<br/>[dqn.py]"]
  node_training_components["Pluggable learning components<br/>Loss, optimizer, transition<br/>[hft_loss.py]"]
  node_trainers["RL trainers<br/>Training orchestration<br/>[trainer.py]"]
end

subgraph group_operations["Extended Operations"]
  node_deployment{{"Deployment client and service<br/>Serving boundary<br/>[backend_service.py]"}}
  node_realtime_collection["Real-time market collection<br/>Binance and messaging<br/>[binance.py]"]
end

subgraph group_finagent["FinAgent"]
  node_finagent_entry["FinAgent runtime<br/>LLM trading entry point<br/>[main.py]"]
  node_provider["LLM provider layer<br/>Provider abstraction<br/>[provider.py]"]
  node_memory[("FAISS memory<br/>Vector memory<br/>[faiss.py]")]
  node_finagent_env["FinAgent trading environment<br/>Trading logic<br/>[trading.py]"]
end

node_base_configs -->|"reused by"| node_configs
node_cli -->|"loads"| node_configs
node_configs -->|"selects components through"| node_registries
node_yfinance -->|"prepares"| node_raw_data
node_imputation -.->|"repairs"| node_raw_data
node_market_dynamics -.->|"adds labels to"| node_raw_data
node_raw_data -->|"supplies"| node_datasets
node_market_dynamics -.->|"optional label inputs"| node_configs
node_registries -->|"builds"| node_datasets
node_registries -->|"builds"| node_environments
node_registries -->|"builds"| node_agents
node_registries -->|"builds"| node_trainers
node_datasets -->|"provides market state"| node_environments
node_environments -.->|"includes specialized variant"| node_execution_env
node_agents -->|"acts in"| node_environments
node_networks -->|"implements policy/value models"| node_agents
node_training_components -->|"configures optimization"| node_trainers
node_agents -->|"trained by"| node_trainers
node_environments -->|"rollouts and evaluation"| node_trainers
node_trainers -.->|"produces deployable models"| node_deployment
node_realtime_collection -.->|"updates market inputs"| node_raw_data
node_finagent_entry -->|"uses"| node_provider
node_finagent_entry -->|"uses"| node_memory
node_finagent_entry -->|"runs"| node_finagent_env
node_provider -->|"assists decisions in"| node_finagent_env

click node_cli "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/tools/algorithmic_trading/train.py"
click node_configs "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/configs/portfolio_management/portfolio_management_dj30_eiie_eiie_adam_mse.py"
click node_base_configs "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/configs/_base_/agents/algorithmic_trading/deepscalper.py"
click node_registries "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/agents/builder.py"
click node_raw_data "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/data/algorithmic_trading/BTC/train.csv"
click node_yfinance "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/preprocessor/yfinance_preprocessor/processor.py"
click node_imputation "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/imputation/missing_value_imputation/imputation.py"
click node_market_dynamics "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/evaluation/market_dynamics_labeling/model/slice_and_merge_model.py"
click node_datasets "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/datasets/algorithmic_trading/dataset.py"
click node_environments "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/environments/builder.py"
click node_execution_env "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/environments/order_execution/eteo_environment.py"
click node_agents "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/agents/high_frequency_trading/ddqn.py"
click node_networks "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/nets/dqn.py"
click node_training_components "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/losses/hft_loss.py"
click node_trainers "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/trainers/high_frequency_trading/trainer.py"
click node_deployment "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/deploy/backend_service.py"
click node_realtime_collection "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/collector/binance.py"
click node_finagent_entry "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/tools/finagent/main.py"
click node_provider "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/finagent/provider/provider.py"
click node_memory "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/finagent/memory/faiss.py"
click node_finagent_env "https://github.com/trademaster-ntu/trademaster/blob/1.0.0/finagent/environment/trading.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_configs,node_base_configs,node_registries toneBlue
class node_raw_data,node_yfinance,node_imputation,node_market_dynamics,node_datasets toneAmber
class node_environments,node_execution_env,node_agents,node_networks,node_training_components,node_trainers toneMint
class node_deployment,node_realtime_collection toneRose
class node_finagent_entry,node_provider,node_memory,node_finagent_env toneIndigo
```

## Components

### Experiment Assembly

- [Task training CLIs](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/tools/algorithmic_trading/train.py): Python entry points · Launch task-specific training runs. · `tools/algorithmic_trading/train.py`
- [Composed experiment configs](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/configs/portfolio_management/portfolio_management_dj30_eiie_eiie_adam_mse.py): Python configs · Select task components and experiment settings. · `configs/portfolio_management/portfolio_management_dj30_eiie_eiie_adam_mse.py`
- [Base component configs](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/configs/_base_/agents/algorithmic_trading/deepscalper.py): Config fragments · `configs/_base_/agents/algorithmic_trading/deepscalper.py`
- [Builders and registries](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/agents/builder.py): Extension boundary · Construct configured datasets, agents, and trainers. · `trademaster/agents/builder.py`

### Data Pipeline

- [Local market data](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/data/algorithmic_trading/BTC/train.csv): CSV datasets · Task-organized train, validation, and test data. · `data/algorithmic_trading/BTC/train.csv`
- [Yahoo Finance preprocessing](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/preprocessor/yfinance_preprocessor/processor.py): Data ingestion · `trademaster/preprocessor/yfinance_preprocessor/processor.py`
- [Missing-value imputation](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/imputation/missing_value_imputation/imputation.py): Diffusion stage · Optional upstream data-quality transformation. · `trademaster/imputation/missing_value_imputation/imputation.py`
- [Market-dynamics labeling](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/evaluation/market_dynamics_labeling/model/slice_and_merge_model.py): Regime labeling · Persists labeled slices and metrics for downstream runs. · `trademaster/evaluation/market_dynamics_labeling/model/slice_and_merge_model.py`
- [Task datasets](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/datasets/algorithmic_trading/dataset.py): Dataset adapters · Normalize files into samples and state features. · `trademaster/datasets/algorithmic_trading/dataset.py`

### RL Runtime

- [Trading simulators](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/environments/builder.py): Gym-like environments · Turn actions into positions, rewards, and observations. · `trademaster/environments/builder.py`
- [Specialized execution env](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/environments/order_execution/eteo_environment.py): ETEO environment · `trademaster/environments/order_execution/eteo_environment.py`
- [RL agents](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/agents/high_frequency_trading/ddqn.py): Policies and replay · Task agents choose actions and manage learning state. · `trademaster/agents/high_frequency_trading/ddqn.py`
- [PyTorch networks](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/nets/dqn.py): Model architectures · `trademaster/nets/dqn.py`
- [Pluggable learning components](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/losses/hft_loss.py): Loss, optimizer, transition · `trademaster/losses/hft_loss.py`
- [RL trainers](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/trainers/high_frequency_trading/trainer.py): Training orchestration · Coordinate rollout, optimization, validation, checkpoints, and evaluation. · `trademaster/trainers/high_frequency_trading/trainer.py`

### Extended Operations

- [Deployment client and service](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/deploy/backend_service.py): Serving boundary · `deploy/backend_service.py`
- [Real-time market collection](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/trademaster/collector/binance.py): Binance and messaging · Collects market data through asynchronous infrastructure. · `trademaster/collector/binance.py`

### FinAgent

- [FinAgent runtime](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/tools/finagent/main.py): LLM trading entry point · `tools/finagent/main.py`
- [LLM provider layer](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/finagent/provider/provider.py): Provider abstraction · `finagent/provider/provider.py`
- [FAISS memory](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/finagent/memory/faiss.py): Vector memory · `finagent/memory/faiss.py`
- [FinAgent trading environment](https://github.com/trademaster-ntu/trademaster/blob/1.0.0/finagent/environment/trading.py): Trading logic · `finagent/environment/trading.py`

## Connections

- Base component configs → Composed experiment configs: reused by
- Task training CLIs → Composed experiment configs: loads
- Composed experiment configs → Builders and registries: selects components through
- Yahoo Finance preprocessing → Local market data: prepares
- Missing-value imputation → Local market data: repairs
- Market-dynamics labeling → Local market data: adds labels to
- Local market data → Task datasets: supplies
- Market-dynamics labeling → Composed experiment configs: optional label inputs
- Builders and registries → Task datasets: builds
- Builders and registries → Trading simulators: builds
- Builders and registries → RL agents: builds
- Builders and registries → RL trainers: builds
- Task datasets → Trading simulators: provides market state
- Trading simulators → Specialized execution env: includes specialized variant
- RL agents → Trading simulators: acts in
- PyTorch networks → RL agents: implements policy/value models
- Pluggable learning components → RL trainers: configures optimization
- RL agents → RL trainers: trained by
- Trading simulators → RL trainers: rollouts and evaluation
- RL trainers → Deployment client and service: produces deployable models
- Real-time market collection → Local market data: updates market inputs
- FinAgent runtime → LLM provider layer: uses
- FinAgent runtime → FAISS memory: uses
- FinAgent runtime → FinAgent trading environment: runs
- LLM provider layer → FinAgent trading environment: assists decisions in
