Algorithmic Trading
A systematic trading research project in Python: gradient-boosted trees, an LSTM, and a reinforcement-learning agent, then a simpler weekly factor model chosen for honest, testable behavior.
Cumulative stocks replaced
- Top 10 weekly
- Patient
- Bought this week
Motivation
Markets are one of the most data-rich systems available to study, and one of the easiest to fool yourself with. This project is practice in turning an idea about market behavior into a precise, testable strategy, and then trying hard to prove it wrong.
The data pipeline
Python fetchers collect daily prices for the S&P 500 since 2015, company fundamentals, and macroeconomic series from the Federal Reserve’s FRED database (rates, credit spreads, the VIX), all stored locally as Parquet files. A backtesting engine and every model read from that one store.
Three machine learning models
Before settling on a strategy, I built and compared three families of model on a single stock, using the same features and test periods:
- XGBoost: gradient-boosted trees predicting whether Apple stock rises over the next five days, with SHAP values to explain each prediction and an analysis of which market regimes the model gets wrong.
- LSTM: a recurrent neural network in PyTorch that sees the last 20 days as a sequence, to test whether trends in the features (a VIX rising for ten days, a slowly inverting yield curve) carry information a snapshot misses.
- Reinforcement learning: a PPO agent (Stable-Baselines3, in a custom Gymnasium environment) that learns position sizing directly from a reward that penalizes drawdowns.
All three use walk-forward validation: train on two years, test on the next six months, step forward, and stitch the out-of-sample predictions together. Scalers are fit on training data only. In finance, a model evaluated on data it has seen in any form will look far better than it is.
The factor model
The strategy I settled on is deliberately simpler: a cross-sectional factor model that ranks S&P 500 stocks each week on momentum (50%), quality (35%), and low volatility (15%), and holds the top ten. It is designed to run weekly from a GitHub Actions schedule against a paper-trading account, with a dry-run preview before any orders are placed.
Patient rebalancing keeps a stock until it falls below rank 15, not the moment it slips out of the top 10. Stocks hovering around the cut-off stop generating trades every week. The replay at the top of this page runs both rules through the same 222 weeks: rebalancing to the top 10 every week replaces 284 stocks, and patient rebalancing replaces 117, 59% fewer trades.
Testing the signals honestly
The information coefficient measures whether a factor actually ranks next week’s winners above its losers. Computed weekly across the whole universe since 2021, every factor’s average is close to zero, and the confidence intervals for momentum and low volatility comfortably include zero:

That chart is the most useful result of the project so far. Weekly stock returns are noisy, and a backtest built on signals this weak can look impressive by luck. Two other biases make it worse: the universe is today’s S&P 500, so companies that fell out of the index are missing (survivorship bias), and the fundamentals are a current snapshot applied to the past. For those reasons I don’t publish backtest returns for this strategy.