Skip to main content

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.
RUT - NASDAQ 2013
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.



Popular posts from this blog

Invest like a trader

Here is my book: "Invest like a trader". I finally decided to create this book based on my blog and experience investing, trading and coding. It is an introduction to my view on investing. For the next few days you can get a free copy on Amazon: https://amzn.to/2JjwQqj

This book is for you if you are an investor looking to learn a more trading-like approach to investing. Buy and hold investing has become buy and pray investing, with countless sleepless nights. A more trading-like approach to investing puts you back into control of your investments and it can be a lot of fun. After reading this book you should be more able to build and grow your investment account consistently.


Link to the e-book: https://amzn.to/2JjwQqj
Let me know what you think after reading the book. You can contact me via email or on social media. Even better, you can leave a review on Amazon.

Why I am a trader and investor and why you might wanna be one too

I am a coder by day and trader by night! Coding is my day job. I am fully aware that only having a job will not allow me to provide for my family and myself consistently in the long run. This is because most of the western societies are setup in a way to be more and more disadvantageous towards the working middle class. I do not need to be a billionaire to be happy, but our society is becoming more and more binary. It seems that one can either be rich or poor, in which case I opt to be rich, or at least hedge myself against being poor. Trading and investing are my hedge against this trend.

The super rich and us Below are two videos from the BBC documentary "The Super Rich and Us". It clearly illustrates and explains the polarization of wealth distribution in our current society. Here is a catchy quote from the documentary:

There are the haves, the haves not and the haves yachts! But actually we seem to be heading for a society where there are only haves not and haves yachts

Why I buy Gold and Silver

I own crypto currencies, because I have come to the conclusion that money as we know it today is no longer sustainable. I have come to this conclusion during the crisis of 2008. At that time I had the intuition / the gut feeling something was wrong not only with our financial system, but more specifically with our money. It took a few years of researching and investigating until I figured it out. The thing is, part of me did not want to believe it was true. And for a long time I did not act upon the intuition and knowledge I had. That changed last year when I decided to put my money where my mouth is and I started to accumulate crypto currencies. Cryptos align perfectly with my expertises: coding and trading. So I went for it... And with success.

Now I want to diversify my holdings more by adding Gold and Silver to it. I buy Gold and Silver as a hedge or insurance against the undwindled money printing of all of the governments / central banks of the world. Money, or actually, currenc…