Here is a simple example to compute Cointegration between two stock pairs using python libraries like NSEpy, Pandas, statmodels, matplotlib
Cointegration is used in Statistical Arbitrage to find best Pair of Stocks (Pair Trading) to go long in one stock and short(Competitive peers) another to generate returns. Statistical Arbitrage(StatArb) is all about mean reversion, looking for deviation in the spreads and expecting mean reversion from the spread.
NSEpy – fetches historical data from nseindia.com
Pandas – Python library to handle time series data
Statmodels – Python library to handle statistical operations like cointegration
Matplotlib – Python library to handle 2D chart plotting
We will be using get_history NSEpy function to fetch the index data from nseindia. However to fetch stock data you need to use get_price_history. Exploring the NSEpy library would give you a broader idea about how to replicate the same for stocks. But the problem with NSEIndia data is that stock data is not adjusted to split/bonus. Will handle that in a different post about how to process the data for split/bonus before analyzing the time series data.
Sample IPython Notebook to compute Cointegration below :
Quantopian Lecture on Pair Trading
Python Library to get publicly available data on NSE website – NSEpy