← Back

alpha-engine

Everyone has a strategy that “would have worked.” Almost nobody can prove it.

A tester for trading strategies. It replays a strategy over past prices and tries hard to prove it doesn't work, before real money is at risk. It charges realistic trading costs, judges the strategy only on periods it hasn't seen, and can send the same trades to a practice (paper) account.

PythonpandasnumpyAlmgren impact modelAlpaca APIStreamlitpytest
GitHub →

// 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.

1

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.

2

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.

3

Overfitting

Tune parameters on the full dataset and you've built a model of the past, not the future.

How this engine stops them

  • • Today's decision is delayed by a day before it is scored, so the code can't cheat by using tomorrow's prices
  • • Every trade pays a realistic cost: the gap between buy and sell prices, plus extra when the order is large
  • • Walk-forward testing: tune on about two years, test on the next three months, then slide forward and repeat. A strategy that only worked on the data it was tuned on gets 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.

Rank stocks by how much they rose over the past year, ignoring the most recent month. Buy the top 20% and bet against the bottom 20%, hold less of the bumpy ones, only buy names trading above their 200-day average, and never put more than 10% in one stock.

mean reversion

Stretched prices snap back.

Measure how far the recent price has stretched away from its 60-day average. Trade only when it is unusually far (about two standard deviations) and a momentum gauge (RSI) agrees, so you aren't catching a falling knife. Exit when the price is back to normal.

// the report card

Thirteen metrics, because Sharpe alone isn't enough

Return, volatility, Sharpe (return per unit of risk), max drawdown (the biggest fall from a peak), win rate, tail-loss measures and more — 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.