Advanced QuantLesson 3
Pairs Trading & Statistical Arbitrage
Find pairs of correlated stocks and trade the spread reverting to mean.
12 minute read
4 key takeaways
Pairs Trading: The Market-Neutral Strategy
Find two stocks that move together. When they diverge, bet they'll converge again. It's mean-reversion with a market hedge.
Finding Cointegrated Pairs
Two stocks are cointegrated if they have a long-term equilibrium relationship, even if individually they trend.
python
from statsmodels.tsa.stattools import coint
# Find pairs with high cointegration
scores, pvalues, _ = coint(stock_A, stock_B)
if pvalues < 0.05:
print(f"Stocks are cointegrated (p={pvalues})")
# This pair is tradeable!
else:
print("Not cointegrated - skip this pair")
# Classic pairs: KO/PEP, GS/MS, XOM/CVX
The Spread: The Core
Spread = Price_A - β × Price_B
Where β comes from regressing A on B
python
# Calculate hedge ratio
from sklearn.linear_model import LinearRegression
X = stock_B.values.reshape(-1, 1)
y = stock_A.values
model = LinearRegression()
model.fit(X, y)
beta = model.coef_[0]
# Calculate spread
spread = stock_A - beta * stock_B
# Normalize spread (z-score)
spread_mean = spread.rolling(60).mean()
spread_std = spread.rolling(60).std()
z_score = (spread - spread_mean) / spread_std
# Trade signals
if z_score[-1] < -2:
signal = "BUY spread (long A, short B)"
elif z_score[-1] > 2:
signal = "SELL spread (short A, long B)"
Execution: The Tricky Part
- SIMULTANEOUS entry/exit reduces slippage
- Use limit orders on both legs
- Watch commissions—you're paying twice (round trip)
- Bid-ask spreads matter (you pay spread on both A and B)
Market-Neutral: The Advantage
If market crashes 20%, both A and B might drop 18-22%. Your spread (hedge) protects you. Pairs trading is immune to market risk.
Pairs Trades are Boring
Pairs typically make 10-15% annual returns. Small, steady, boring. But in 2020 when everything crashed, pairs traders made money while directional traders got destroyed.
Key Takeaways
- Find cointegrated pairs—they move together but sometimes diverge
- Trade the spread when z-score is extreme
- Pairs trading is market-neutral (beta = 0)
- It's mean-reversion on steroids (more reliable than single-stock MR)