Shipwright Studio

Pairs Trading Lab

Pairs Trading Lab is a free online tool that simulates how statistical pairs trading works: it generates a synthetic cointegrated stock pair in your browser, estimates the hedge ratio with OLS regression, tracks the spread's z-score against ±σ entry/exit bands, and backtests the mean-reversion trade with a full trade log.

No live market data, no sign-up, no API keys — every price series is simulated with a seedable random generator, so the experiment is reproducible. TICKER A / TICKER B are fictional.

Market & strategy

How hard the spread is pulled back to its mean. 0 = pure random walk.
Daily shock to the spread. More noise = wider bands, sloppier pair.
The hidden ratio used to build pair B. The Lab re-estimates it — see how close it gets.
A slow, smooth pull on the spread. The rolling window mostly absorbs this — try the shift below for a real break.
The spread's equilibrium jumps at day 126 and stays there — a takeover, a broken thesis. This is what kills pairs books.
Open a spread trade when |z| crosses this band.
Close the trade when |z| falls back below this band.
Flat friction per side — spread costs both entry and exit.

Estimates

Estimated hedge ratio β̂ (OLS)–
Mean-reversion half-life–
Stationarity (ADF-style heuristic)–
Price correlation (A, B)–
Current spread z-score–
Signal–

Price co-movement · rebased to 100

Stock A Stock B

Spread & bands · rolling 60-day ±σ

Spread (B − β̂·A, residual) ± entry band Entry marker Exit marker

Band backtest · $10,000 notional per leg

–
Total P&L ($)
–
Trades
–
Win rate
–
Max drawdown ($)
–
Return on exposure
Entry dayExit dayDirectionEntry zExit zP&L ($)

How to read this

Practical pairs trading is a statistical bet: two prices share a long-run equilibrium, so when they drift apart, the spread should snap back. This Lab walks through the whole pipeline on simulated data, in five steps:

  1. Generate. Stock A is a random walk; the spread follows a mean-reverting process (Ornstein–Uhlenbeck) with speed θ, plus optional smooth drift and a one-time regime shift halfway through; Stock B = β·A + spread.
  2. Estimate β — without peeking. Regress B on A over a 60-day formation window only (the shaded region on the spread chart). The residual is the estimated spread. Fitting on the full sample would be cheating: demeaning a random walk manufactures fake mean reversion, making even a broken pair look tradable.
  3. Score it. A rolling 60-day z-score normalises the spread, so "+2" means the same thing on day 100 and day 200.
  4. Test stationarity. A simplified ADF check asks whether the spread genuinely mean-reverts. t-stat below −2.86 ≈ stationary.
  5. Trade the bands. Short the spread above +entry σ (short B, long β̂·A), buy it below −entry σ, exit near zero. Positions are sized to $10,000 notional per leg, the way a real pairs book would size them — a flat per-side cost is then economically meaningful.

The core formulas

β̂ = Cov(A, B) / Var(A)
zt = (spreadt − μ60) / σ60
half-life = ln(2) / |γ|  ·  from Δspreadt = γ·spreadt−1 + ε

What the presets teach

Honest limitations