NSEPython Documentation

First Initialize it -

from nsepython import *

Printing Indicies -

print(indices)

The Output will show -

['NIFTY', 'NIFTYIT', 'BANKNIFTY']

1 Like

Getting the FNO List -

print(fnolist())

The Output will show -

['BANKNIFTY', 'NIFTY', 'NIFTYIT', 'ADANIENT', 'ADANIPOWER', 'AMARAJABAT', 'ACC', 'PVR', 'RAMCOCEM', 'AMBUJACEM', 'APOLLOHOSP', 'ASIANPAINT', 'AUROPHARMA', 'AXISBANK', 'BAJAJ-AUTO', 'BAJAJFINSV', 'BAJFINANCE', 'ADANIPORTS', 'BALKRISIND', 'BHARTIARTL', 'BANDHANBNK', 'BATAINDIA', 'BEL', 'BERGEPAINT', 'BHARATFORG', 'BHEL', 'BOSCHLTD', 'BRITANNIA', 'CANBK', 'CIPLA', 'COALINDIA', 'COLPAL', 'CUMMINSIND', 'DABUR', 'BANKBARODA', 'DIVISLAB', 'DRREDDY', 'EICHERMOT', 'EXIDEIND', 'FEDERALBNK', 'GAIL', 'GLENMARK', 'GRASIM', 'HAVELLS', 'HCLTECH', 'HDFCLIFE', 'HEROMOTOCO', 'HINDALCO', 'HINDPETRO', 'HINDUNILVR', 'ICICIBANK', 'ICICIPRULI', 'INDIGO', 'INDUSINDBK', 'INFY', 'ITC', 'JUBLFOOD', 'JUSTDIAL', 'KOTAKBANK', 'LICHSGFIN', 'LUPIN', 'MANAPPURAM', 'MARICO', 'MARUTI', 'BIOCON', 'CADILAHC', 'MCDOWELL-N', 'MFSL', 'MGL', 'MINDTREE', 'MOTHERSUMI', 'MRF', 'MUTHOOTFIN', 'NATIONALUM', 'NCC', 'NIITTECH', 'NMDC', 'NTPC', 'PEL', 'PETRONET', 'PFC', 'PIDILITIND', 'RBLBANK', 'RECLTD', 'SAIL', 'SBIN', 'SIEMENS', 'SRF', 'SRTRANSFIN', 'SUNTV', 'TATAPOWER', 'TECHM', 'TITAN', 'TORNTPHARM', 'TORNTPOWER', 'TVSMOTOR', 'UJJIVAN', 'ULTRACEMCO', 'UPL', 'VOLTAS', 'ASHOKLEY', 'CONCOR', 'INFRATEL', 'BPCL', 'CHOLAFIN', 'DLF', 'EQUITAS', 'ESCORTS', 'IDEA', 'JSWSTEEL', 'LT', 'GODREJCP', 'GODREJPROP', 'SBILIFE', 'HDFC', 'HDFCBANK', 'IDFCFIRSTB', 'JINDALSTEL', 'M&M', 'M&MFIN', 'UBL', 'NAUKRI', 'NESTLEIND', 'ONGC', 'PAGEIND', 'POWERGRID', 'RELIANCE', 'SHREECEM', 'SUNPHARMA', 'TATAMOTORS', 'TATASTEEL', 'CENTURYTEX', 'VEDL', 'APOLLOTYRE', 'PNB', 'TATACHEM', 'IGL', 'IOC', 'TATACONSUM', 'TCS', 'WIPRO', 'ZEEL', 'L&TFH', 'IBULHSGFIN', 'GMRINFRA']

Getting Option Chain Data -

print(nse_optionchain_scrapper('PVR'))

The Output will show a huge JSON data. You can use https://jsonformatter.org/json-viewer to navigate through the data schema.

The oi_chain_builder Function

Usage:
oi_data,ltp,crontime=oi_chain_builder(symbol,expiry,oi_mode,mode)

Inputs:

  • symbol = FNO Symbol
  • expiry = “latest” (By default) or, You can choose next expiry by typing the expiry dates like “30-Jul-2020”
  • oi_mode = “full” (By default) or “compact” (It will fetch all options info).

Output:

  • oi_data = It will be a Pandas Dataframe.
  • ltp = LTP of the symbol
  • crontime = Data’s Updation Time as per NSE Server when fetched

Example:
oi_data, ltp, crontime = oi_chain_builder("PVR","latest","full")
or,
oi_data, ltp, crontime = oi_chain_builder("NIFTY")

it only shows “{}” when executed in powershell. Does powershell not show json?

The latter one makes a dictionary I suppose. Shows error that no such file or directory: oi_data/TATASTEEL.pickle.

Pickle is a module installed by python by default, so idk why the error.

Sometimes NSE site get crapped and genuinely give {}. That might be one reason.
Regarding Powershell, I never tried it.

NIFTY total OI and Change in oi columns got from nsepython is not matching with actual data in nse (All other columns are matching except toi and coi)

NsePython_Error

please check the attached screenshot

https://www1.nseindia.com/ and https://www.nseindia.com/ gives two different output.

Like, Lets say if we take BankNIFTY. One will show number of lots like 10 (Lets assume) while another will show in number of qty like 10*20=200.

Obviously www1 site will depreciate! Right?

Its not matching with both the old and new websites.
image

All three are screenshots taken almost close time

image

But how that will be possible. You can see my code too. You may be fetching in local mode from pickle? Instead of live?

oi_data, ltp, crontime = oi_chain_builder(“NIFTY”,“latest”,“full”,“fetch”)
Iam just using the oi_data received from this method

Then God knows…Check my code. It is just scrapping from NSE only. See if you can spot any mistake there.

image

Finally found whats wrong…U have hardcoded the multiplication of oi by 20. If it’s only Banknifty data scrapper it was fine but for others, it gives wrong OI and Change in OI :face_with_head_bandage:

Fixed it. Also wrote your name in code as Thank you Gesture.

  1. Fixed the OI problem as mentioned here. NSEPython Documentation
  2. Added ATM Strike Function as mentioned here - Getting the ATM strikes for all the FNO stocks
  3. Removed Pickle as suggested by everyone.

Getting live quotes for stocks using stock codes.

There are two types of APIs that fetch info in NSE.

nseindia.com/api/quote-equity?symbol=BAJAJFINSV
nseindia.com/api/quote-derivative?symbol=BANKNIFTY

Equity API and Derivatives API fetches the structure in a different manner. You can just paste their output to a website like jsonformatter.org/json-viewer and study.

print(nse_eq("JUSTDIAL"))
print(nse_fno("BANKNIFTY"))

You can also alternatively use quote_equity() function instead of nse_eq().
You can also alternatively use quote_derivative() function instead of nse_fno().

This first function will paste the output from the Equity API and the second function will paste the output from Derivatives API.

Stocks API

To get LTP of any stock, You need to do this -
print(nse_eq("JUSTDIAL")['priceInfo']['lastPrice'])

For Open, High, Low, Clsoe, You need to do this -
print(nse_eq("JUSTDIAL")['priceInfo']['open'])
print(nse_eq("JUSTDIAL")['priceInfo']['intraDayHighLow']['min'])
print(nse_eq("JUSTDIAL")['priceInfo']['intraDayHighLow']['max'])
print(nse_eq("JUSTDIAL")['priceInfo']['close'])

You can also find things like sector’s PE or sector’s name of which the stock belong -
print(nse_eq("JUSTDIAL")['metadata']['pdSectorPe'])
print(nse_eq("JUSTDIAL")['metadata']['pdSectorInd'])

Derivatives API

To get LTP of any derivative’s underlying, You need to do this -
print(nse_fno("BANKNIFTY")['underlyingValue'])

These APIs show a very detailed level of data. You need to explore yourself using the JSON formats. If there is any significant stuff, I may create a separate functions for it later on.

You can also alternatively use option_chain() function instead of nse_optionchain_scrapper() .

Also, Added one dumb function -

def running_status(): 
    start_now=datetime.datetime.now().replace(hour=9, minute=15, second=0, microsecond=0)
    end_now=datetime.datetime.now().replace(hour=15, minute=45, second=0, microsecond=0)
    return start_now<datetime.datetime.now()<end_now

Just do running_status(). It will give true if the market is running and false if the market is not running.

The Holiday API

There are two types -
https://www.nseindia.com/api/?type=trading
https://www.nseindia.com/api/?type=clearing

Here is how to use the function

nse_holidays(type)
or, holiday_master(type)

type = trading or clearing. By default, trading will be selected.

Here is how to see the holidays of FNOs -

print(pd.json_normalize(nse_holidays()['FO']))

The Results API

nseindia.com/companies-listing/corporate-filings-financial-results

The API looks like this -

nseindia.com/api/corporates-financial-results?index=equities&period=Quarterly

index has three types:

  • equities
  • debt
  • sme

period has four types:

  • Quarterly
  • Annual
  • Half-Yearly
  • Others

Usage:

nse_results(index,period)

Default Value of index is equities and period is Quarterly in nse_results() function.

Example Usage:

nse_results()
nse_results("equities","Others")