Implementing pair trading strategy with python

This series explains how you can implement a pair trading system with Python. We will use jugaad-data for extracting data and pandas for analysis. This implementation is based on Zerodha’s varsity article. I will keep reffering back to the varsity article so I can’t stress enough how important it is to read that article is to understand the python implementation discussed here.

I’m summarizing the process in these easy steps, you can click on the each step to follow the code.

Identify correlated pairs of stocks

Calculate ratio spread, standard deviation and cumulative density (cdf)

Test the strategy

Varsity article on which this implementation is based, it discusses a lot of options but ultimately chooses just one for approach and even this implementation will follow the same approach, so please keep in mind that there are multiple ways to approach this problem. Once you understand the process, I encourage you to play with different steps and see the results.

Also if you find any mistakes in the implementation, do report them on the github issues page.

Calculate pair stats

Calculate mean, standard deviation and cdf of the ratio for the pair

2020-08-25

Identify correlated stocks from F&O stock list

Download data for all the stocks traded in F&O, calculate the correlation matrix on close price and finally filter stocks with high correlation

2020-08-25

Test the strategy

We take the pair stats calculated in last section and make trading decisions based on these stats.

2020-08-25