Edges
Pairs Trading Explained
Most trading strategies have a direction problem: they need the market to cooperate. A long momentum strategy needs prices to keep rising. A mean reversion strategy needs prices to stop falling. Both are exposed to the same broad market forces — a large macro move wipes out the position regardless of how sound the individual instrument thesis is.
Pairs trading sidesteps the direction problem. By going long one asset and simultaneously shorting a correlated asset, the strategy constructs a position that is insulated from broad market moves. The bet is not on whether the market goes up or down — it is on whether the spread between two related instruments narrows after an unusual divergence. This is the defining characteristic of market-neutral strategies: profit comes from relative value, not directional movement.
Pairs trading is a form of mean reversion applied across two assets rather than one. Instead of asking whether a single asset's price reverts to its own historical mean, you ask whether the relationship between two assets reverts to its historical equilibrium. The logic is the same; the cointegration framework is what makes the cross-asset version structurally sounder than applying mean reversion to a single trending instrument.
What Pairs Trading Is
Pairs trading is a long/short strategy built on the temporary divergence of two instruments that historically move together. When one instrument rises relative to the other — creating a spread wider than normal — the trade goes long the underperformer and short the outperformer. Profit occurs if the spread narrows back toward its historical equilibrium, which is the reversion event the strategy is betting on.
The key word is temporary. The trade assumes the divergence is a transient displacement from a long-run equilibrium, not a permanent repricing. If the divergence is permanent — if one company in the pair has undergone a fundamental change — the position will not recover. Distinguishing temporary from permanent divergence is the central risk management problem in pairs trading.
The spread. The spread is the difference (or ratio, depending on implementation) between the two instruments' prices, adjusted for a hedge ratio. A simple 1:1 spread takes the price of asset A minus the price of asset B. A hedge-ratio-adjusted spread multiplies one leg by a coefficient derived from ordinary least squares (OLS) regression, so that the expected dollar value of the long and short positions moves together in the same direction by a proportional amount. The OLS hedge ratio is more accurate than 1:1 in most pairs because the two instruments rarely have identical price magnitudes or volatility profiles.
Entry and exit via z-score. Once the spread is computed, the signal is expressed as a z-score of the spread — how many standard deviations the current spread sits above or below its rolling mean. At z = +2.0, the spread is unusually wide (asset A is expensive relative to asset B). The trade enters long B and short A. When the spread reverts toward z = 0, the position closes at a profit. The z-score framework is the same one used in single-asset mean reversion strategies; it normalizes for the spread's natural volatility.
Cointegration vs. Correlation
Selecting valid pairs is where most implementations fail, because correlation is the wrong metric and cointegration is the correct one.
Why correlation is insufficient. Two assets can be highly correlated over a period and still not be suitable for pairs trading. Correlation measures whether two price series tend to move in the same direction at the same time. It does not measure whether their price relationship — the spread — is stable over the long run. Two oil producers might be highly correlated in daily returns because they both respond to oil price moves. But if one has been gradually gaining market share while the other has been losing it, their price levels have been drifting apart over time. The spread is trending, not mean-reverting. Entering a pairs trade on the expectation of spread reversion when the spread is structurally widening is a systematic money-losing trade.
Cointegration measures long-run equilibrium. Two price series are cointegrated if there exists a linear combination of them that is stationary — that is, the combined series has a stable long-run mean and does not drift indefinitely. Cointegration was formalized by Granger and Engle (1987), and the standard test for it is the Engle-Granger two-step cointegration test. The test checks whether the residuals of an OLS regression of one price series on the other are stationary. If they are, the two series share a long-run equilibrium relationship, and the spread will revert to that equilibrium after temporary divergences.
Cointegration is a stronger condition than correlation. Two assets can have zero return correlation but still be cointegrated if their price levels are linked by a structural economic relationship. The relevant question for pairs trading is not whether they tend to move together in the short run (correlation) but whether their prices are bound together in the long run (cointegration).
Practical test threshold. The Engle-Granger test produces a p-value. A p-value below 0.05 is the conventional threshold for rejecting the null hypothesis of no cointegration — meaning there is less than a 5% probability the cointegration is spurious. For a pairs trading strategy, you should use 0.05 as a ceiling, not a target. Pairs that test at p = 0.001 have more structural robustness than pairs that test at p = 0.04. In live environments, cointegration strength degrades over time, so starting with a strong relationship provides more buffer.
How to Select Valid Pairs
The starting universe for pair selection should be restricted to instruments with a plausible structural economic link. Statistical cointegration without a business rationale is fragile — two instruments might have happened to be cointegrated over a sample period by chance. A pair where the cointegration is grounded in shared economics is more likely to remain cointegrated in the future.
Same sector, similar business model. The most durable pairs share the same sector and similar revenue drivers — two regional banks in the same geography, two oil refiners with overlapping feedstock costs. The structural link creates a fundamental reason for prices to remain connected.
Engle-Granger p-value below 0.05. After identifying candidates by economic rationale, run the cointegration test on at least 2–3 years of daily price data. A p-value below 0.05 is required; below 0.01 is preferred. Pairs that pass the threshold in multiple non-overlapping sample windows are more reliable than pairs that pass in a single window.
Stable cointegration over time. Run the cointegration test on rolling windows — test the past 252 trading days at monthly intervals for the past three years. If the p-value is consistently below 0.05 across most windows, the pair is structurally sound. If it has been rising toward 0.05 or crossing it intermittently, the relationship is weakening.
Adequate liquidity on both legs. Both instruments need to be liquid enough that the long and short legs can be entered and exited near the observed price. The spread you compute on historical data may be unreachable in practice if market impact moves the price on entry.
Sizing the Spread Trade
Once a signal is identified, the position must be sized so that the long and short legs genuinely offset each other.
Dollar-neutral sizing. The simplest approach: match the dollar value of the long leg to the dollar value of the short leg. If you allocate $10,000 to the trade, $5,000 goes long asset A and $5,000 goes short asset B. Dollar neutrality eliminates exposure to broad market moves that affect both assets proportionally by dollar. It does not eliminate beta exposure if the two assets have different betas to the market.
Beta-neutral sizing. Size each leg so that the portfolio beta is zero. If asset A has a beta of 1.2 and asset B has a beta of 0.8, a dollar-neutral allocation is not beta-neutral — the long leg carries more market exposure than the short. Scale the short leg by the ratio of betas to achieve beta neutrality. Beta-neutral pairs trades are less exposed to broad market moves during the holding period.
Z-score entry and exit thresholds. Entry typically occurs when the spread z-score exceeds ±2.0. Exit occurs as the z-score reverts toward 0, typically triggered at ±0.5. A stop-loss at ±3.0 or ±3.5 cuts the trade if the spread widens further rather than reverting — this is the signal that the divergence may not be temporary. The stop-loss level is the mechanism for distinguishing between a temporary displacement and a permanent repricing while the position is live.
When It Fails
Pairs trading fails primarily through permanent decoupling — when the cointegrated relationship between two instruments breaks down and does not recover.
Corporate events. An acquisition announcement on one instrument in a pair immediately reprices it to approximately the acquisition price, disconnecting it from the other instrument's dynamics. The spread does not revert — the acquired company's price converges to the deal price and stops moving. Any open pairs position in that relationship must be closed immediately when the announcement is made, not held in hopes of spread reversion.
Sector structural shifts. A regulation that affects one company in a pair but not the other can permanently change the cost structure or revenue outlook of one leg while leaving the other unchanged. The spread widens and stays wide. Historical cointegration breaks down. The pair's structural economic link — the reason they were cointegrated — has been severed.
Business model divergence. Two companies in the same sector can move in different directions: one pivots to a higher-margin model, one accelerates repurchases, one expands internationally while the other contracts. These divergences are slow and difficult to detect in real time; the cointegration relationship degrades gradually rather than breaking sharply.
Rolling cointegration as early warning. Compute the Engle-Granger p-value on a rolling 252-day window and monitor it over time. A p-value that was consistently below 0.02 and has risen to 0.08 signals a weakening relationship. Reduce position size or exit as the p-value drifts upward — do not wait for the relationship to break completely before responding.
How to Code It in Python
The following implements the cointegration test and spread z-score signal. The test_cointegration function returns the test score and p-value from the Engle-Granger test. The spread_zscore function computes the rolling z-score of the spread.
import pandas as pd
import numpy as np
from statsmodels.tsa.stattools import coint
def test_cointegration(price_a: pd.Series, price_b: pd.Series) -> tuple:
score, p_value, _ = coint(price_a, price_b)
return score, p_value
def spread_zscore(price_a: pd.Series, price_b: pd.Series, window: int = 30) -> pd.Series:
spread = price_a - price_b # simplified; in practice use OLS hedge ratio
rolling_mean = spread.rolling(window).mean()
rolling_std = spread.rolling(window).std()
return (spread - rolling_mean) / rolling_std
The coint function from statsmodels.tsa.stattools implements the Engle-Granger test. It regresses price_a on price_b, then tests the residuals for stationarity using an augmented Dickey-Fuller test. The returned p_value is what you compare against the 0.05 threshold. score is the ADF test statistic — the more negative it is, the stronger the evidence for cointegration, but the p-value is more interpretable for threshold decisions.
OLS hedge ratio in production. The simplified spread = price_a - price_b in spread_zscore assumes a 1:1 relationship between the two price series. In practice, use OLS to find the hedge ratio:
import statsmodels.api as sm
def ols_hedge_ratio(price_a: pd.Series, price_b: pd.Series) -> float:
model = sm.OLS(price_a, sm.add_constant(price_b)).fit()
return model.params[price_b.name]
Multiply price_b by the hedge ratio before subtracting from price_a to compute the spread. The hedge ratio changes over time — recompute it on a rolling window (every 30–60 days is typical) to account for gradual drift in the relationship.
Generating signals from the z-score. A signal series can be derived directly from the z-score output: enter long the spread (long A, short B) when z < -2.0; enter short the spread when z > 2.0; exit when |z| < 0.5. The spread_zscore function above returns the full z-score series; the signal logic sits in the caller.
The Oyamori Approach
Oyamori treats pairs trading as a core category within the mean reversion edge family. The pairs catalog includes instruments selected through the process described above: same-sector economic rationale first, cointegration test second, rolling stability check third. Pairs that pass all three filters are tracked continuously; when a pair's rolling cointegration p-value begins to drift, it is flagged before the relationship breaks completely.
The platform monitors spread z-scores across all catalog pairs in real time and generates entry signals when the spread exceeds the entry threshold in a pair whose cointegration health is still strong. Position sizing is beta-neutral by default, with dollar-neutral as an option for instruments where beta estimates are unreliable.
For traders building their own pairs framework, the cointegration test and spread z-score functions above cover the core signal logic. The structural challenge is pair selection and ongoing monitoring — a pairs strategy that enters without checking whether the cointegration relationship is still intact is taking a bet on historical data that may no longer describe the current relationship between the two instruments.
Next: Gap Trading Strategy →