alpha-engine
Everyone has a strategy that “would have worked.” Almost nobody can prove it.
A backtesting framework that tries its best to prove your strategy doesn't work before the market does it for you — with real transaction costs, walk-forward validation, and a paper-trading loop against live prices.
// why this exists
Three ways backtests lie
I've seen too many backtests that were fiction. Someone loops over historical prices, forgets that trades cost money, accidentally lets tomorrow's data leak into today's decision, and ends up with a beautiful equity curve that would have lost money in the real world.
Lookahead bias
Your signal at time t quietly uses information that only exists at t+1. Sometimes it's as subtle as applying today's weight to today's return.
Ignored costs
A strategy with 50% daily turnover and a paper Sharpe of 2.0 is often a Sharpe of 0.3 after spread and market impact.
Overfitting
Tune parameters on the full dataset and you've built a model of the past, not the future.
How this engine stops them
- • Weights are shifted one day before touching returns — the code structurally can't earn tomorrow's return on today's signal
- • Every rebalance pays spread + square-root market impact
- • Walk-forward trains on 504 days, tests on 63, steps forward. Overfit strategies get exposed.
The honest part
The gap between backtest and live paper trading is itself a measurement. That's why the same weight targets can be sent to Alpaca's paper API — real fills, real slippage, real answers.
// what ships with it
Two strategies, deliberately simple
momentum
Winners keep winning — for a while.
12-1 month cross-sectional momentum: rank stocks by the last year's return, skip the most recent month, scale by inverse volatility, only long names above their 200-day average. Long the top 20%, short the bottom 20%, capped at 10% per name.
mean reversion
Stretched prices snap back.
Z-score of the 20-day price against the 60-day mean. Enter past ±2 standard deviations, but only if RSI confirms — so you're not catching falling knives. Exit when the z-score crosses zero.
// the report card
Thirteen metrics, because Sharpe alone isn't enough
Sharpe, Sortino, Calmar, max drawdown, win rate, profit factor, skew, kurtosis, 95% VaR and CVaR — plus daily turnover and total cost drag, because those last two tell you whether the strategy survives contact with a broker. The Streamlit dashboard shows the NAV curve, drawdown chart, rolling Sharpe, and a weight heatmap of what the strategy actually held.