Pairs trading and cointegration
Trading the gap between two linked stocks: when it stretches far from normal, buy the one that lagged and short the one that ran ahead, betting the gap comes back.
5 min readLesson 55 of 57
In short
It only makes sense if the pair is cointegrated: a fixed mix of the two keeps returning to its average, which is more than moving together day to day. Test hundreds of pairs and some will look cointegrated by chance, so the results need adjusting for how many were tried.
Why it matters
Most strategies bet on prices going up. Pairs trading bets on a relationship instead: two similar companies, say two big oil producers, whose prices have kept a steady link. When one runs ahead of the other, you sell the one that ran ahead and buy the one that lagged, and profit if the gap closes, whether the market rises or falls.
It's one of the oldest quant strategies, and a good lesson in how easily statistics can fool you. Search enough pairs and some will look perfectly linked by chance.
How it works
Correlation isn't enough. Correlation says two stocks move the same way day to day; it says nothing about whether their prices drift apart over months. Two stocks can be highly correlated and still end the year 30% apart, with nothing pulling them back. Cointegration is the stronger idea: a fixed mix of the two prices, the gap, keeps returning to its average. Think of a dog on a long lead: each wanders, but they can't drift far apart.
MarketLab's pairs lab scans pairs of stocks from the same sector over a formation year. For each pair it fits the hedge ratio by least squares: how much A has tended to move for each 1% move in B. The gap is log A minus the hedge ratio times log B, so it's measured in percentages. The Engle–Granger test then gives a p-value: how likely a gap that reverts this convincingly would be if the pair weren't really cointegrated. It also reports the half-life, the number of trading days it typically took the gap to close half of a stretch.
Testing many pairs needs adjusting. About 400 tests at a 5% threshold find about 20 false discoveries by chance alone, the same trap as Overfitting and the subject of Luck or skill?. So the lab also shows a Benjamini–Hochberg adjusted p-value, which keeps the share of false discoveries among the pairs it keeps near 5%. The best of 400 p-values can be adjusted up as far as 400 times.
The chosen pair is then traded on the following year, using only the formation year's average and spread for the gap. Its z-score is how many standard deviations the gap sits from that average. Past +2, sell the gap: short A, buy B. Past −2, buy the gap. Close when the z-score gets back to 0, and stop out at ±4, since a gap that keeps widening may mean the link has broken. B's leg is the hedge ratio times A's in dollars, so a market-wide move roughly cancels out.
Shorting costs extra. A short seller borrows the shares and pays a fee for as long as the position is open, 0.5% a year by default here, on top of commission and slippage. MarketLab's public prices are simulated: stocks share only market and sector moves, so their gaps have nothing pulling them back. Few or no pairs survive the adjustment, and that's the honest answer, not a bug.
gap = log A − hedge ratio × log B; z-score = (gap − formation average) ÷ formation standard deviation
How stretched the gap is, measured against how it behaved in the formation year.
A worked example
Two oil producers, A and B, have a formation-year hedge ratio of 1.5: A has tended to move 1.5% for each 1% move in B. The gap averaged 1% with a standard deviation of 4%. You trade with $10,000.
- A month into the next year, A has run ahead and the gap is 11%. Its z-score is (11% − 1%) ÷ 4% = 2.5, past +2, so sell the gap: short A, buy B.
- Legs: B's is 1.5 times A's, and together they're $10,000. A's leg is $10,000 ÷ 2.5 = $4,000 short, and B's is $4,000 × 1.5 = $6,000 long.
- If the market lifts B 1%, the $6,000 long gains $60, and A's expected 1.5% rise costs the $4,000 short about $60. The market move cancels out.
- Stop: if instead the gap widens to a z-score of 4, that's 1% + 4 × 4% = 17%, and both legs are closed at a loss.
- Borrowing: 0.5% a year on the $4,000 short is $4,000 × 0.5% = $20 a year, paid on every day the trade is open.
Result: The trade wins if the gap closes and loses if it keeps widening; the market's direction barely matters. Its real risk is that the link that held last year has broken, which is why the stop and the formation-year test both matter.
How to read it
- Adjusted p-value below 5%
- The pair still looks cointegrated after allowing for how many pairs were tested. Rare, and worth testing on the next year.
- Raw p-value below 5%, adjusted well above
- Probably one of the chance discoveries that hundreds of tests always produce.
- Short half-life (a few days to a few weeks)
- Stretches close quickly, so there are more trades and each one ties up money briefly.
- Long or missing half-life
- The gap barely pulls back. A trade could wait months or never close.
In the real world
- Pairs trading was developed at Morgan Stanley in the mid-1980s by a team of quants led by Nunzio Tartaglia. A well-known 2006 study by Evan Gatev, William Goetzmann and Geert Rouwenhorst found simple pairs rules were profitable in US stocks for decades, with profits shrinking in later years as more funds did the same.
- Royal Dutch and Shell traded as two share lines with fixed claims on one business, yet their prices drifted as much as 30% apart from fair value for years. A gap that should close can stay open far longer than a trader can afford to wait.
- In August 2007, many market-neutral funds holding similar long-short positions lost heavily within days as crowded trades were unwound at once. Hedging away the market doesn't hedge away everyone else selling the same thing.
Common mistakes
- Treating high correlation as cointegration. Stocks can move together daily and still drift apart for good.
- Picking the best p-value from hundreds of tests without adjusting. Some pairs always look linked by chance.
- Measuring the z-score with the trading year's own average and spread, which you wouldn't have known at the time.
- Forgetting the borrow fee and costs. A small gap that closes slowly can be eaten entirely by them.
See it in MarketLab
Test yourself
Optional: 4 quick questions on the words, using the idea, and how it plays out in real markets.
Loading your answers…