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 :**

**References**

**Quantopian Lecture** on Pair Trading

Python Library to get publicly available data on NSE website – NSEpy

Great post Rajandran

Is there any specific version of python that would you recommend. Else may i know the python version that you are trying this on.

Regards,

Surya

We are using Python 2.7

hello Rajandran

i want your support will u please give your email.id or phone no

do send a mail to rajandran@marketcalls.in

Happy to see my work in action, Thanks for using the library!!

Do let me know if any feedbacks!!

Please do try https://github.com/swapniljariwala/zerodha as well and let me know feedback

I am using nsepy but it is not working to fetch indices data to manipulate for our reqirement.

what i have to do.

What error you are facing?

now due to chane in indices symbol free downloaders(eod nse) can not download indices. data

how to download or extract some main indices data from indices snapshot csv data file quickly.

i need nifty, ,banknifty, nse it, nifty junior, midcap50, nse 100 etc

I’ve updated the API, The updated API will solve your problem

https://github.com/swapniljariwala/nsepy

Simple few lines of code and you are done

from nsepy import get_history, get_index_pe_history

from datetime import date

#Index price history

nifty = get_history(symbol=”NIFTY”,

start=date(2015,1,1),

end=date(2015,1,10),

index=True)

nifty[[‘Close’, ‘Turnover’]].plot(secondary_y=’Turnover’)

Hello all,

Please check out the latest version of NSEpy, I’ve added the support for

Derivative data. (Probably only API for indian derivative and India VIX data as Yahoo API has no support for derivatives), Index data

Unified and simplified API for all (Equity, Index, Derivative, Volatility Indexes-INDIAVIX)

Compatible and Tested with Python 2.7 and 3.4

Examples on git-hub page-

https://github.com/swapniljariwala/nsepy

Please report any bugs or issues on –

https://github.com/swapniljariwala/nsepy/issues

Sir, I beginner in Algo Trading trying to use my quant knowlegde. I am using NSEpy though getting error like “ValueError: negative dimensions are not allowed” and “AttributeError: ‘ThreadReturns’ object has no attribute ‘result’. Could you please help me. I would thankful to you. My code is mentioned below:

#importing modules

import pandas as pd

import numpy as np

import statsmodels

from statsmodels.tsa.stattools import coint

import matplotlib.pyplot as plt

import datetime as date

import nsepy

from nsepy.archives import get_price_history

import seaborn

#Defining Cointegrated pairs formula

def find_cointegrated_pairs(stockdata):

n = len(stockdata.minor_axis)

score_matrix = np.zeros((n, n))

pvalue_matrix = np.ones((n, n))

keys = stockdata.keys

pairs = []

for i in range(n):

for j in range(i+1, n):

S1 = stockdata.minor_xs(stockdata.minor_axis[i])

S2 = stockdata.minor_xs(stockdata.minor_axis[j])

result = coint(S1, S2)

score = result[0]

pvalue = result[1]

score_matrix[i, j] = score

pvalue_matrix[i, j] = pvalue

if pvalue = 0.99))

print (pairs)

I am getting same error, did you find the solution ?

if pvalue < 0.05:

pairs.append((stockdata.minor_axis[i], stockdata.minor_axis[j]))

return score_matrix, pvalue_matrix, pairs

#time period of testing

histdata = {}

startdate = date.datetime(2016,5,20)

enddate = date.datetime.today()

#Dowloading NSE Symbols

url = "https://www.nseindia.com/content/indices/ind_nifty50list.csv"

csvfromurl = req.get(url).content

csvfordf = pd.read_csv(strio.StringIO(csvfromurl.decode('utf-8')))

nifty50df = pd.DataFrame(csvfordf)

#fetching data

for eachscrip in nifty50df[‘Symbol’]:

try:

stockdata = get_price_history(stock = eachscrip, start = startdate, end = enddate)

except (ValueError,IOError) as err:

print(err)

#Arranging pairs combination

stockdata.minor_axis = map(lambda x: x.Symbol, stockdata.minor_axis)

Getting above two errors when fetching data

Is there anyone who is still using NSEpy ?

I m newbie with python but willing to learn.

Hi

How do i get live data using nsepy?

Hey hi. Can you help with adjusting historical prices with splits and bonuses

how to get live data using nsepy