Introduction

In 1987 economists Robert Engle and Clive Granger described a relationship between time series that neither correlation nor regression fully captures: cointegration. Two prices can each wander like a random walk — unpredictable individually — yet be tethered by a shared long-run equilibrium. Whenever they drift apart, economic forces (competition, shared costs, investor arbitrage) pull them back together.

Pairs trading exploits exactly that tether. You find two cointegrated assets, track the ratio or linear combination of their prices (the spread), and bet on the spread returning to its historical mean whenever it stretches too far. When the spread is unusually wide you sell the expensive leg and buy the cheap one; when it collapses you close both positions.

The key measurement is the z-score — how many standard deviations the current spread sits away from its rolling mean. A z-score beyond ±2\pm 2 is the classic trigger; if the two assets are truly cointegrated, gravity tends to bring the spread home.

This is one of the oldest ideas in quantitative finance, a relative-value strategy that is market-neutral by design: gains and losses come not from the market's direction but from the gap between two related assets. Related ideas appear in bayesian inference (updating beliefs about the gap) and non-convex optimization (fitting the hedge ratio).

Trade the Spread

Below is a simulated cointegrated spread between two fictional assets. The spread is generated as a mean-reverting process — it wanders but always gravitates back to zero. The z-score band shows how stretched the spread currently is.

<div class="header-row">
  <span class="label">{{lbl_spread}}</span>
  <span id="zscore-badge" class="badge">z = 0.00</span>
  <span id="pnl-badge" class="badge pnl">P&amp;L: $0.00</span>
</div>
<canvas id="chart" width="540" height="180"></canvas>
<div id="status" class="status">{{msg_idle}}</div>
<div class="btns">
  <button id="btn-step" type="button">{{btn_step}}</button>
  <button id="btn-enter" type="button" disabled>{{btn_enter}}</button>
  <button id="btn-exit" type="button" disabled>{{btn_exit}}</button>
  <button id="btn-reset" type="button" class="ghost">{{btn_reset}}</button>
</div>
<div class="legend">
  <span class="dot spread-dot"></span>{{lbl_spread_legend}}
  <span class="dot entry-dot"></span>{{lbl_entry_legend}}
  <span class="dot exit-dot"></span>{{lbl_exit_legend}}
