Code: https://github.com/drimranbhatti/reinforcement-learning-finance
Inspiration
Reinforcement learning is sold as a trading edge, but most demos never test out of sample or charge trading costs. I wanted to show where it helps and where it does not.
What it does
Three studies of sequential decisions in finance:
- Which asset to hold: epsilon-greedy, UCB1, discounted UCB and Thompson sampling bandits rotating across 9 sector ETFs over 1,451 weeks.
- How to sell a large position: dynamic programming for optimal execution, matched against the closed-form Almgren-Chriss path, plus value and policy iteration for a regime-switching allocation with costs.
- When to be in the market: a tabular Q-learning trader on trend and volatility states across 5 ETFs, retrained yearly.
How I built it
A small tested Python package (rlfin: bandits, mdp, tabular, trading) with 8 tests that check each method on a problem with a known answer before it touches market data: bandits must find the best arm, DP must equal the closed form and give TWAP when risk-neutral, value and policy iteration must agree, Q-learning must solve the cliff walk, and costs must be charged correctly.
Challenges I ran into
Weekly and daily returns are very noisy, so learning agents chase noise and turnover costs eat any gain.
Accomplishments that I'm proud of
- Dynamic programming reproduces Almgren-Chriss within one block and traces the cost-risk frontier; the regime policy halves the drawdown.
- The negative results are reported as measured: no bandit beats equal weight after costs (Sharpe 0.60 vs at best 0.46), and the Q-learner mostly learns buy-and-hold (out-of-sample Sharpe 0.67-0.74 vs 0.72 for SPY).
What I learned
RL is reliable where the dynamics are known (execution scheduling, cost-aware rebalancing) and weak where the signal is buried in noise.
What's next
Add function approximation (deep Q-networks) with purged walk-forward validation and realistic market-impact costs.
Built by Dr. Muhammad Imran Bhatti, PhD (FCMA, FCPA), fractional CFO and quant, from my WorldQuant University MScFE work. Research only, not investment advice.