How trademaster-ntu/trademaster works
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: agents/builder.py Builders and registries · environments/builder.py Trading simulators · train.csv Local market data · trainer.py RL trainers
Components 21
Experiment Assembly
- Task training CLIs · Launch task-specific training runs.
tools/algorithmic_trading/train.py - Composed experiment configs · Select task components and experiment settings.
configs/portfolio_management/portfolio_management_dj30_eiie_eiie_adam_mse.py - Base component configs
configs/_base_/agents/algorithmic_trading/deepscalper.py - Builders and registries · Construct configured datasets, agents, and trainers.
trademaster/agents/builder.py
Data Pipeline
- Local market data · Task-organized train, validation, and test data.
data/algorithmic_trading/BTC/train.csv - Yahoo Finance preprocessing
trademaster/preprocessor/yfinance_preprocessor/processor.py - Missing-value imputation · Optional upstream data-quality transformation.
trademaster/imputation/missing_value_imputation/imputation.py - Market-dynamics labeling · Persists labeled slices and metrics for downstream runs.
trademaster/evaluation/market_dynamics_labeling/model/slice_and_merge_model.py - Task datasets · Normalize files into samples and state features.
trademaster/datasets/algorithmic_trading/dataset.py
RL Runtime
- Trading simulators · Turn actions into positions, rewards, and observations.
trademaster/environments/builder.py - Specialized execution env
trademaster/environments/order_execution/eteo_environment.py - RL agents · Task agents choose actions and manage learning state.
trademaster/agents/high_frequency_trading/ddqn.py - PyTorch networks
trademaster/nets/dqn.py - Pluggable learning components
trademaster/losses/hft_loss.py - RL trainers · Coordinate rollout, optimization, validation, checkpoints, and evaluation.
trademaster/trainers/high_frequency_trading/trainer.py
Extended Operations
- Deployment client and service
deploy/backend_service.py - Real-time market collection · Collects market data through asynchronous infrastructure.
trademaster/collector/binance.py
FinAgent
- FinAgent runtime
tools/finagent/main.py - LLM provider layer
finagent/provider/provider.py - FAISS memory
finagent/memory/faiss.py - FinAgent trading environment
finagent/environment/trading.py
Connections 25
- 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