Skip to content

Latest commit

ย 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

FLX: Financial Reinforcement Learning

Deep Reinforcement Learning Framework for Automated Quantitative Trading

A production-ready DRL framework for financial trading โ€” covering data acquisition, model training, backtesting, and live paper trading across stocks, crypto, and portfolios.

Python 3.7+ PyPI License Status


๐Ÿ“Œ Table of Contents


๐Ÿง  About the Project

FLX is a deep reinforcement learning framework for automated quantitative trading. It provides end-to-end support for the full trading pipeline โ€” from market data acquisition and feature engineering to model training, backtesting, and live paper trading.

The framework is designed around three core layers:

  • ๐Ÿ›๏ธ Market Environments โ€” Gym-style environments for stocks, crypto, and portfolios
  • ๐Ÿค– DRL Agents โ€” Plug-and-play support for ElegantRL, RLlib, and Stable Baselines 3
  • ๐Ÿ“Š Financial Applications โ€” Ready-to-use trading strategies and backtesting pipelines

Whether you're a researcher prototyping new trading strategies or a developer building production trading systems โ€” FLX provides the foundation.


โœจ Key Features

Feature Description
๐Ÿงช Multi-Agent Training Train 5 DRL agents simultaneously: A2C, DDPG, PPO, TD3, SAC
๐Ÿ“ˆ Backtesting Engine Compare agent performance against MVO and market index baselines
๐Ÿ“ก 14+ Data Sources Yahoo Finance, Alpaca, Binance, WRDS, CCXT, and more
๐Ÿ”ง Technical Indicators Built-in MACD, RSI, Bollinger Bands, CCI, SMA via stockstats
๐Ÿ’น Multi-Asset Support Stocks, cryptocurrencies, forex, and portfolio allocation
๐Ÿ‹๏ธ Gym-Style Environments OpenAI Gymnasium-compatible market environments
๐Ÿ“ฑ Paper Trading Live paper trading integration with Alpaca API
๐Ÿงฎ Ensemble Strategies Combine multiple agents for robust trading decisions

๐Ÿ—๏ธ Architecture

The framework follows a train โ†’ test โ†’ trade pipeline:

Data Acquisition โ†’ Feature Engineering โ†’ Environment Setup โ†’ Agent Training โ†’ Backtesting โ†’ Paper Trading

Three-Layer Design:

  1. Application Layer โ€” Stock trading, portfolio allocation, crypto trading, HFT
  2. Agent Layer โ€” ElegantRL, RLlib, Stable Baselines 3 algorithms
  3. Environment Layer โ€” Market data processors, gym environments, preprocessors

๐Ÿ› ๏ธ Tech Stack

Technology Purpose
Python 3.7+ Core language
Stable Baselines 3 Primary DRL algorithm library
ElegantRL Lightweight DRL algorithms
Ray/RLlib Scalable distributed RL
Gymnasium Environment interface
yfinance Market data (Yahoo Finance)
Alpaca API Paper/live trading
stockstats Technical indicators
Matplotlib / Recharts Visualization and plotting
pandas / numpy Data manipulation

๐Ÿ“ Project Structure

Finance_pro/
โ”œโ”€โ”€ flx/                          # Core library
โ”‚   โ”œโ”€โ”€ applications/              # Trading task implementations
โ”‚   โ”‚   โ”œโ”€โ”€ cryptocurrency_trading/
โ”‚   โ”‚   โ”œโ”€โ”€ high_frequency_trading/
โ”‚   โ”‚   โ”œโ”€โ”€ portfolio_allocation/
โ”‚   โ”‚   โ””โ”€โ”€ stock_trading/
โ”‚   โ”œโ”€โ”€ agents/                    # DRL algorithm integrations
โ”‚   โ”‚   โ”œโ”€โ”€ elegantrl/
โ”‚   โ”‚   โ”œโ”€โ”€ rllib/
โ”‚   โ”‚   โ””โ”€โ”€ stablebaseline3/
โ”‚   โ”œโ”€โ”€ meta/                      # Market environments & data
โ”‚   โ”‚   โ”œโ”€โ”€ data_processors/
โ”‚   โ”‚   โ”œโ”€โ”€ env_cryptocurrency_trading/
โ”‚   โ”‚   โ”œโ”€โ”€ env_portfolio_allocation/
โ”‚   โ”‚   โ”œโ”€โ”€ env_stock_trading/
โ”‚   โ”‚   โ”œโ”€โ”€ preprocessor/
โ”‚   โ”‚   โ””โ”€โ”€ data_processor.py
โ”‚   โ”œโ”€โ”€ config.py                  # Global configuration
โ”‚   โ”œโ”€โ”€ config_tickers.py          # Ticker lists (DOW 30, NASDAQ, etc.)
โ”‚   โ”œโ”€โ”€ main.py                    # CLI entry point
โ”‚   โ”œโ”€โ”€ train.py                   # Training pipeline
โ”‚   โ”œโ”€โ”€ test.py                    # Testing pipeline
โ”‚   โ”œโ”€โ”€ trade.py                   # Live trading pipeline
โ”‚   โ””โ”€โ”€ plot.py                    # Visualization utilities
โ”‚
โ”œโ”€โ”€ examples/                      # Tutorial scripts
โ”‚   โ”œโ”€โ”€ FLX_StockTrading_2026_1_data.py
โ”‚   โ”œโ”€โ”€ FLX_StockTrading_2026_2_train.py
โ”‚   โ””โ”€โ”€ FLX_StockTrading_2026_3_Backtest.py
โ”‚
โ”œโ”€โ”€ unit_tests/                    # Test suite
โ”œโ”€โ”€ docs/                          # Documentation (Sphinx)
โ”œโ”€โ”€ docker/                        # Docker configuration
โ”œโ”€โ”€ figs/                          # Diagrams and figures
โ”œโ”€โ”€ setup.py                       # Package setup
โ”œโ”€โ”€ pyproject.toml                 # Poetry configuration
โ”œโ”€โ”€ requirements.txt               # Dependencies
โ””โ”€โ”€ README.md

