Exploring pair trading opportunities with yahoo, python and LibreOffice
- Quantitative Trading: How to Build Your Own...
- Algorithmic Trading: Winning Strategies and...
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|
|Main US indexes dropped 37,5% in 2008; Source: Yahoo Finance|
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|
|DAX - AEX 2013|
|RUT - NASDAQ 2013|
|SP500 - NASDAQ 2013|
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.