Algorithmic Trading Using Deep Reinforcement Learning algorithms (PPO and DQN)
DeepRL-trade
Algorithmic Trading Using Deep Reinforcement Learning (PPO & DQN)
Introduction
In quantitative finance, stock trading is essentially a dynamic decision problem โ deciding where, at what price, and how much to trade in a stochastic, dynamic, and complex market. Deep reinforcement learning (DRL) enables modelling and solving these sequential decision problems with a human-like approach.
This project trains two DRL agents โ Proximal Policy Optimization (PPO) and Deep Q-Learning (DQN) โ to autonomously make trading decisions on GOOG stock and compares their performance against a Buy & Hold benchmark using risk-adjusted metrics.
Project Structure
DeepRL-trade/
โโโ configs/
โ โโโ default.yaml # Base config (all defaults)
โ โโโ ppo_goog.yaml # PPO experiment overrides
โ โโโ dqn_goog.yaml # DQN experiment overrides
โโโ src/
โ โโโ data/
โ โ โโโ source.py # DataSource (yfinance + Tiingo backends)
โ โ โโโ preprocess.py # Train/test split, rolling z-score normalisation
โ โโโ envs/
โ โ โโโ trading_env.py # Gymnasium-based trading environment
โ โโโ agents/
โ โ โโโ networks.py # Custom actor-critic network for PPO
โ โ โโโ ppo.py # PPO agent factory
โ โ โโโ dqn.py # DQN agent factory
โ โโโ evaluation/
โ โ โโโ testing.py # Multi-round evaluation with statistical analysis
โ โ โโโ metrics.py # pyfolio perf_stats wrapper, CI helpers
โ โโโ visualization/
โ โ โโโ plots.py # All plotting functions
โ โโโ utils/
โ โโโ config.py # YAML config loading, merging, validation
โ โโโ helpers.py # Seed, rounding, index helpers
โ โโโ logging.py # Centralised logging
โโโ scripts/
โ โโโ train.py # Training entry point
โ โโโ evaluate.py # Standalone evaluation
โ โโโ visualize.py # Generate figures from saved results
โโโ notebooks/
โ โโโ DeepRL_trader.ipynb # Original notebook (reference)
โโโ configs/
โโโ requirements.txt
โโโ .env.example # API key template
โโโ .gitignore
โโโ README.md
Quick Start
1. Install
python -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # Linux/macOS
pip install -r requirements.txt
2. Configure API keys (optional)
cp .env.example .env
Edit .env and add your TIINGOAPIKEY (only needed if using Tiingo)
3. Train
# Train PPO agent on GOOG (default config)
python scripts/train.py --config configs/ppo_goog.yaml
Train DQN agent
python scripts/train.py --config configs/dqn_goog.yaml
Override any parameter from CLI
python scripts/train.py --config configs/ppogoog.yaml --override seed=123 agent.totaltimesteps=100000
4. Evaluate
python scripts/evaluate.py --config configs/ppo_goog.yaml \
--checkpoint checkpoints/ppo/best_model.zip
5. Visualise
python scripts/visualize.py --results outputs/ppo/PPOGOOGresults.pkl \
outputs/dqn/DQNGOOGresults.pkl \
--output figures/
Configuration
All settings are in YAML files under configs/. The system works in layers:
configs/default.yamlโ all defaults- Experiment YAML (e.g.
ppo_goog.yaml) โ overrides specific keys - CLI
--overrideโ overrides anything at runtime
data, env, agent (with ppo/dqn sub-sections), evaluation, tracking, paths.
Experiment Tracking
Set tracking.usewandb: true in your config and ensure WANDBAPI_KEY is in .env (or run wandb login). Metrics, models, and configs are auto-logged to your Weights & Biases project.
How It Works
| Component | Description | |-----------|-------------| | Environment | Gymnasium-compatible discrete-action env. Actions: Long (+1), Cash (0), Short (-1). Reward = % change in total assets. | | PPO Agent | Custom actor-critic with LayerNorm + Dropout (configurable architecture). | | DQN Agent | Standard MLP policy from stable-baselines3. | | Evaluation | 500 random-start episodes โ point estimates, 95%/99% CIs, percentile bands, pyfolio stats. | | Data | OHLCV from yfinance (default) or Tiingo. Technical indicators via pandas-ta. Rolling z-score normalisation. |
References
- Human-level control through deep reinforcement learning (DQN): paper
- Proximal Policy Optimization (PPO): paper, blog, spinning-up