๐Ÿ“ก Supported Data Sources

Data Source Type Range & Frequency Raw Data Preprocessed Data
YahooFinance US Securities Frequency-specific, 1min OHLCV Prices & Indicators
Alpaca US Stocks, ETFs 2015-now, 1min OHLCV Prices & Indicators
Binance Cryptocurrency API-specific, 1s OHLCV Prices & Indicators
CCXT Cryptocurrency API-specific, 1min OHLCV Prices & Indicators
WRDS US Securities 2003-now, 1ms Intraday Trades Prices & Indicators
Akshare CN Securities 2015-now, 1day OHLCV Prices & Indicators
Baostock CN Securities 1990-now, 5min OHLCV Prices & Indicators
IEXCloud NMS US Securities 1970-now, 1day OHLCV Prices & Indicators
JoinQuant CN Securities 2005-now, 1min OHLCV Prices & Indicators
QuantConnect US Securities 1998-now, 1s OHLCV Prices & Indicators
Tushare CN Securities -now, 1min OHLCV Prices & Indicators
Sinopac Taiwan Securities 2023-now, 1min OHLCV Prices & Indicators

OHLCV: Open, High, Low, Close prices + Volume

Technical Indicators: MACD, Bollinger Bands, RSI, CCI, DX, SMA (30/60 day)


๐Ÿš€ Installation

Prerequisites

  • Python 3.7 or higher
  • pip or Poetry

Option 1: Install from Source

git clone <your-repo-url>
cd Finance_pro
python -m venv venv
source venv/bin/activate    # Linux/Mac
# venv\Scripts\activate     # Windows
pip install -e .

Option 2: Install via pip

pip install flx

Option 3: Docker

cd docker
bash bin/build_container.sh

๐Ÿ“– Quick Start Tutorial

Step 1: Set Up the Environment

cd Finance_pro
python -m venv venv
venv\Scripts\activate        # Windows
pip install -e .

Step 2: Download & Preprocess Data

python examples/FLX_StockTrading_2026_1_data.py

This downloads DOW 30 stock data from Yahoo Finance, adds technical indicators (MACD, RSI, etc.), VIX, and turbulence index, then splits into training (2014โ€“2025) and trading (2026) sets.

Step 3: Train DRL Agents

python examples/FLX_StockTrading_2026_2_train.py

Trains 5 DRL agents (A2C, DDPG, PPO, TD3, SAC) using Stable Baselines 3. Models are saved to trained_models/.

Key Hyperparameters:

Parameter Description Default
total_timesteps Total environment interactions for training 20,000
learning_rate Weight update step size 0.001
batch_size Samples per gradient update 100
buffer_size Replay buffer capacity (off-policy) 1,000,000

Step 4: Backtest

python examples/FLX_StockTrading_2026_3_Backtest.py

Runs trained agents on trading data and compares against MVO and DJIA baselines. Results are printed to console and saved as backtest_result.png.


๐Ÿ“ก API Reference

Configuration (flx/config.py)

Variable Default Description
TRAIN_START_DATE 2014-01-06 Training period start
TRAIN_END_DATE 2025-12-31 Training period end
TRADE_START_DATE 2026-01-01 Trading/test period start
TRADE_END_DATE 2026-03-20 Trading/test period end
INDICATORS MACD, RSI, Bollinger, etc. Technical indicators to compute

DRL Agent Parameters

Agent Key Parameters
A2C n_steps=5, ent_coef=0.01, lr=0.0007
PPO n_steps=2048, batch_size=64, lr=0.00025
DDPG batch_size=128, buffer_size=50k, lr=0.001
TD3 batch_size=100, buffer_size=1M, lr=0.001
SAC batch_size=64, buffer_size=100k, lr=0.0001

๐Ÿ“ Notes & Recommendations

  • API Keys โ€” Set your Alpaca, Binance, or other API keys in flx/config.py or via environment variables. Never commit secrets.
  • Training Time โ€” Start with low total_timesteps (1,000) to verify the pipeline works, then scale up (100k+) for serious training.
  • GPU Support โ€” Stable Baselines 3 supports CUDA. Install PyTorch with GPU support for faster training.
  • Data Quality โ€” Yahoo Finance is free but has limitations. For institutional-grade data, use WRDS or Alpaca.
  • Backtesting โ€” Always backtest before paper/live trading. Compare against multiple baselines.

๐Ÿ“„ License

This project is licensed under the MIT License โ€” see the LICENSE file for details.

Disclaimer: This software is for educational and research purposes only. Nothing herein constitutes financial advice or a recommendation to trade real money. Users are solely responsible for any financial decisions made using this software. Consult a qualified professional before deploying capital.


Built by Digital Metro ๐Ÿš€

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages