derek nester

Statistical Arbitrage Engine

Pairs-trading research on S&P 500 stocks. Every result here is walk-forward and out-of-sample, and the strategy runs on a paper account. No real money yet.

personal project, 2026Python · pandas · statsmodels · Plotly

github.com/DerekNest/StatisticalArbitrageEngine

Read this first

This is research, and it has never traded real money. The numbers below are walk-forward and out-of-sample: each test year is traded with settings fit on the two years before it, on daily prices with next-open fills. The same code also trades an Alpaca paper account. I’m not claiming it will make money live.

The problem

Two stocks in the same business tend to move together. When the gap between them stretches unusually far, a pairs trade bets it closes: buy the cheap one, short the expensive one. The hard parts are finding pairs where that relationship is real, noticing when it breaks, and testing all of it without fooling yourself.

Here is the rule itself. Drag the marker to stretch the gap and watch what the strategy does.

z = 0.0flatdrag the marker: this is how far the gap between the two stocks has stretched.

The lines are the thresholds in config.py. This is a simplified picture of the rule, not a result: the engine acts on the previous day's close, holds a trade for a minimum number of bars, and gives up on one after 45.

Between the lines, the same z-score means two different things: with no trade open it waits, and with one open it holds. Testing all of this without fooling myself is the part I cared about most.

What I built

  • A pair screener. It looks only within a sector and puts each pair through five checks in order: return correlation of at least 0.70, an Engle-Granger cointegration test, an ADF test on the spread, a half-life between 5 and 126 trading days, and a Chow test for structural breaks at three split points.
  • A spread model. A rolling hedge ratio and a 20-bar z-score, with the hedge ratio fixed before the trading period starts so nothing leaks from the future.
  • An event-driven backtester. One day at a time, signals at the close and fills at the next open. It re-screens pairs every quarter on a rolling two-year window. An earlier version screened once and traded the same pairs for seven years, and lost money holding pairs through their breakdowns.
  • Risk controls. Half-Kelly sizing capped at 25% of equity per pair, a per-pair loss cap, and drawdown limits that shrink and then stop new positions.
  • Walk-forward validation. Four folds, two years of training and one year of testing each, test years 2020 through 2023.
  • A paper trader that submits end-of-day orders to an Alpaca paper account.
Research pipeline: prices feed the pair screener, then the spread model, then signals, then the backtester with risk controls, then walk-forward validation. Signals also feed a paper trader. pricesdaily closes, yfinance118 stocks, by sector pair screenercorrelation · cointegrationadf · half-life · chow spread modelrolling hedge ratio20-bar z-score signalsenter 2.2 · exit 0.5stop 3.2 · 45-bar limit backtester + riskevent-driven · next-open fillshalf-kelly · circuit breakers walk-forward4 folds2 yr train · 1 yr test paper traderalpaca paper account re-screened every quarter only these numbers count
The dashed line is the only path that touches a broker, and it's a paper account.

The real numbers

All out of sample: each fold’s pairs and hedge ratios come from its two training years, and the result is measured on the year after.

Test year Pairs Trades Return Sharpe Max drawdown
2020 19 6 +3.14% 1.193 −1.49%
2021 38 22 +7.58% 1.240 −2.72%
2022 51 12 +5.51% 1.930 −1.17%
2023 25 3 +2.43% 1.634 −0.94%

Out-of-sample Sharpe for each test year, from zero. The area of a dot is the number of trades behind it, so the eye gives 2023 about as much weight as three trades deserve.

Median fold Sharpe is 1.434 and the mean is 1.499, over 43 trades. I lead with the median because 2022 is doing a lot of the work in the mean.

Why you should discount those numbers:

  • 43 trades is a small sample. The 95% interval on the Sharpe is roughly ±0.5, so the true figure could be anywhere from about 1.0 to 2.0.
  • The thresholds were tuned on these same folds. I picked the entry, exit and stop levels with a grid search over 48 combinations, scored on out-of-sample Sharpe. That keeps in-sample data out of the score, but it means the winning combination was chosen by looking at these test years. The headline has selection bias in it, and a clean test needs a holdout the search never saw.
  • 2022 may be a one-off. Insurers repriced at different speeds while rates rose, which made exactly the kind of spreads this strategy trades.
  • 2023 is three trades. Spreads were compressed and the strategy mostly sat out.
  • Costs are optimistic. Slippage is a flat 0.05%, shorting is assumed free and always available, and prices are daily adjusted closes from yfinance with a universe I fixed at the start.

I also re-ran the screener on November 16, 2026 to make sure it still works. On the full 2018–2024 history it tested 551 candidate pairs and passed 33.

What’s broken or unfinished

  • No published paper track record. I’ve run it on an Alpaca paper account and it was up the last time I checked, but that log isn’t in the repo, so I’m not putting a number on it here.
  • No untouched holdout, for the reason above.
  • Cointegration is tested one pair at a time with Engle-Granger, which depends on which stock you put on the left side.
  • No market-impact model. Fine at this size, wrong at any real one.

What I’d do next

  1. Hold out data the parameter search never sees, and report that number instead.
  2. Run the paper trader for a few months and publish the log, good or bad.
  3. Replace Engle-Granger with the Johansen test, and the walk-forward split with combinatorial purged cross-validation.
  4. Model costs properly: borrow fees and size-dependent impact.

the repo · ← back to the field notes