Learn / Track V / lesson 3 of 5
Pairs trading and cointegration
Trading the spread between two related assets — how to find pairs that genuinely tie together, and how to size the legs.
The idea
Two assets driven by the same fundamentals — two oil majors, two exchange tokens, a coin and its liquid-staking wrapper — tend to move together. When they temporarily diverge, buy the cheap one, sell the rich one, and wait for convergence. Done well, the position is largely hedged against the market.
Correlation is not enough
Two prices can be highly correlated in returns and still drift apart forever. What you need is cointegration: a linear combination of the two price series that is stationary.
Each price can wander like a random walk, but the spread between them is tethered.
The Engle–Granger test
- Regress on to estimate the hedge ratio .
- Compute the residual spread .
- Run an ADF test on . If you reject the unit root (using Engle–Granger critical values, which are stricter than ordinary ADF ones), the pair is cointegrated.
The Johansen test handles more than two assets at once and does not depend on which asset you put on the left of the regression.
Log prices are often used instead of raw prices so that reflects relative percentage moves.
Trading the spread
Once you have a stationary spread, it is an Ornstein–Uhlenbeck process. The rest is the z-score trade: enter when exceeds a threshold, exit near zero, with the hedge ratio telling you how many units of B to trade per unit of A.
With on log prices, a 1% move in B is “explained” by a 0.6% move in A. For 10,000 of notional in A, hold 6,000 of B on the other side. When the z-score of the spread hits , sell 10,000 of A and buy 6,000 of B; close both legs when returns to 0.
Practical problems
- Unstable hedge ratios. drifts. Re-estimate it on a rolling window, or use a Kalman filter to track it continuously.
- Data mining. Test 5,000 pairs and roughly 250 will look cointegrated at 5% significance by chance. Require an economic reason for the link and confirm it out-of-sample. See overfitting.
- Leg risk and costs. You pay spread and fees on two instruments, and in fast markets one leg may fill while the other doesn’t. Execution logic matters as much as the signal.
- Structural breaks. Mergers, delistings, depegs and hacks break pairs permanently. Use stops and a time limit as in any mean-reversion trade.
Statistical arbitrage generalises the idea to baskets: regress each asset on the principal components of its sector and trade the mean-reverting residuals. It is pairs trading with many legs and a factor model in place of a single partner.
Put it to work
Build this idea
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a pairs-trading research system that screens a universe for cointegrated pairs with Engle-Granger and Johansen tests, trades the z-scored spread, and reports out-of-sample results with costs- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch