Escolar Documentos
Profissional Documentos
Cultura Documentos
Release 1.0
Swapnil Jariwala
1 Quick Start 3
1.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Quick Hand-On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
i
ii
NSEpy Documentation, Release 1.0
NSEpy is a library to extract historical and realtime data from NSE’s website. This Library aims to keep the API very
simple.
Python is a great tool for data analysis along with the scipy stack and the main objective of NSEpy is to provide
analysis ready data-series for use with scipy stack. NSEpy can seamlessly integrate with Technical Analysis library
(Acronymed TA-Lib, includes 200 indicators like MACD, RSI). This library would serve as a basic building block for
automatic/semi-automatic algorithm trading systems or backtesting systems for Indian markets.
Contents 1
NSEpy Documentation, Release 1.0
2 Contents
CHAPTER 1
Quick Start
1.1 Installation
Here’s a simple example to get historical stock data for the month of January 2015.
3
NSEpy Documentation, Release 1.0
Function get_history fetches the price history of stocks/indices/derivatives and returns a pandas dataframe.
In [1]: sbin
Out[1]: Symbol Series Prev Close Open High Low Last Close \
Date
2015-01-01 SBIN EQ 311.85 312.45 315.00 310.70 314.0 314.00
2015-01-02 SBIN EQ 314.00 314.35 318.30 314.35 315.6 315.25
2015-01-05 SBIN EQ 315.25 316.25 316.80 312.10 312.8 312.75
2015-01-06 SBIN EQ 312.75 310.00 311.10 298.70 299.9 299.90
2015-01-07 SBIN EQ 299.90 300.00 302.55 295.15 301.4 300.15
%Deliverble
Date
2015-01-01 0.3059
5
NSEpy Documentation, Release 1.0
2015-01-02 0.4249
2015-01-05 0.4208
2015-01-06 0.4844
2015-01-07 0.3743
Set futures=True and provide expiry_date of the contract (Refer Fetching Expiry Dates)
In [38]: stock_opt
Out[38]:
Symbol Expiry Open High Low Close Last \
Date
2015-01-01 SBIN 2015-01-29 315.10 317.95 313.40 316.65 317.00
2015-01-02 SBIN 2015-01-29 317.50 320.95 317.10 317.75 318.30
2015-01-05 SBIN 2015-01-29 318.00 318.75 314.10 315.00 315.05
2015-01-06 SBIN 2015-01-29 312.95 312.95 300.10 301.30 301.10
2015-01-07 SBIN 2015-01-29 301.95 304.55 297.35 302.25 303.50
2015-01-08 SBIN 2015-01-29 306.50 308.40 303.70 306.65 307.00
2015-01-09 SBIN 2015-01-29 306.75 309.25 301.05 304.75 304.15
Change in OI Underlying
Date
2015-01-01 358750 314.00
2015-01-02 -392500 315.25
2015-01-05 631250 312.75
2015-01-06 982500 299.90
2015-01-07 1335000 300.15
2015-01-08 -748750 304.85
2015-01-09 -252500 303.20
There are currently 50+ indices maintained by NSE. You can get historical data for all of them.:: Usage-
• symbol - Name of the index in capital (Refer this page for list of indices)
• index - Set this True for all index related operations
# NIFTY Next 50 index
nifty_next50 = get_history(symbol="NIFTY NEXT 50",
start=date(2015,1,1),
end=date(2015,1,10),
index=True)
# NIFTY50 Equal wight index (random index from the list)
nifty_eq_wt = get_history(symbol="NIFTY50 EQUAL WEIGHT",
start=date(2017,6,1),
end=date(2017,6,10),
index=True)
You will observe a lot of NaN values for many indeces like ‘NIFTY50 Equal wight index’, In those cases just use
‘Close’ values
Out of the 60+ indices, only 7 indeces are available for derivative. (List available here.) Usage-
• index - Set True
• futures - Set True
• expiry_date - Expiry date of the contract. refer:ref:get_expiry_date.
nifty_fut = get_history(symbol="NIFTY",
start=date(2015,1,1),
end=date(2015,1,10),
index=True,
futures=True,
expiry_date=date(2015,1,29))
nifty_opt = get_history(symbol="NIFTY",
start=date(2015,1,1),
end=date(2015,1,10),
index=True,
option_type='CE',
strike_price=8200,
expiry_date=date(2015,1,29))
India VIX is a volatility index which gives a measurement of market volatility based on NIFTY options contract. This
servers as important parameter in option pricing.
vix = get_history(symbol="INDIAVIX",
start=date(2015,1,1),
end=date(2015,1,10),
index=True)
Although NSE specifies a trading cycle of three months, most stock futures will not be traded for whole
three months, rather they will be active only in the month of expiry, So you may get 0 values for days
when no contracts were traded. Please deal with this situation carefully in your logic.
Fetch expiry date of all derivative contracts for a perticular month and year. Usage-
• month - Month of contract expiry starting from 1 for January and 12 for December
• year - Year of the contract expiry
stock_opt = get_history(symbol="SBIN",
start=date(2015,1,1),
end=date(2015,1,10),
futures=True,
expiry_date=get_expiry_date(2015,1))
9
NSEpy Documentation, Release 1.0
P/E ratio of a security helps to estimate if the security is over-priced or under-priced. NSE offers historical P/E ratio
for 30+ thematic and sectoral indices, we can use this data to determine which sectors are over-priced or under-priced
and further make investment decisions.
11
NSEpy Documentation, Release 1.0
13
NSEpy Documentation, Release 1.0
Download daily bhav copy, which is nothing but OHLC prices of all the traded stocks on a particular day:
15
NSEpy Documentation, Release 1.0
NSEpy offers a simple to use, easy to remember command line interface, This method is useful when you just want
the data for further processed in Excel, R or any other tool which supports CSV format. Basic Use, Fetch stock price
history
This will save price history of State Bank of India for the month of January 2017 as csv file with name output.csv.
Lets see the complete functionality using –help option.:
$ nsecli --help
Usage: nsecli history [OPTIONS]
Options:
Similar to stocks you can get Index data, just by adding –index flag -
Contents:
17
NSEpy Documentation, Release 1.0
• genindex
• modindex
• search
19