Kalman filter

Posted on Oct 16, 2019 by Kris Longmore

In the first three posts of this mini-series on pairs trading with Zorro and R, we: Implemented a Kalman filter in R Implemented a simple pairs trading algorithm in Zorro Connected Zorro and R and exchanged data between the two platforms In this fourth and final post, we're going to put it all together and develop a pairs trading script that uses Zorro for all the simulation aspects (data handling, position tracking, performance reporting and the like) and our Kalman implementation for updating the hedge ratio in real-time. You can download the exact script used in this post for free down at the very bottom. Let's go! Step 1: Encapsulate our Kalman routine in a function Encapsulating our Kalman routine in a function makes it easy to call from our Zorro script - it reduces the call to a single line of code. Save the following R script, which implements the iterative Kalman operations using data sent from Zorro, in your Zorro strategy folder: ###### KALMAN FILTER ####### delta <- 0.0001 Vw <- delta/(1-delta)*diag(2) Ve <- 0.01 R <- matrix(rep(0,...

Posted on Sep 19, 2019 by Kris Longmore

This Kalman Filter Example post is the first in a series where we deploy the Kalman Filter in pairs trading. Be sure to follow our progress in Part 2: Pairs Trading in Zorro, and Part 3: Putting It All Together. Anyone who's tried pairs trading will tell you that real financial series don't exhibit truly stable, cointegrating relationships. If they did, pairs trading would be the easiest game in town. But the reality is that relationships are constantly evolving and changing. At some point, we're forced to make uncertain decisions about how best to capture those changes. One way to incorporate both uncertainty and dynamism in our decisions is to use the Kalman filter for parameter estimation. The Kalman filter is a state space model for estimating an unknown ('hidden') variable using observations of related variables and models of those relationships. The Kalman filter is underpinned by Bayesian probability theory and enables an estimate of the hidden variable in the presence of noise. There are plenty of tutorials online that describe the mathematics of the Kalman filter, so I won't...