Skip to content
All library documents

Kalman Filtering for Adaptive Pairs Trading in R

Article Robot Wealth

Summary

This tutorial builds an adaptive pairs trading example with gold and gold-mining ETF prices. A Kalman filter estimates a changing hedge ratio and intercept as new observations arrive. The prediction error is compared with its estimated standard deviation to create entry signals when the error crosses a threshold. The example then prevents repeated signals from opening overlapping trades, lags positions to avoid look-ahead bias, and calculates cumulative profit and loss from the two legs.

The article includes R code for loading prices, estimating filter states, generating signals, and simulating positions. It also demonstrates tighter thresholds as a more aggressive signal variant. The evidence is illustrative: plots show estimated coefficients, signals, and cumulative PnL, but the excerpt supplies no numerical performance assessment. The author flags important limitations: trades are assumed to occur at daily closing prices with no transaction costs, slippage, or market impact. The approach therefore illustrates implementation mechanics rather than establishing that the strategy is profitable in live trading.

Key ideas

  • A Kalman filter can update a pair’s hedge ratio and intercept as observations arrive.
  • The prediction error can be scaled by its estimated variance to define entry thresholds.
  • Lagging trade signals helps avoid using information unavailable when the position is taken.
  • A simple backtest can still be misleading when it omits costs, slippage, and market impact.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.