PT Pairs Trading Introduction
The original market-neutral strategy — buy one, short the other, profit from the gap
Learning Objectives
- •Understand pairs trading mechanics and why they work
- •Learn the spread, hedge ratio, and z-score framework
- •Evaluate risk factors specific to pairs strategies
Explain Like I'm 5
Pairs trading is one of the cleanest strategies in quantitative finance. Find two assets that move together (like EUR/USD and GBP/USD). When one becomes temporarily expensive relative to the other, sell the expensive one and buy the cheap one. You profit when they converge. The elegant part: you're long AND short simultaneously, so overall market direction doesn't matter.
Think of It This Way
Think of pairs trading as a bet on siblings. They're different people but share genetics. If one suddenly becomes far more successful than the other, you bet the gap narrows — either the laggard catches up or the leader falls back. You don't care if both do well or both struggle. You only care about the DIFFERENCE between them.
1How Pairs Trading Works
2The Spread is Everything
Pairs Trading Spread (Z-Score Over Time)
3Risk and Reality Check
4A Real PnL Profile
Pairs Trading Cumulative PnL (200 Trades)
5Practical Implementation Tips
Key Formulas
Spread Z-Score
Where S_t is the current spread, S-bar is the mean spread, sigma_S is the standard deviation. z > 2 suggests the spread is unusually wide (sell). z < -2 suggests unusually narrow (buy).
Hedge Ratio (OLS)
The hedge ratio determines how many units of B to trade for each unit of A, making the combined position market-neutral.
Hands-On Code
Basic Pairs Trading Strategy
import numpy as np
from sklearn.linear_model import LinearRegression
def pairs_trading(price_a, price_b, lookback=100, entry_z=2.0, exit_z=0.5):
"""Simple pairs trading strategy."""
log_a = np.log(price_a)
log_b = np.log(price_b)
# Compute hedge ratio
model = LinearRegression()
model.fit(log_b.reshape(-1, 1), log_a)
beta = model.coef_[0]
# Compute spread
spread = log_a - beta * log_b
# Rolling z-score
spread_mean = np.mean(spread[-lookback:])
spread_std = np.std(spread[-lookback:])
z = (spread - spread_mean) / spread_std
print(f"=== PAIRS ANALYSIS ===")
print(f"Hedge ratio (beta): {beta:.4f}")
print(f"Current z-score: {z[-1]:.2f}")
if z[-1] > entry_z:
print(f" SELL spread (sell A, buy B)")
elif z[-1] < -entry_z:
print(f" BUY spread (buy A, sell B)")
elif abs(z[-1]) < exit_z:
print(f" EXIT positions (spread reverted)")
else:
print(f" HOLD current position")
return z, betaComputes the hedge ratio via OLS, constructs the log spread, and generates entry/exit signals based on rolling z-score thresholds.
Knowledge Check
Q1.The spread between EUR/USD and GBP/USD has z-score = 2.5. What should you do?
Assignment
Implement a pairs trading strategy for EUR/USD vs GBP/USD. Compute the spread, z-score, and generate entry/exit signals. Backtest and compare performance to a pure directional strategy.