</div>
* { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; color: #222; margin: 0; }
.header-row { display: flex; align-items: center; gap: .5rem; margin-bottom: .4rem; flex-wrap: wrap; }
.label { font-size: .85rem; color: #555; flex: 1; }
.badge { font: 700 .85rem ui-monospace, monospace; background: #e8eef3; border-radius: 6px;
         padding: .2rem .55rem; white-space: nowrap; }
.badge.pnl.pos { background: #d4f0df; color: #0a7d33; }
.badge.pnl.neg { background: #fde0e0; color: #c92f3c; }
canvas { display: block; width: 100%; max-width: 540px; border: 1px solid #dde3ea;
         border-radius: 8px; background: #f8fafc; }
.status { font-size: .95rem; font-weight: 600; min-height: 1.4em; margin: .45rem 0; }
.status.ok { color: #0a7d33; }
.status.warn { color: #b35c00; }
.status.bad { color: #c92f3c; }
.btns { display: flex; gap: .45rem; flex-wrap: wrap; margin-bottom: .5rem; }
button { font: 600 13px system-ui; padding: .4rem .8rem; border: 1px solid #1d3557;
         background: #1d3557; color: #fff; border-radius: 8px; cursor: pointer; }
button.ghost { background: #fff; color: #1d3557; }
button:disabled { opacity: .4; cursor: default; }
.legend { font-size: .8rem; color: #666; display: flex; gap: .9rem; flex-wrap: wrap; align-items: center; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 3px; }
.spread-dot { background: #1d3557; }
.entry-dot { background: #e63946; }
.exit-dot { background: #2a9d8f; }
// Code not found

Click Enter trade when the z-score crosses the trigger threshold (the dashed lines). The demo opens a short on the expensive leg and a long on the cheap one. Click Exit trade to close the position. Watch how waiting for the spread to revert earns a profit — but entering too late, after the spread has already reversed, locks in a loss. The Step button advances one period at a time so you can see the logic play out tick by tick.

The Real Math

The spread St=PtAβPtBS_t = P_t^A - \beta P_t^B is estimated by regressing asset A on asset B to find the hedge ratio β\beta. If the residuals of that regression are stationary — they have a finite, bounded variance over time — the pair is cointegrated.

Testing cointegration is the first hard step:

  • Engle–Granger (1987): regress one price on the other, then apply the Augmented Dickey–Fuller (ADF) test to the residuals. If the ADF rejects a unit root, the pair is cointegrated. Simple but only handles two variables.
  • Johansen test (1991): a multivariate generalization that can detect multiple cointegrating relationships and is more powerful in small samples.

Once you have a cointegrated spread, its dynamics are often modeled as an Ornstein–Uhlenbeck (OU) process:

dSt=κ(μSt)dt+σdWtdS_t = \kappa(\mu - S_t)\,dt + \sigma\,dW_t

where κ\kappa is the mean-reversion speed, μ\mu is the equilibrium level, σ\sigma is volatility, and WtW_t is a Wiener process. The half-life of mean reversion is ln(2)/κ\ln(2) / \kappa — the expected time for the spread to halve its deviation. A half-life of days is tradeable; a half-life of years is not.

The z-score used for signals is simply:

zt=Stμ^σ^z_t = \frac{S_t - \hat{\mu}}{\hat{\sigma}}

where μ^\hat{\mu} and σ^\hat{\sigma} are estimated on a rolling window. Common entry thresholds are zt>2|z_t| > 2; exits at zt<0.5|z_t| < 0.5.

The strategy can break: cointegration is not permanent. Corporate events, regulatory changes, or a fundamental shift in one firm can sever the relationship. A spread that widens without reverting is called a divergent trade — the most dangerous failure mode in pairs trading.

Where It Matters

The cointegration framework extends far beyond a single stock pair:

  • Statistical arbitrage at scale: hedge funds run thousands of pairs simultaneously, treating the strategy as a diversified portfolio of mean-reversion bets rather than a single trade.
  • Fixed-income relative value: sovereign bond spreads (e.g., Italian vs. German 10-year yields) exhibit cointegration-like behavior driven by shared monetary policy; traders go long the cheap bond and short the expensive one.
  • ETF arbitrage: an ETF and its underlying basket are nearly perfectly cointegrated by construction — any gap is quickly closed by authorized participants, but short-lived discrepancies are still traded.
  • Commodity pairs: crude oil grades (WTI vs. Brent), natural gas hubs, or agricultural spreads (corn vs. wheat) share supply-chain cointegration that drives basis trading.
  • Currency carry and triangular arbitrage: exchange rates linked by covered interest parity form cointegrated relationships that underpin carry strategies.

Beyond finance, the Ornstein–Uhlenbeck model appears in physics (Brownian motion with a restoring force), control engineering (mean-reverting feedback loops), and ecology (population sizes bounded by carrying capacity). Any system with a tendency to revert to a set point is, mathematically, exhibiting the same mean-reversion property that pairs traders exploit.

Conclusion

Two stocks that individually look like random walks can be secretly tethered by economic forces that neither alone reveals. Cointegration formalizes that tether; the Ornstein–Uhlenbeck model describes the spring; and the z-score is the ruler that tells you how stretched it is.

Pairs trading is, at its core, a bet on equilibrium — the idea that whatever pulls two related assets apart will eventually pull them back. When it works, it is elegant and market-neutral. When cointegration breaks, the spring snaps and losses can be severe.

The strategy sits at the intersection of statistics, time-series analysis, and market microstructure — a reminder that even in noisy, competitive markets, patient mathematical reasoning can find a signal worth trading. Like bayesian inference, it is about updating your belief in a relationship based on evidence, and acting on the gap between what is and what equilibrium says should be.

Share this article

Pick a channel — or use your device's native share sheet.

Comments

Loading comments...

https://www.kipuhub.com/en/article/pairs-trading-cointegration/Content licensed under CC BY-NC 4.0.