Exploring pair trading opportunities with yahoo, python and LibreOffice

I have been reading two books about quantative and algorithmic trading by Ernie Chan:
  • Quantitative Trading: How to Build Your Own...
  • Algorithmic Trading: Winning Strategies and...
See the books page for my reading recommendations.

One of the main take aways of this reading is the insight that it is possible to create stationary pairs by shorting and longing two, or more equities that usually move in tandem. The idea is to arbitrage the oscillating differences in moves between the equities in the pairs. Mr Chan explains all the mathematics behind mean reversion and provides Matlab code do the necessary calculus.

My approach to exploring this idea and the possibilities is far simpler and less elegant. Still, I think some might find it useful to see my take on it. I wrote a python script that pulls daily data from Yahoo and then calculates the ratios between two equities. With the resulting ratios data I continued my investigations in LibreOffice Calc. I like to use Calc to model my ideas and as they become more complicated or as I want to test more data sets I move the model over to code using Python and the pandas library.

But first I visually inspect the data by simply looking at Yahoo interactive charts using the compare feature:

Main US indexes; Source: Yahoo Finance

Main EU indexes; Source: Yahoo Finance
I focus on indexes as the open and close data on these are pretty reliable and because indexes will be easy to short using CFDs when I will actually start trading a strategy that might come out of this. As expected the indexes move more or less together, but there are also periods of divergence and convergence between the indexes. And it is this divergence and convergence that might offer trade opportunities independently of the overall market direction. On above charts the indexes are all moving up, but the idea is that we could use the same approach when markets are falling, like in 2008.
Main US indexes dropped 37,5% in 2008; Source: Yahoo Finance
With this approach we only care about the relative strengths and weaknesses of the indexes.

To get a feel for how to trade this and for how profitable this approach can be, I did some back tests on index pairs over one year of data YTD. All charts show the back test result as ROI / Return. The ratios have been normalized, so I can inspect how the strategy behaved lined up with the ratios of the pair. The charts also contain linear regression lines and mean lines of the ratios.

CAC - DAX 2013
This pair of the French CAC 40 and the German Dax seems to have been the most co-integrated of the EU index pairs. The linear regression of the ratios has almost no slope and is lining up with the mean.
DAX - AEX 2013
The normalized ratios of the German vs Dutch index is sloping up which seems to me to be in line with the underlying fundamentals. That is, that the German economy has been outperforming the Dutch the last year.
The Russell 2000 and the Nasdaq showed the best co-integration of all tests. The back test result was also most consistent of all the test performed.
SP500 - NASDAQ 2013
The S&P has been losing ground to the Nasdaq this year, hence the down slope of the linear regression on the normalized ratios.

The rules of the strategy I tested are simple. If the daily difference of the ratio in a pair A/B is minus x percent I long A and short B the next day. If the difference is plus x percent then I short A and long B the next day. I close both positions at the end / close of the next day. All graphs of the pairs tested are with the same x percent. Changing the percentage does change performance, but does not kill profitability.

Finally I did one out of sample back test on Rut - Nasdaq over 2008.

Rut - Nasdaq 2008

Although the return curve is very volatile, the final result is still positive.

I have good hopes this strategy will work out, but more testing is needed before I will consider trading this.