Quant research & trading·Python·Published
Quant Research Platform
I wrote this after rebuilding the same lag-and-compound backtest loop for the fourth time in a notebook, and I wanted one engine that every strategy plugs into instead of a folder of scripts. BacktestEngine takes a dict of {ticker: OHLCV DataFrame}, builds a close-price panel, asks each strategy for target weights, lags those weights one day so nothing peeks at the bar it trades on, and compounds returns with transaction costs charged proportional to turnover. It emits total return, CAGR, Sharpe, Sortino, max drawdown, Calmar, volatility, hit rate and turnover, and the optional walk-forward mode splits the panel 70/30 into in-sample and out-of-sample and reports both sides rather than just the flattering one. Strategies register through a @register class decorator, and the three built-ins are 12-1 momentum, a z-score band mean reversion, and a trend-plus-momentum composite. Around the engine I put a FastAPI service with /health, /strategies and a POST /backtest, a Streamlit dashboard with a backtest runner, run history and strategy catalog, a PaperBroker that rebalances on an APScheduler cron and appends to JSONL, and a docker-compose file. Results persist as metrics.json plus a Plotly HTML report with equity curve, drawdown and rolling Sharpe, and the pytest smoke suite runs the whole path on synthetic data so CI never needs the network.



- Lagging target weights by one bar is the single cheapest defence against look-ahead bias, and it has to live in the engine rather than in each strategy, because every strategy author eventually forgets it.
- Charging costs as a fraction of turnover rather than a flat per-trade fee makes high-turnover strategies pay for their own churn automatically, which changes which strategy looks best far more than the fee level does.
- A 70/30 in-sample/out-of-sample split is a sanity check, not validation; it tells you whether the parameter choice survived one regime boundary, not whether the edge is real.
Scale 0–100: how much this project relied on each skill.