Pairs Trading (Cointegration)
Trade the spread between two cointegrated assets back to equilibrium.
Understand what this strategy is actually betting on before you touch the parameter panel.
Read the model brief like a skeptic
Strategy families & selection
Validation & skepticism
The Intuition
Pairs trading exploits long-run equilibrium relationships between two economically related assets. When two stocks (or ETFs, or futures) share common risk factors — same sector, same supply chain, same macro driver — their prices should move together in the long run. Short-term divergences from that co-movement relationship are the tradeable signal.
The statistical foundation is cointegration, formalised by Engle and Granger (1987). Two non-stationary price series are cointegrated if there exists a linear combination of them that is stationary. In practice, this means the spread between two assets (after accounting for a hedge ratio) should have a finite, bounded variance — it oscillates around a constant, rather than random-walking away.
Gatev, Goetzmann, and Rouwenhorst (2006) showed that pairs trading generated positive Sharpe ratios in US equities from 1962–2002, attributing the returns to temporary liquidity provision: when one stock falls and another rises, pairs traders buy the loser and short the winner, effectively providing liquidity to the market. The convergence then earns the liquidity premium.
Key assumptions: (1) The pair is genuinely cointegrated — confirmed by the Engle-Granger test (p-value < 0.05). (2) The hedge ratio from OLS is stable over the trading period (it can drift in practice). (3) The spread z-score is a reliable entry/exit signal — the spread actually converges. If the fundamental relationship breaks (e.g., one company gets acquired, or a supply shock hits only one), the spread can diverge permanently, causing large losses.
Classic pairs in practice: Coke vs. Pepsi, gold vs. gold miners (GDX), crude oil vs. natural gas during linked periods, treasury futures of adjacent maturities. The strategy has become crowded in equities since the 2000s, compressing returns. Modern practitioners use cointegration in futures term structures, FX crosses, or across-country equity index pairs where fewer arbitrageurs operate.
The Math
Read this as a compact model summary: what the signal sees, what it ignores, and where fragility can creep in.
log_A(t) = log(Close_A(t))
log_B(t) = log(Close_B(t))
hedge_ratio = OLS coefficient of log_A on log_B
spread(t) = log_A(t) - hedge_ratio × log_B(t)
z(t) = (spread(t) - mean(spread[t-n:t])) / std(spread[t-n:t])
Entry: z(t) < -entry_z → long spread (long A, short B)
z(t) > entry_z → short spread
Exit: |z(t)| < exit_z → flat
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| ticker_b | str | QQQ | Second ticker for the pair |
| window | int | 60 | Rolling window for spread z-score |
| entry_z | float | 2.0 | Z-score threshold to enter trade |
| exit_z | float | 0.5 | Z-score threshold to exit trade |
Source Code
Live source — fetched from engine/strategies/pairs.py
Further Reading
- Engle, R. & Granger, C. (1987). Co-integration and Error Correction. Econometrica, 55(2), 251–276.
- Gatev, E., Goetzmann, W. & Rouwenhorst, K. (2006). Pairs Trading. Review of Financial Studies, 19(3), 797–827.
- Vidyamurthy, G. (2004). Pairs Trading: Quantitative Methods and Analysis. Wiley.
- Chan, E. (2013). Algorithmic Trading, Ch. 3. Wiley.
Related Mean Reversion Strategies
Use nearby strategies to compare the same market hypothesis under different signal constructions.