Skip to content
All library documents

Kalman Filter Pairs Trading with a Dynamic Hedge Ratio

Article Robot Wealth

Summary

This tutorial combines a Kalman filter written in R with a simple pairs trading system in Zorro. The filter updates a hedge ratio as new prices arrive, estimates the spread prediction error, and calculates its uncertainty. The trading logic uses that uncertainty to set entry levels, then takes opposing positions in the two assets when the error crosses those levels. The example pair is GDX and GLD, with historical prices loaded from an external data source.

The article also describes connecting Zorro and R, plotting the evolving hedge ratio and spread error, and reproducing earlier results before iterating on trade design. Suggested experiments include exiting when the error returns toward zero, imposing a maximum holding period, using multiple entry thresholds, and changing their spacing. The document supplies implementation details but provides no numerical performance evidence in the excerpt. Its example depends on the chosen pair, data, cost assumptions, and execution logic; results from a historical reproduction do not establish that the strategy will remain profitable.

Key ideas

  • A Kalman filter can update a pairs hedge ratio as each new price observation arrives.
  • The spread’s prediction error and estimated uncertainty can be used to define entry thresholds.
  • The example trades opposite positions in GDX and GLD when the error crosses specified levels.
  • Possible design variations include mean reversion exits, holding-time limits, and multiple entry thresholds.
  • Pair selection, transaction costs, and implementation choices limit how broadly the example’s results can be applied.

Tags

Cited by

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