13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 28

Kalman Filter-Based Pairs Trading

using QSTrader

In previous chapters of the book we considered the mathematical underpinnings of State Space

Models and Kalman Filters, as well as the application of the PyKalman library to a pair of ETFs

to dynamically adjust a hedge ratio as a basis for a mean reverting trading strategy.

In this chapter we will discuss a trading strategy originally due to Ernest Chan[32] and tested

by Aidan O’Mahony over at Quantopian[73]. We will make use of the QSTrader backtesting

framework in order to provide a new implementation of the strategy. QSTrader will carry out

the "heavy lifting" of the position tracking, portfolio handling and data ingestion, while we

concentrate here solely on the code that generates the trading signals.

28.1 The Trading Strategy

The pairs-trading strategy is applied to a couple of Exchange Traded Funds (ETF) that both

track the performance of varying duration US Treasury bonds. They are:

• TLT - iShares 20+ Year Treasury Bond ETF

• IEI - iShares 3-7 Year Treasury Bond ETF

The goal is to build a mean-reverting strategy from this pair of ETFs.

The synthetic "spread" between TLT and IEI is the time series that we are actually interested

in longing or shorting. The Kalman Filter is used to dynamically track the hedging ratio between

the two in order to keep the spread stationary.

To create the trading rules it is necessary to determine when the spread has moved too far

from its expected value. How do we determine what "too far" is? We could utilise a set of

fixed absolute values, but these would have to be empirically determined. This would introduce

another free parameter into the system that would require optimisation (and additional danger

of overfitting).

One approach to creating these values is to consider a multiple of the standard deviation of

the spread (as in the previous chapter) and use these as the bounds. For simplicity we can set

the coefficient of the multiple to be equal to one.

401

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!