Pairs Trading with the Kalman Filter: Dynamic Hedge Ratio
Pairs trading, also called statistical arbitrage, exploits the mean reversion of the spread between two cointegrated instruments: when the spread deviates far from its mean, buy the cheap leg, sell the expensive leg, and close the position when the spread reverts. The critical ingredient is the hedge ratio \( \beta \), i.e. how many units of the second asset offset one unit of the first. In practice \( \beta \) drifts over time. A Kalman filter estimates it online, without the window length that a rolling regression requires, and as a by-product delivers a statistically normalized trading signal.
This page explains an estimation technique. It is not investment advice; transaction costs, borrowing costs, and the risk that cointegration breaks down are not considered.
Why not a rolling regression?
The classical approach regresses the price \( p_1 \) on \( p_2 \) over a fixed window, \( p_1 = \beta p_2 + \alpha \). The window length is a hard trade-off: a short window reacts quickly but is noisy, a long window is smooth but lags. All observations inside the window are weighted equally, and the estimate jumps when old data leaves the window. The Kalman filter replaces the window by an explicit model of how fast \( \beta \) may change, weighs new data according to its information content, and quantifies its own uncertainty.
State-space model
The hedge ratio and the intercept form the state \( \mathbf{x}(k) = [\beta(k), \alpha(k)]^T \). Without better knowledge, both follow a random walk:
\[ \mathbf{x}(k) = \mathbf{x}(k-1) + \mathbf{z}(k-1), \qquad \mathbf{z} \sim \mathcal{N}(\mathbf{0}, \mathbf{Q}) \]
The price of the first asset is the measurement, with the price of the second asset entering the time-varying measurement matrix:
\[ p_1(k) = \underbrace{\begin{bmatrix} p_2(k) & 1 \end{bmatrix}}_{\mathbf{C}(k)} \mathbf{x}(k) + v(k), \qquad v \sim \mathcal{N}(0, R) \]
This is a linear Kalman filter with \( \mathbf{A}_d = \mathbf{I} \) and a measurement matrix that changes every step. Since the measurement is a scalar, no matrix inversion is needed.
The innovation is the trading signal
The innovation
\[ \nu(k) = p_1(k) - \mathbf{C}(k)\mathbf{x}(k\vert k-1) = p_1(k) - \beta(k\vert k-1)\,p_2(k) - \alpha(k\vert k-1) \]
is the one-step-ahead forecast error of the spread, and its variance \( S(k) = \mathbf{C}(k)\mathbf{P}(k\vert k-1)\mathbf{C}(k)^T + R \) comes for free from the filter. The ratio \( \nu(k)/\sqrt{S(k)} \) is therefore a z-score that is automatically normalized by the current uncertainty, which makes it a natural trading signal:
- \( \nu(k) < -\sqrt{S(k)} \): the spread is unusually low → long the spread: buy \( p_1 \), sell \( \beta \) units of \( p_2 \).
- \( \nu(k) > +\sqrt{S(k)} \): the spread is unusually high → short the spread: sell \( p_1 \), buy \( \beta \) units of \( p_2 \).
- Exit when the z-score crosses zero, i.e. the spread is back at its estimated mean.
The entry threshold (here one standard deviation) is a strategy parameter. Note that the squared z-score is exactly the NIS, so the same quantity also tells you whether the filter is consistent.
Avoiding look-ahead bias
Trade only on information available at time \( k \). The signal at \( k \) uses the hedge ratio predicted before \( p_1(k) \) was observed, so the innovation-based signal is free of look-ahead bias by construction. Use aligned prices of both instruments (log prices are a common alternative), and update the filter once per bar.
Tuning Q and R
The ratio of \( \mathbf{Q} \) to \( R \) determines how fast the hedge ratio adapts, exactly like the window length of a rolling regression, but continuously:
- \( \mathbf{Q} = \frac{\delta}{1-\delta}\mathbf{I} \) with a small \( \delta \) (for example \( 10^{-5} \) to \( 10^{-3} \)) is a common parametrization. Larger \( \delta \) → \( \beta \) adapts faster but becomes noisier, and real deviations of the spread are absorbed into \( \beta \) instead of producing signals.
- \( R \) is the variance of the spread around its mean. It can be initialized from the residual variance of an initial static regression.
- Instead of guessing, \( \mathbf{Q} \) and \( R \) can be estimated by maximum likelihood or with the EM algorithm [1]. Check the result with the NIS: the time average of \( \nu^2/S \) should be close to one.
General tuning guidance is on the page how to tune a Kalman filter.
Pitfalls
- Test for cointegration first (for example Engle-Granger or Johansen). The Kalman filter will always produce a hedge ratio, even for two unrelated random walks.
- Cointegration can break after structural changes. A sudden, persistent increase of the NIS is a warning sign.
- Too much adaptivity kills the strategy. If \( \beta \) follows every move, the spread is always “at the mean” and no signals occur.
- Costs matter. Frequent re-hedging as \( \beta \) changes generates transaction costs that are not part of the model.
References
[1] Shumway, Robert and David S. Stoffer. “AN APPROACH TO TIME SERIES SMOOTHING AND FORECASTING USING THE EM ALGORITHM.” Journal of Time Series Analysis 3 (1982): 253-264.
[2] MT4: https://sites.google.com/site/marketformula/mt4-indicators/pairs-trading-mt4-indicator
[3] Triangular Arbitrage: https://sites.google.com/site/marketformula/articles/triangular-arbitrage-101?authuser=0
[4] Synthetic hedge: https://www.forexfactory.com/thread/262827-synthetic-hedges-cointegration-mean-reversion-and-similar-stuff
[5] Z. Zhao and D. P. Palomar, “Mean-Reverting Portfolio With Budget Constraint,” in IEEE Transactions on Signal Processing, vol. 66, no. 9, pp. 2342-2357, 1 May1, 2018, doi: 10.1109/TSP.2018.2799193. https://arxiv.org/pdf/1701.05016.pdf
[6] Vidyamurthy G. Pairs Trading : Quantitative Methods and Analysis. Hoboken N.J: J. Wiley; 2004.
[7] Tim Leung and Xin Li, Optimal Mean Reversion Trading: Mathematical Analysis and Practical Applications. WSPC. 2015
[8] Spectral factorization: https://www.youtube.com/watch?v=HXeT5WexXPs Poor88
[9] Ornstein-Uhlenbeck process in pairs trading: https://medium.com/@financialnoob/pairs-trading-with-ornstein-uhlenbeck-process-part-1-cf7f9b532f5d
[10] https://medium.com/analytics-vidhya/understanding-and-implementing-kalman-filter-in-python-for-pairs-trading-9b8986d79b2d
[11] Kalman Filter Trading Applications: https://www.youtube.com/watch?v=hmKwjuZLpqg