joshriang
← Back to portfolio

Quant research & trading·Python·Published

Backtest Harness

This is the small, dependency-light engine I reach for when I want to see what a weight series actually does bar by bar instead of trusting a vectorised shortcut. A strategy is just a function that takes a price DataFrame and returns a Series of target weights in [0, 1], and the engine steps through each bar, marks the position to market, rebalances when the weight changes, and records every trade with entry and exit dates, prices, size and PnL. It ships three built-in weight functions, SMA crossover, 12-month momentum and a recent-return mean reversion, plus Sharpe, Sortino, max drawdown, Calmar, total return and win rate in metrics.py. The walk-forward wrapper runs rolling windows with an optimizer callback, and the README example uses train_size=504, test_size=126, step=63 on SPY, which is two years of training, six months of testing and a quarterly roll. The CLI pulls from yfinance and dumps metrics to JSON, so a single command gets me a number I can compare across strategies. Tests run on seeded synthetic prices, so the suite is deterministic and offline.

Date
2026
Main stack
Python
Code
github ↗
01
02
  • A bar-by-bar loop is slower than a vectorised panel but it makes the fill assumption explicit, and that is where most backtest bugs hide.
  • Constraining weights to [0, 1] removes short-selling and leverage questions from the engine entirely, which keeps the comparison between strategies about signal quality rather than about who took more risk.
  • Rolling walk-forward with a 63-day step gives roughly eight test windows per two years of data, which is enough to see whether a parameter set is stable or just lucky in one window.
03

Scale 0–100: how much this project relied on each skill.