Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

Compute Cointegration using NsePy, Pandas Library

1 min read

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 the best Pair of Stocks (Pair Trading) to go long in one stock and short(Competitive peers) in 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

NSEpy is an open-source Python library for collecting historical data from the National Stock Exchange (NSE) of India. The library provides easy-to-use functions for accessing data related to stocks, indices, and derivatives traded on the NSE.

The library utilizes web scraping techniques to retrieve data from the NSE website and provide data in a pandas DataFrame format, which can be easily manipulated and analyzed using Python. The data can be retrieved for various time intervals, ranging from daily to monthly, yearly, or customized intervals.

Importing the Libraries, Fetching Data from NSE and Computing Cointegration


import numpy as np
import pandas as pd

import statsmodels
from statsmodels.tsa.stattools import coint


import matplotlib.pyplot as plt
import nsepy 
from datetime import date

S1 = nsepy.get_history(symbol = 'SBIN', 
                        start = date(2015,1,1), 
                        end = date(2015,10,10))

S2 = nsepy.get_history(symbol = 'ICICIBANK', 
                        start = date(2015,1,1), 
                        end = date(2015,10,10))

result = coint(S1[['Close']], S2[['Close']])
score = result[0]
pvalue = result[1]

Plot State Bank of India Dataframe

S1[['Close']].plot()


Plot ICICI Bank Dataframe

S2[['Close']].plot()

 

Calculate the p-value

score, pvalue, _ = coint(S1[['Close']], S2[['Close']])
pvalue

Output

0.0052518039905594

Calculate the Spread

diff_series= S2[['Close']] - S1[['Close']]
diff_series.plot()

Calculate and Plot the Z-score with +/- 2 SD levels

def zscore(series):
    return (series - series.mean()) / np.std(series)

zscore(diff_series).plot()
#plt.axhline(zscore(diff_series).mean(), color='black')
plt.axhline(1.0, color='red', linestyle='--')
plt.axhline(-1.0, color='green', linestyle='--')

Simple Strategy using Co-Integration

Go “Long” the spread whenever the z-score is below -1.0
Go “Short” the spread when the z-score is above 1.0
Exit positions when the z-score approaches zero

Since we originally defined the “spread” as S1-S2, “Long” the spread would mean “Buy 1 share of S1, and Sell Short 1 share of S2” (and vice versa if you were going “Short” the spread)

Sample IPython Notebook to compute Cointegration below :

Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

How to Place Orders Concurrently using ThreadPoolExecutor – Python…

Creating concurrent orders is essential for active traders, especially those handling large funds, as it allows for executing multiple trade orders simultaneously, thereby maximizing...
Rajandran R
2 min read

Host your Python Flask Web Application using pyngrok and…

Ngrok offers several significant advantages for developers, especially when it comes to testing applications or hosting machine learning models. Ngrok allows you to expose...
Rajandran R
1 min read

20 Replies to “Compute Cointegration using NsePy, Pandas Library”

  1. 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

  2. I am using nsepy but it is not working to fetch indices data to manipulate for our reqirement.
    what i have to do.

  3. 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

    1. 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’)

  4. 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

  5. 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)

  6. 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&quot;
    csvfromurl = req.get(url).content
    csvfordf = pd.read_csv(strio.StringIO(csvfromurl.decode('utf-8')))
    nifty50df = pd.DataFrame(csvfordf)

  7. #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)

  8. Is there anyone who is still using NSEpy ?

    I m newbie with python but willing to learn.

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More