25 May 2025 · Andreas Mitillos

Cointegration Pairs Trading Strategy

We implement a statistical arbitrage strategy using cointegration-based pairs trading. This project demonstrates the identification of cointegrated pairs, statistical testing, and backtesting through an interactive trading tool.

Pairs trading background

Pairs Trading Strategy.

Pairs trading is a statistical arbitrage strategy that exploits temporary deviations from the long-run equilibrium relationship between two cointegrated securities. The strategy involves:

  • Identifying cointegrated pairs: Two assets whose prices move together over time despite short-term divergences
  • Monitoring the spread: The difference between the prices after adjusting for the long-run relationship
  • Trading mean reversion: Going long the undervalued asset and short the overvalued asset when the spread deviates significantly

Cointegration & Testing.

Two time series PA,tP_{A,t} and PB,tP_{B,t} are cointegrated if there exists a linear combination that is stationary:

PA,t=α+βPB,t+ϵtP_{A,t} = \alpha + \beta P_{B,t} + \epsilon_t

where ϵt\epsilon_t is stationary. We use the Engle-Granger two-step method:

  • Step 1: Estimate the cointegrating regression via OLS to obtain residuals ϵ^t\hat{\epsilon}_t
  • Step 2: Test stationarity of residuals using the Augmented Dickey-Fuller (ADF) test

The ADF test examines the null hypothesis H0H_0: unit root (non-stationary) vs. H1H_1: stationary. Low p-values (< 0.05) suggest cointegration.

Trading Implementation.

The trading strategy uses rolling statistics to adapt to changing market conditions:

Rolling Regression

Over a rolling window of length ww, we estimate:

PA,t=αt+βtPB,t+ϵtP_{A,t} = \alpha_t + \beta_t P_{B,t} + \epsilon_t

Spread Calculation

The spread at time tt is:

Spreadt=PA,t(α^t+β^tPB,t)\text{Spread}_t = P_{A,t} - (\hat{\alpha}_t + \hat{\beta}_t P_{B,t})

Z-Score Normalisation

We normalize the spread using rolling statistics:

Zt=Spreadtμspread(t)σspread(t)Z_t = \frac{\text{Spread}_t - \mu_{\text{spread}}(t)}{\sigma_{\text{spread}}(t)}

Position Logic

Trading signals are generated based on Z-score thresholds:

  • Long Spread: When Ztentry thresholdZ_t \leq -\text{entry threshold} (buy A, sell B)
  • Short Spread: When Zt+entry thresholdZ_t \geq +\text{entry threshold} (sell A, buy B)
  • Exit: When Ztexit threshold|Z_t| \leq \text{exit threshold}

Trading Tool.

Use the interactive tool below to analyse cointegrated pairs or run backtests with your chosen parameters. You can select between analysis mode to test for cointegration or backtest mode to simulate the trading strategy.

Simulation Mode

© 2025Made by Andreas Mitillos