ebrahimpichka
DeepRL-trade
Jupyter Notebook

Algorithmic Trading Using Deep Reinforcement Learning algorithms (PPO and DQN)

Last updated Jul 2, 2026
21
Stars
3
Forks
0
Issues
0
Stars/day
Attention Score
50
Language breakdown
No language data available.
โ–ธ Files click to expand
README

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
Key sections: 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
๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท ebrahimpichka/DeepRL-trade ยท Updated daily from GitHub