# pairs trading

This post summarises the key lessons of the academic literature that has been published on pairs trading. The key themes are highlighted at the end of the page. Pair Trading Literature Review Gatev, Goetzmann, Rouwenhorst - "Pairs Trading: Performance of a Relative Value Arbitrage Strategy" https://papers.ssrn.com/sol3/papers.cfm?abstract_id=141615 This is the first meaningful academic paper on pair trading They use daily closing prices from 1962 to 2002 (in the original paper) They decide which pairs to trade in in a 12 month formation period, and then trade them for the next 6 months The selection criteria are to pick those with whose normalised prices (normalised to 1 at the start of the formation period) have the minimum squared distance between them The trading algorithm itself is similar to the one we use They see significant excess returns in the first part of the sample but see significant decay in recent years. Elliot, Hoek & Malcom - "Pairs Trading" http://stat.wharton.upenn.edu/~steele/Courses/434/434Context/PairsTrading/PairsTradingQFin05.pdf The spread is modelled as an Ornstein-Uhlenbeck process This allows forecast time to convergence (half-life) to be estimated The paper doesn't show any...

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,...

In our previous post, we looked into implementing a Kalman filter in R for calculating the hedge ratio in a pairs trading strategy. You know, light reading... We saw that while R makes it easy to implement a relatively advanced algorithm like the Kalman filter, there are drawbacks to using it as a backtesting tool. Setting up anything more advanced than the simplest possible vectorised backtesting framework is tough going and error-prone. Plus, it certainly isn't simple to experiment with strategy design - for instance, incorporating costs, trading at multiple levels, using a timed exit, or incorporating other trade filters. To be fair, there are good native R backtesting solutions, such as Quantstrat. But in my experience none of them let you experiment as efficiently as the Zorro platform. And as an independent trader, the ability to move fast - writing proof of concept backtests, invalidating bad ideas, exploring good ones in detail, and ultimately moving to production efficiently - is quite literally a superpower. I've already invalidated 3 ideas since starting this post The downside with Zorro is that...

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...

Some price series are mean reverting some of the time, but it is also possible to create portfolios which are specifically constructed to have mean-reverting properties. Series that can be combined to create stationary portfolios are called cointegrating, and there are a bunch of statistical tests for this property. We'll return to these shortly. While you can, in theory, create mean reverting portfolios from as many instruments as you like, this post will largely focus on the simplest case: pairs trading. What is Pairs Trading? Pairs trading involves buying and selling a portfolio consisting of two instruments. The instruments are linked in some way, for example they might be stocks from the same business sector, currencies exposed to similar laws of supply and demand, or other instruments exposed to the same or similar risk factors. We are typically long one instrument and short the other, making a bet that the value of this long-short portfolio (the spread) has deviated from its equilibrium value and will revert back towards that value. One of the major attractions of pairs trading is that...