Kalman Pairs Trading

Statistical-arbitrage on a cointegrated pair of assets, with a time-varying hedge ratio estimated by a Kalman filter on the state-space form of the spread. The method returns a long/short position in the selected pair, signed by a trailing z-score of the filtered spread.

Overview

Pairs trading exploits cointegration: two non-stationary price series can be combined linearly into a stationary, mean-reverting spread. The classical Engle-Granger (1987) framework treats the cointegration coefficient as constant, but in practice the relationship between two stocks - especially in emerging markets and across sector regimes - drifts slowly over time. A constant-hedge approach therefore generates large estimation error in the spread, which manifests as systematic losses in stress periods.

The Kalman-filter approach treats the hedge ratio as an unobserved time-varying state and updates it recursively as new prices arrive. Vidyamurthy (2004), Elliott, van der Hoek and Malcolm (2005), and a long line of subsequent work formalise this state-space view. Gatev, Goetzmann and Rouwenhorst (2006) and Krauss (2017) survey the practical evidence. Palomar (2025) Chapter 15, Section 15.6 gives a textbook treatment.

FolioLab searches every pair in the submitted universe for cointegration, fits a Kalman filter to the winning pair to estimate its time-varying intercept and hedge ratio, and returns a long/short weight vector for the two legs of that pair. The entry threshold is fixed at . There is no exit band, and neither value can be set on the request.

The method returns weights, not trade signals. It emits a weight on each leg plus a diagnostic field signal_state, which reads active when the current z-score is at or beyond the entry threshold and watch when it is inside. The weights are returned either way: a watch result is still a position, not a flat book.

This method is absent from the website method picker. It is reachable through the API and through MCP. It is the only long/short method on the platform, which is why it is not offered beside the long-only methods in the web interface.

Mathematical Formulation

State-space model

is the price (or log-price) of the dependent leg of the pair, is the independent leg, and the latent state vector holds the time-varying intercept and hedge ratio. The state evolves as a random walk; the magnitude of controls how fast the hedge ratio is allowed to drift.

Kalman recursions

For each new observation, the filter performs a predict step and an update step. With state estimate and state covariance :

Here . The innovation is the one-step-ahead spread. The filtered spread series is not the innovation: it is the residual after the update, . The predictive variance is used inside the update step only: it does not reach the signal rule below.

Signal rule

The z-score is not the standardised innovation. It is a trailing sample z-score over the last 126 filtered spread values , using that window's own mean and sample standard deviation:

The predictive variance plays no part in this. Where a shorter history is available, the window is the whole filtered series.

The sign of sets the direction: a positive z-score gives a short position in the dependent leg and a long position in the independent leg, and a negative z-score reverses both. The magnitude sets signal_state only: active when , and watch below it. The weights are returned in both cases, scaled so the gross exposure is normalised. There is no exit band: this method returns a position, and it does not track an open trade across runs.

Choosing the pair

Pair selection precedes the Kalman step. Standard tests are Engle-Granger cointegration (regression of one log-price on the other and an ADF test on the residuals; see Dickey and Fuller, 1979) and Johansen's system test for cointegration. On Indian equities, sector-pair candidates are typically the two majors in a duopoly (HDFC Bank / ICICI Bank, Reliance / ONGC for energy, Maruti / M&M for autos) or the two most liquid names in a tightly cointegrated industrial cluster.

Half-life of mean reversion (Ornstein-Uhlenbeck or AR(1) framework; see Lo and MacKinlay, 1988) is the diagnostic that tells you whether a pair is tradeable: a half-life of a few days to a few weeks is workable; multi-month half-lives are too slow to support a stat-arb book.

On this platform the search uses the Engle-Granger test on log prices; the Johansen test is not used. Each unordered pair is one hypothesis, so the method tests both orderings and keeps the one with the smaller p-value. A universe of assets therefore holds tests, and at 5% each, a universe with no cointegration at all would still show a qualifying pair most of the time. The method controls the family-wise error instead: it applies the Holm (1979) step-down adjustment over all pairs, including pairs whose fit failed, so that the chance of selecting any pair by luck alone stays at 5%. A pair qualifies when its adjusted p-value is at most 0.05 and its estimated half-life is between 1 and 126 trading days, or cannot be estimated. Among qualifying pairs the method takes the lowest adjusted p-value, then the lowest raw p-value, then the shortest half-life, then the largest absolute z-score. The result records pairs_tested, the raw and the adjusted p-value, and the adjustment. If no pair qualifies, the method does not run, and on a universe with no real relation that is the expected result. It needs at least two assets, at least 120 price rows, and strictly positive prices.

The filter starts from the OLS intercept and hedge ratio of the selected pair. is fixed at , and is the variance of the OLS residuals. The dependent leg gets a weight of plus or minus one and the independent leg the opposite sign times the filtered hedge ratio, and the two are scaled so that the gross exposure is one. The net exposure is generally not zero.

Advantages & Limitations

Advantages

  • Time-varying hedge: Captures slow drift in the cointegration coefficient.
  • Trailing standardisation: The z-score uses the last 126 filtered spread values, so it follows changes in spread dispersion.
  • Offsetting legs: The long and short legs offset much of the exposure the two names share.
  • Computationally cheap: Kalman recursions are O(1) per step.

Limitations

  • Cointegration breakdowns: Structural breaks invalidate the spread.
  • Fixed Q and R: Drift speed and observation noise are set by rule, not calibrated to the pair.
  • No constraints: The positions are long and short, and the constraint surface is long-only, so a run that carries constraints does not run this method.
  • Single-pair scope: Doesn't scale natively to multi-asset stat-arb baskets.
  • Borrow constraints: Indian short-selling has frictions that erode the strategy edge.

References

  • Engle, R. F., & Granger, C. W. J. (1987). "Co-integration and Error Correction: Representation, Estimation, and Testing." Econometrica, 55(2), 251-276.
  • Kalman, R. E. (1960). "A New Approach to Linear Filtering and Prediction Problems." Journal of Basic Engineering, 82(1), 35-45.
  • Gatev, E., Goetzmann, W. N., & Rouwenhorst, K. G. (2006). "Pairs Trading: Performance of a Relative-Value Arbitrage Rule." The Review of Financial Studies, 19(3), 797-827.
  • Vidyamurthy, G. (2004). Pairs Trading: Quantitative Methods and Analysis. John Wiley & Sons.
  • Elliott, R. J., Van Der Hoek, J., & Malcolm, W. P. (2005). "Pairs Trading." Quantitative Finance, 5(3), 271-276.
  • Krauss, C. (2017). "Statistical Arbitrage Pairs Trading Strategies: Review and Outlook." Journal of Economic Surveys, 31(2), 513-545.
  • Dickey, D. A., & Fuller, W. A. (1979). "Distribution of the Estimators for Autoregressive Time Series with a Unit Root." Journal of the American Statistical Association, 74(366a), 427-431.
  • Holm, S. (1979). "A Simple Sequentially Rejective Multiple Test Procedure." Scandinavian Journal of Statistics, 6(2), 65-70.
  • Lo, A. W., & MacKinlay, A. C. (1988). "Stock Market Prices Do Not Follow Random Walks." The Review of Financial Studies, 1(1), 41-66.
  • Palomar, D. P. (2025). Portfolio Optimization: Theory and Application. Cambridge University Press, Chapter 15 (Pairs Trading Portfolios), Section 15.6 (Kalman Filtering for Pairs Trading).

Not investment advice. Past performance is not indicative of future results.