How ai4finance-foundation/finrl-trading works
Project Type and Purpose
• This is a modular data‐driven trading pipeline (not a web app). It ingests market+fundamental data, preprocesses/feature‐engineers it, then runs two “decision” engines – a Supervised Learning (SL) stock‐selection engine and a Deep Reinforcement Learning (DRL) portfolio‐allocation engine – followed by backtesting and (optionally) live paper‐trading deployment.
File Structure Analysis
Top-level:
• .devcontainer/ – Docker‐based dev environment
• README.md – high‐level project overview and phases
• requirements.txt – Python dependencies
• china_stock_market_run.py – deployment script for China markets
• fundamental_run_model.py, fundamental_portfolio_drl.py, ml_model.py, rl_model.py – core modules
• fundamental_*.ipynb, stock_selection.ipynb, backtesting notebooks – research & prototyping
• data_processor/ – data‐ingestion & feature‐engineering notebooks, CSV, outputs
• old_repo_ensemble_strategy/ – packaged DRL ensemble strategy code, configs, raw/trained data
Readme Insights
• Phases I–III map directly to modular components:
Phase I: Data ingestion + feature engineering
Phase IIa: SL‐based stock selection + backtesting
Phase IIb: DRL ensemble portfolio allocation + backtesting
Phase III: Deployment to online trading (e.g., Alpaca or Chinese broker API)
Main System Components
A. External Data Providers
• Market Data APIs or CSV dumps (Yahoo Finance, AlphaVantage, local CSV)
• Fundamental Data (S&P 500 constituents, financial ratios)
B. Data Processor Module (data_processor/)
• Notebooks & scripts: Step1_get_sp500_ticker, Step2_preprocess_fundamental_data
• Outputs: final_ratios.csv, sector*.xlsx
C. Stock Selection Engine (Supervised Learning)
• ml_model.py / stock_selection.ipynb
• Classic algorithms: LSTM, RF, SVM, Linear/Lasso/Ridge
• fundamental_run_model.py, fundamental_portfolio.ipynb
D. Portfolio Allocation Engine (Deep RL)
• old_repo_ensemble_strategy/ package + rl_model.py / fundamental_portfolio_drl.py
• Algorithms: PPO, DDPG, A2C, SAC, TD3
• backtesting.ipynb, run_DRL.py, trained_models/
E. Backtesting & Evaluation
• fundamental_back_testing.ipynb, old_repo_ensemble_strategy/backtesting.ipynb
• Metrics calculation (returns, Sharpe, drawdown)
F. Deployment Module
• china_stock_market_run.py (China markets)
• (Optional) Alpaca paper trading notebook/script
Relationships & Data Flow
• Data Providers → Data Processor → “Processed Data”
• Processed Data → SL Engine → Selected Stock List
• Selected Stock List + Processed Data → DRL Engine → Position Signals
• Signals → Backtesting Module → Performance Reports
• (Optional) Position Signals → Deployment Module → Live/Paper Trading API
Architectural Patterns & Principles
• Pipeline Architecture: clearly‐defined stages (staging, modeling, evaluation, deployment)
• Modular design: each engine in its own directory or script for separation of concerns
• Config‐driven: old_repo_ensemble_strategy/config/config.py for hyperparameters
• Reusability: data_processor outputs feed both SL and DRL
Technologies & Frameworks
• Python, Jupyter Notebooks
• Pandas, NumPy for data processing
• Scikit‐Learn for SL algorithms
• FinRL / Stable Baselines for DRL algorithms
• Docker (.devcontainer) for consistent environment
• Alpaca (or alternative broker API) for deployment
• Clear labels (e.g., “SL Stock Selector”)
• Versioned config file indicator (config.py)
• Docker/devcontainer boundary to show environment encapsulation