The powerful python package to read India stock data from NSE

I have created a new python package to help you read any publicly available data from NSE website including stock quotes, historical data, live indices etc.

By

What is it

This is a Python library that let's you download any publicly available data from NSE website, including stock quotes, historical data, live indices etc. In addition, it has some pretty handy functionalities, such as trading holiday detection, expiry dates calculations, Live data reading etc.

Installation

You can install this using PIP as follows:

pip install nse-data-reader

It may be a good idea to also install few of the following libraries as it will help us follow the demonstrated usage below.

pip install tables 
pip install sqlalchemy 
pip install matplotlib

Example Usage

Here are a few usecases for this package. 

Get Daily NIFTY OHLC data

To get NIFTY data, you will need to set the index parameter to true.

from datetime import date
from nsepy import get_history

# get all the data of NIFTY index from 01 Feb 2023 to 10 Feb 2023
nifty = get_history(symbol="NIFTY", start=date(2023,2,1), end=date(2023,2,10), 
                    index=True)

# save the data
nifty.to_csv("nifty-01Feb23-10Feb23.csv")

The data, thus obtained looks like this -

Date,       Open,    High,     Low,      Close,    Volume,    Turnover
2023-02-01, 17811.6, 17972.2,  17353.4,  17616.3,  512870802, 384861900000.0
2023-02-02, 17517.1, 17653.9,  17445.95, 17610.4,  490113567, 376399600000.0
2023-02-03, 17721.75,17870.3,  17584.2,  17854.05, 424123037, 345036700000.0
2023-02-06, 17818.55,17823.7,  17698.35, 17764.6,  282544790, 218648800000.0
2023-02-07, 17790.1, 17811.15, 17652.55, 17721.5,  354395693, 236110800000.00003
2023-02-08, 17750.3, 17898.7,  17744.15, 17871.7,  290994265, 237334000000.0
2023-02-09, 17885.5, 17916.9,  17779.8,  17893.45, 260854055, 215299700000.0
2023-02-10, 17847.55,17876.95, 17801.0,  17856.5,  231991834, 170639900000.00003

Get stock specific OHLC data

You can get EoD candle-stick data of any NSE listed stocks using the stock symbol as follows.

from datetime import date
from nsepy import get_history

# get all the data of state bank (SBIN) from 01 Feb 2023 to 10 Feb 2023
state_bank = get_history(symbol='SBIN', start=date(2023, 2, 1), end=date(2023, 2, 10))

# save the data to a csv file
state_bank.to_csv("sbin-feb.csv")

Get NIFTY Future OHLC data

To get NIFTY Futures data, you will need to set the futures parameter to true. You will also need to provide the expiry_date and the strike_price of the futures contract.

For example, below code will get the data of NIFTY February 18000 futures contract from 1st Feb 2023 to 10th Feb 2023 -

from datetime import date
from nsepy import get_history

# get all the data of NIFTY index from 01 Jan 2023 to 10 Jan 2023
future = get_history(symbol="NIFTY", start=date(2023,1,1), end=date(2023,1,10), 
                    index=True, futures=True, expiry_date=date(2023, 1, 25), strike_price=18000)

# save the data
future.to_csv("nifty-18000-jan-futures-01jan23-10jan23.csv")

Get NIFTY Option OHLC data

To get NIFTY Options data, you will need to set the option_type parameter to 'CE' or 'PE' ('CA' or 'PA' for American style options). Additionally, you will also need to provide the option's expiry_date and the strike_price of the option contract.

For example, below code will get the data of NIFTY's 18000 CALL options for the month of February from 1st Feb 2023 to 10th Feb 2023 -

from datetime import date
from nsepy import get_history

# get all the data of NIFTY index from 01 Jan 2023 to 10 Jan 2023
future = get_history(symbol="NIFTY", start=date(2023,2,1), end=date(2023,2,10), 
                    index=True, futures=False, option_type='CE', expiry_date=date(2023, 2, 23), strike_price=18000)

# save the data
future.to_csv("nifty-18000-feb-ce-01Feb23-10Feb23.csv")

The sample data looks like this

Date,      Symbol, Expiry,     Option Type, Strike Price, Open,  High,   Low,    Close,  Last,  Settle Price, Number of Contracts,Turnover,          Premium Turnover,Open Interest,Change in OI,Underlying
2023-02-01,NIFTY,  2023-02-23, CE,          18000.0,      220.0, 283.9,  100.2,  136.35, 131.0, 136.35,       190910,             173613336000.0,    1794336000.0,2765050,154000,17616.3
2023-02-02,NIFTY,  2023-02-23, CE,          18000.0,      119.95,142.75, 96.85,  114.55, 112.0, 114.55,       77046,              69789964000.0,     448564000.00000006,2904800,139750,17610.4
2023-02-03,NIFTY,  2023-02-23, CE,          18000.0,      131.1, 179.8,  92.0,   166.65, 150.0, 166.65,       105887,             95981525000.0,     683225000.0,2930150,25350,17854.05
2023-02-06,NIFTY,  2023-02-23, CE,          18000.0,      140.1, 147.65, 105.05, 117.0,  113.0, 117.0,        81645,              73961452000.0,     480952000.00000006,3266350,336200,17764.6
2023-02-07,NIFTY,  2023-02-23, CE,          18000.0,      129.15,134.65, 83.5,   96.75,  95.0,  96.75,        90064,              81525422000.0,     467822000.0,3521550,255200,17721.5
2023-02-08,NIFTY,  2023-02-23, CE,          18000.0,      98.0,  142.0,  95.95,  126.45, 123.1, 126.45,       123661,             112065422000.0,    770522000.0,3308550,-213000,17871.7
2023-02-09,NIFTY,  2023-02-23, CE,          18000.0,      125.0, 146.75, 90.5,   129.1,  134.6, 129.1,        122787,             111236568000.0,    728268000.0,3355650,47100,17893.45
2023-02-10,NIFTY,  2023-02-23, CE,          18000.0,      129.1, 129.1,  77.4,   89.2,   87.0,  89.2,         136399,             123361410000.00002,602310000.0,3669300,313650,17856.5

 

FAQ

Is this Legal to take this data from NSE?

The data obtained by this Python Library is already available publicly in NSE website. Anyone can visit the website and download this data. This code only provides a convenient mechanism to read the same data and store it in a pandas dataframe. It is completely legal.

Why did I create this Python Library separately where Nsepy already exists?

nse-data-reader is heavily inspired from nsepy - an excellent package originally created by Swapnil Jariwala. I do not know the author personally, but of late I noticed that the nsepy library is not being maintained properly in a timely manner.

I also noticed that there are many PRs with good features and bug fixes which have not been incorporated to the library by the original author.

Due to such uncertainties, I decided to fork my own copy of the nsepy library and maintain it separately.

I added all the bugfixes and some of the new features proposed by others and I improved the overall readability of the code.

How Can I contribute?

Please feel free to fork the code base and submit PR backed by appropriate test cases.

Terms Privacy Feed