Pair Trading
Identify temporary price divergences between statistically related assets
Risk
Low
Holding Period
Days to weeks
Best For
Steady returns in sideways markets
How it works
Think of two assets that have historically moved together. When their prices temporarily diverge, the model measures how far they have drifted from their long-run relationship. Pair trading research is built on the observation that cointegrated series tend to revert to their equilibrium.
Mathematical Foundation
spread = price_A − β × price_B Z = (spread − μ) / σSignal Generation Logic
- 1Scan all asset pairs for cointegration using the Engle-Granger test (p-value < 0.05)
- 2Estimate hedge ratio β via OLS regression of price_A on price_B
- 3Calculate rolling 150-day spread mean (μ) and standard deviation (σ)
- 4Enter long spread when Z-score < −1.2 (spread is cheap relative to history)
- 5Enter short spread when Z-score > +1.2 (spread is expensive relative to history)
- 6Exit when |Z-score| < 0.5 — spread has normalised
- 7Re-test cointegration every 63 bars; force-exit if relationship breaks down
Parameters Explained
entry_zZ-score threshold to enter a position. Lower = more trade signals but lower quality. Higher = fewer but more selective trades.
Default
1.2exit_zZ-score threshold to exit. When the spread returns within this band, close the position. Setting too close to 0 risks premature exits.
Default
0.5coint_windowRolling window in bars for the cointegration test and spread statistics. Longer windows are more stable but slower to adapt to changing relationships.
Default
150recalc_freqHow often to re-test cointegration (in bars). Default is ~quarterly. More frequent re-testing catches relationship breakdowns faster but increases computation.
Default
63When It Works
In range-bound markets where cointegrated assets temporarily diverge. Best when there is a clear fundamental reason for the pair to stay connected (same sector, correlated economic drivers, commodity vs. currency). Low-volatility, non-trending environments are ideal.
When It Fails
During regime changes when the cointegration relationship structurally breaks down (e.g., a currency peg breaks, or two companies diverge due to M&A activity). Also underperforms in strong trending markets where mean reversion fails.
Risks & Limitations
- Cointegration can break down without warning — structural market changes may invalidate the pair permanently
- Spreads can remain divergent far longer than expected, leading to large drawdowns before mean reversion occurs
- Two-leg transaction costs can significantly erode profits on short holding periods
- Data-mining bias: running many pairs and selecting cointegrated ones can produce spurious results
- Requires careful synchronisation of both legs — execution slippage on one side creates unintended net exposure
Implementation
Uses statsmodels for the Engle-Granger cointegration test and OLS regression to estimate hedge ratios. Spread statistics are computed on a rolling basis to prevent look-ahead bias. Re-testing every 63 bars ensures the strategy exits stale pairs before significant losses accumulate.
Model parameters
Entry Z-Score
Open when spread Z ≥ 1.2σ
Exit Z-Score
Close when spread Z ≤ 0.5σ
Cointegration Window
Days used for Engle-Granger test
Recalc Frequency
Re-test cointegration every ~quarter
Academic background
Academic Basis
Based on Gatev, Goetzmann & Rouwenhorst (2006), 'Pairs Trading: Performance of a Relative-Value Arbitrage Rule', Review of Financial Studies
Backtest this strategy
Run the exact model on your selected assets and date range. See trade-by-trade performance.
Backtest This →