Implementing a Simple Mean Reverting Pairs Trading Algorithm in the Quantconnect platform (Part 1)
Learn how to implement and backtest a Mean Reverting strategy from the book “Algorithmic Trading: Winning Strategies and Their Rationale” using the Quantconnect framework.
Update
For information about the course Introduction to Python for Scientists (available on YouTube) and other articles like this, please visit my website cordmaur.carrd.co.
Introduction
Hi! In my last story “Understanding and Implementing Kalman Filter for Pairs Trading” [1] I’ve used an example from the book Algorithmic Trading: Winning Strategies and Their Rationale [2] to illustrate the use of a Pairs Trading strategy using the Kalman Filter with the EWA (Australia’s ETF) and EWC (Canada’s ETF) pair. However, in the mentioned post, I’ve focused on the hedge ratio (relation between the two assets) and didn’t show the strategy’s performance, and that was intentional. In the Algorithmic Trading book, Ernest Chan presents an outstanding 26,2% of annualized return (APR) and a Sharpe ratio of 2.4 for this strategy from 2006 to 2012 (Figure 1).

Too good to be true?
I have implemented the Chan’s strategy in Python (the original code is in MatLab) and achieved similar results. However (remember that “the devil is in the details”), when we take a closer look at the backtest, we can notice a number of assumptions that don’t seem very realistic in a live trading environment. For example, no transaction costs or slippage are modeled, it considers that the portfolio is fully invested throughout the test, etc. Additionally, the parameters have been inferred using the same data from the backtest, incurring in the so-called look-ahead bias. Chan explains that these omissions were intended to keep source codes simpler to understand but he also raises a warning:
“I urge readers to undertake the arduous task of cleaning up such pitfalls when implementing their own backtests of these prototype strategies.”
With that in mind, I’ve decided to backtest these strategies as close as possible to reality, and that’s where Quantconnect comes in.
The Quantconnect framework
There are some ready-to-use packages to backtest a trading strategy in Python. Two good examples are Zipline and Backtrader but there are posts listing many others. Some quants even write their own code for it. The problem is that these approaches share the same shortcoming: getting good quality data.
Good quality historical data is usually charged and normally free data is provided only in a daily basis. That’s where Quantconnect comes in handy.
Quantconnect (Figure 2) is an algorithm trading framework with tons of free data to be accessed by the trading algorithms with up to minute resolution. It has also connection to different brokers so it is simple to pass from backtest to live without code adaptation. Besides that, it models brokerage transactions costs and slippage to simulate how the orders would be executed in “real” life.

So, I’ve decided to accept Chan’s “arduous” task of cleaning the pitfalls, rewriting the strategies in the Quantconnect framework, and would like to share with you the results in this new series of posts.
Linear Mean Reversion Strategy
The first strategy we are going to implement is the linear mean reversion. It assumes a constant ratio between the assets, that can be derived from a linear regression and the resulting portfolio will use the linear coefficient as the asset’s weights. To make it more clear, I will review some of the basics of cointegration and use the Quantconnect’s research environment to access the historical data.
Cointegration
In a mean reversion strategy, we assume that our series is stationary (i.e. it returns to its mean value). The problem with this assumption is that usually the stocks (and indices, and ETFs, etc.) are not stationary or mean reverting. However if we can make a combination of assets (considering some weights) and this combination is mean reverting, we will be able to trade this “combination”, and that’s the idea behind the cointegration. To exemplify, let’s use a Jupyter Notebook in the Quantconnect research.
To access the research notebook, it is necessary to sign in to Quantconnect and create a new algorithm (in the Algorithm Lab tab). A file called research.ipynb is created by default.
The first step is to take a look at the raw data from the ETFs EWA (Australia) and EWC (Canada):












