NSEPy Functions

The Functions are the newest/better adaptaions of the functionality of another wrapper named NSEPy that is depreciated (No longer actively maintained). 

Side by Side Comparison with NSEpy Functions -

  • Stock price history – Replaced with equity_history().
  • Stock futures price history – Replaced with derivative_history().
  • Stock options price history – Replaced with derivative_history().
  • Index price history – Replaced with derivative_history().
  • Index futures price history – Replaced with derivative_history().
  • Index options price history – Replaced with derivative_history().
  • India VIX price history – Depreciated. There is no API Endpoint found on the new NSE website.
  • Missing or zero values in derivative data – Added as a note to the derivative_history() section.
  • Fetching Expiry Dates – Replaced with expiry_history().
  • Index P/E Ratio History – Replaced with index_pe_pb_div(). It also has a PB ratio and Dividend History.
    • Also added a new function index_total_returns() that gives Total Index Returns.
  • RBI Reference Rates – Depreciated. Did not wanted to Scrap RBI Website yet.
  • Daily Bhav Copy – Replaced with get_bhavcopy().
    • Also added a new function block_deals() that gives all block deals from the NSE Archive.
    • Also added a new function bulk_deals() that gives all bulk deals from the NSE Archive.

Equity History

Example Usage:

				
					equity_history(symbol,series,start_date,end_date)
				
			

Example:

				
					symbol = "SBIN"
series = "EQ"
start_date = "08-06-2021"
end_date ="14-06-2021"
print(equity_history(symbol,series,start_date,end_date))
				
			

Output:

				
					                        _id CH_SYMBOL CH_SERIES CH_MARKET_TYPE  \
0  60c744e8fa749200099709a1      SBIN        EQ              N   
1  60c3505b99ad6d0008be9c68      SBIN        EQ              N   
2  60c1fefe81433a00081e9a8d      SBIN        EQ              N   
3  60c0ad631318bc0008009871      SBIN        EQ              N   
4  60bf5be2d2265400080d7d9b      SBIN        EQ              N   

   CH_TRADE_HIGH_PRICE  CH_TRADE_LOW_PRICE  CH_OPENING_PRICE  \
0               432.45              416.70            427.00   
1               435.50              425.25            434.85   
2               433.30              422.40            424.90   
3               432.30              418.20            427.50   
4               432.50              424.00            432.25   

   CH_CLOSING_PRICE  CH_LAST_TRADED_PRICE  CH_PREVIOUS_CLS_PRICE  \
0            430.35                430.00                 429.60   
1            429.60                429.40                 432.25   
2            432.25                432.40                 421.45   
3            421.45                421.95                 427.20   
4            427.20                427.40                 432.25   

   CH_TOT_TRADED_QTY  CH_TOT_TRADED_VAL  CH_52WEEK_HIGH_PRICE  \
0           30106015       1.279312e+10                441.95   
1           26831924       1.153870e+10                441.95   
2           27397712       1.174471e+10                441.95   
3           27784834       1.185249e+10                441.95   
4           31113823       1.330030e+10                441.95   

   CH_52WEEK_LOW_PRICE  CH_TOTAL_TRADES       CH_ISIN CH_TIMESTAMP  \
0               169.25           279081  INE062A01020   2021-06-14   
1               169.25           211915  INE062A01020   2021-06-11   
2               169.25           211924  INE062A01020   2021-06-10   
3               169.25           241080  INE062A01020   2021-06-09   
4               169.25           247824  INE062A01020   2021-06-08   

                  TIMESTAMP                 createdAt  \
0  2021-06-13T18:30:00.000Z  2021-06-14T12:00:40.201Z   
1  2021-06-10T18:30:00.000Z  2021-06-11T12:00:27.251Z   
2  2021-06-09T18:30:00.000Z  2021-06-10T12:01:02.131Z   
3  2021-06-08T18:30:00.000Z  2021-06-09T12:00:35.995Z   
4  2021-06-07T18:30:00.000Z  2021-06-08T12:00:34.187Z   

                  updatedAt  __v    VWAP   mTIMESTAMP  
0  2021-06-14T12:00:40.201Z    0  424.94  14-Jun-2021  
1  2021-06-11T12:00:27.251Z    0  430.04  11-Jun-2021  
2  2021-06-10T12:01:02.131Z    0  428.67  10-Jun-2021  
3  2021-06-09T12:00:35.995Z    0  426.58  09-Jun-2021  
4  2021-06-08T12:00:34.187Z    0  427.47  08-Jun-2021  
				
			

Derivative History

Syntax:

				
					derivative_history(symbol,start_date,end_date,instrumentType,expiry_date,strikePrice="",optionType="")
				
			

Example:

				
					symbol = "SBIN"
start_date = "15-05-2021"
end_date ="15-06-2021"
instrumentType = "options"
expiry_date ="24-Jun-2021"
strikePrice = 300
optionType="PE"
print(derivative_history(symbol,start_date,end_date,instrumentType,expiry_date,strikePrice,optionType))
				
			

The Strike Price (strikePrice) and Option Type (optionType) can be given  only when the instrument type is Options. They are optional parameter in general.

Output:

				
					
                        _id FH_INSTRUMENT FH_SYMBOL FH_EXPIRY_DT  \
0  60c755447984ddf3843dc745        OPTSTK      SBIN  24-Jun-2021   
1  60c35b6d7984ddf38457d4a9        OPTSTK      SBIN  24-Jun-2021   
2  60c211e67984ddf38439b427        OPTSTK      SBIN  24-Jun-2021   
3  60c0baf27984ddf38458bfe4        OPTSTK      SBIN  24-Jun-2021   

  FH_STRIKE_PRICE FH_OPTION_TYPE FH_MARKET_TYPE FH_OPENING_PRICE  \
0          300.00             PE              N             0.25   
1          300.00             PE              N             0.25   
2          300.00             PE              N             0.35   
3          300.00             PE              N             0.30   

  FH_TRADE_HIGH_PRICE FH_TRADE_LOW_PRICE FH_CLOSING_PRICE  \
0                0.30               0.20             0.25   
1                0.30               0.20             0.25   
2                0.35               0.20             0.25   
3                0.35               0.25             0.35   

  FH_LAST_TRADED_PRICE FH_PREV_CLS FH_SETTLE_PRICE FH_TOT_TRADED_QTY  \
0                 0.20        0.25            0.25            117000   
1                 0.25        0.25            0.25             48000   
2                 0.20        0.35            0.25             34500   
3                 0.35        0.30            0.35            111000   

  FH_TOT_TRADED_VAL FH_OPEN_INT FH_CHANGE_IN_OI FH_MARKET_LOT FH_TIMESTAMP  \
0       35128125.00      756000       -54000.00          1500  14-Jun-2021   
1       14411700.00      810000            0.00          1500  11-Jun-2021   
2       10359075.00      810000        -7500.00          1500  10-Jun-2021   
3       33332175.00      817500        -3000.00          1500  09-Jun-2021   

                  TIMESTAMP  CALCULATED_PREMIUM_VAL  
0  2021-06-13T18:30:00.000Z                   28125  
1  2021-06-10T18:30:00.000Z                   11700  
2  2021-06-09T18:30:00.000Z                    9075  
3  2021-06-08T18:30:00.000Z                   32175  
				
			

Note – 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.

Expiry History

Syntax:

				
					expiry_history(symbol,start_date="",end_date="")
				
			

It means start_date and end_date is not mandatory.

Usage:

				
					symbol = "SBIN"
start_date = "15-05-2020"
end_date ="15-06-2021"

print(expiry_history(symbol,start_date,end_date))
print(expiry_history(symbol,start_date))
print(expiry_history(symbol))
				
			

Output:

				
					['28-May-2020', '25-Jun-2020', '30-Jul-2020', '27-Aug-2020', '24-Sep-2020', '29-Oct-2020', '26-Nov-2020', '31-Dec-2020', '28-Jan-2021', '25-Feb-2021', '25-Mar-2021', '29-Apr-2021', '27-May-2021', '24-Jun-2021', '29-Jul-2021', '26-Aug-2021']

['28-May-2020', '25-Jun-2020', '30-Jul-2020', '27-Aug-2020', '24-Sep-2020', '29-Oct-2020', '26-Nov-2020', '31-Dec-2020', '28-Jan-2021', '25-Feb-2021', '25-Mar-2021', '29-Apr-2021', '27-May-2021', '24-Jun-2021', '29-Jul-2021', '26-Aug-2021']

['27-May-2021', '24-Jun-2021', '29-Jul-2021', '26-Aug-2021']
				
			

As You see the end_date has no meaning when the start_date is given.

Index History

NIFTY Indicies Site Scrapping

This API is scrapped from https://niftyindices.com. Here is how to get history of any index like NIFTY in Python –

Syntax:

				
					index_history(symbol,start_date,end_date)
				
			

Usage:

				
					symbol = "NIFTY 50"
start_date = "01-Mar-2011"
end_date = "07-Jun-2021"
print(index_history(symbol,start_date,end_date))
				
			

Output:

				
					     Index Name INDEX_NAME HistoricalDate      OPEN      HIGH       LOW  \
0      Nifty 50   NIFTY 50    07 Jun 2021  15725.10  15773.45  15678.10   
1      Nifty 50   NIFTY 50    04 Jun 2021  15712.50  15733.60  15622.35   
2      Nifty 50   NIFTY 50    03 Jun 2021  15655.55  15705.10  15611.00   
3      Nifty 50   NIFTY 50    02 Jun 2021  15520.35  15597.45  15459.85   
4      Nifty 50   NIFTY 50    01 Jun 2021  15629.65  15660.75  15528.30   
...         ...        ...            ...       ...       ...       ...   
2537   Nifty 50   Nifty 50    08 Mar 2011    5466.1   5530.55   5464.75   
2538   Nifty 50   Nifty 50    07 Mar 2011   5490.05   5491.25   5408.45   
2539   Nifty 50   Nifty 50    04 Mar 2011    5586.2    5608.2    5524.1   
2540   Nifty 50   Nifty 50    03 Mar 2011   5478.45   5570.75   5468.25   
2541   Nifty 50   Nifty 50    01 Mar 2011      5382   5533.05   5373.55   

         CLOSE  
0     15751.65  
1     15670.25  
2     15690.35  
3     15576.20  
4     15574.85  
...        ...  
2537   5520.80  
2538   5463.15  
2539   5538.75  
2540   5536.20  
2541   5522.30  
				
			

Index Historical PE, PB and Dividend Ratio

Syntax:

				
					index_pe_pb_div(symbol,start_date,end_date)

				
			

Usage:

				
					symbol = "NIFTY 50"
start_date = "01-Mar-2011"
end_date = "07-Jun-2021"
print(index_pe_pb_div(symbol,start_date,end_date))
				
			

Output:

				
					     Index Name     pe    pb divYield         DATE
0      NIFTY 50  29.21  4.44     1.07  07 Jun 2021
1      NIFTY 50  29.06  4.42     1.07  04 Jun 2021
2      NIFTY 50  29.10  4.43     1.07  03 Jun 2021
3      NIFTY 50  28.88  4.39     1.06  02 Jun 2021
4      NIFTY 50  28.87  4.39     1.07  01 Jun 2021
...         ...    ...   ...      ...          ...
2536   Nifty 50  21.14   3.5     1.13  08 Mar 2011
2537   Nifty 50  20.92  3.46     1.14  07 Mar 2011
2538   Nifty 50  21.21  3.51     1.12  04 Mar 2011
2539   Nifty 50   21.2  3.51     1.12  03 Mar 2011
2540   Nifty 50  21.14   3.5     1.13  01 Mar 2011
				
			

Total Index Returns

How to get total Index returns using Python of any index like NIFTY –

Syntax:

				
					index_total_returns(symbol,start_date,end_date)

				
			

Usage:

				
					symbol = "NIFTY 50"
start_date = "01-Mar-2011"
end_date = "07-Jun-2021"
print(index_total_returns(symbol,start_date,end_date))
				
			

Output:

				
					     Index Name         Date TotalReturnsIndex
0      NIFTY 50  07 Jun 2021          22449.24
1      NIFTY 50  04 Jun 2021          22333.23
2      NIFTY 50  03 Jun 2021          22361.86
3      NIFTY 50  02 Jun 2021          22194.18
4      NIFTY 50  01 Jun 2021          22192.25
...         ...          ...               ...
2535   Nifty 50  08 Mar 2011           6931.56
2536   Nifty 50  07 Mar 2011           6859.22
2537   Nifty 50  04 Mar 2011           6954.14
2538   Nifty 50  03 Mar 2011           6950.94
2539   Nifty 50  01 Mar 2011           6933.43
				
			

Daily Bhav Copy

Syntax:

				
					get_bhavcopy(date)

				
			

Usage:

				
					print(get_bhavcopy("04-06-2021"))

				
			

Output:

				
					          SYMBOL  SERIES         DATE1   PREV_CLOSE   OPEN_PRICE   HIGH_PRICE  \
0     1018GS2026      GS   04-Jun-2021       106.00       102.00       102.00   
1      20MICRONS      EQ   04-Jun-2021        62.55        63.40        64.35   
2     21STCENMGM      EQ   04-Jun-2021        15.95        15.95        16.25   
3     3IINFOTECH      EQ   04-Jun-2021         8.95         8.90         9.00   
4        3MINDIA      EQ   04-Jun-2021     26261.80     26431.80     26648.70   
...          ...     ...           ...          ...          ...          ...   
2053   ZODJRDMKJ      BE   04-Jun-2021        36.60        37.40        37.40   
2054        ZOTA      EQ   04-Jun-2021       183.75       184.00       184.00   
2055       ZUARI      EQ   04-Jun-2021        97.30        97.85        98.40   
2056   ZUARIGLOB      EQ   04-Jun-2021        92.55        93.95        94.00   
2057   ZYDUSWELL      EQ   04-Jun-2021      2083.75      2095.25      2107.45   

       LOW_PRICE  LAST_PRICE   CLOSE_PRICE   AVG_PRICE   TTL_TRD_QNTY  \
0         102.00      102.00        102.00      102.00             50   
1          61.30       61.95         61.90       62.25         213663   
2          15.95       16.25         16.25       16.25            358   
3           8.70        8.85          8.85        8.85       13804265   
4       26000.00    26113.70      26110.45    26301.91           2347   
...          ...         ...           ...         ...            ...   
2053       34.85       35.05         36.45       36.10           8456   
2054      160.00      172.95        173.60      174.48         121798   
2055       96.05       96.35         96.50       97.08          89071   
2056       91.00       93.25         92.65       92.34          53853   
2057     2080.00     2091.00       2096.90     2082.57         169249   

       TURNOVER_LACS   NO_OF_TRADES  DELIV_QTY  DELIV_PER  
0               0.05              1         50     100.00  
1             133.00           1813     131248      61.43  
2               0.06              8        358     100.00  
3            1221.17          15649    9166344      66.40  
4             617.31           1771       1192      50.79  
...              ...            ...        ...        ...  
2053            3.05             72          -          -  
2054          212.51           3414      52566      43.16  
2055           86.47           1569      50221      56.38  
2056           49.73            559      37343      69.34  
2057         3524.73           2958     154029      91.01  

				
			

Getting Block Deals of NSE

Usage:

				
					print(get_blockdeals())

				
			

Output:

				
					          Date  Symbol              Security Name  \
0  14-JUN-2021     HEG                    HEG Ltd   
1  14-JUN-2021  VGUARD  V-Guard Industries Limite   
2  14-JUN-2021     HEG                    HEG Ltd   
3  14-JUN-2021  VGUARD  V-Guard Industries Limite   

                         Client Name Buy/Sell  Quantity Traded  \
0      BHARAT INVESTMENTS GROWTH LTD      BUY           100000   
1                    SBI MUTUAL FUND      BUY          5000000   
2                  JHUNJHUNWALA RAVI     SELL           100000   
3  CHITTILAPPILLY THOMAS KOCHUOUSEPH     SELL          5000000   

   Trade Price / Wght. Avg. Price  
0                          2265.0  
1                           265.0  
2                          2265.0  
3                           265.0  
				
			

Getting Bulk Deals of NSE

Request Endpoint: https://archives.nseindia.com/content/equities/bulk.csv

This is different than The Block Deals API. This is using different Request Endpoint.

Usage:

				
					print(get_bulkdeals())

				
			

Output:

				
					           Date      Symbol              Security Name  \
0   14-JUN-2021  ALBERTDAVD       Albert David Limited   
1   14-JUN-2021         BCG     Lycos Internet Limited   
2   14-JUN-2021  DLINKINDIA           D-Link India Ltd   
3   14-JUN-2021    DWARKESH  Dwarikesh Sugar Industrie   
4   14-JUN-2021   GLOBUSSPR     Globus Spirits Limited   
..          ...         ...                        ...   
92  14-JUN-2021  VISESHINFO  Visesh Infotecnics Limite   
93  14-JUN-2021       WABAG          VA Tech Wabag Ltd   
94  14-JUN-2021         WFL   Wonder Fibromats Limited   
95  14-JUN-2021  ZENSARTECH  Zensar Technologies -Depo   
96  14-JUN-2021       ZICOM  Zicom Electronic Security   

                            Client Name Buy/Sell  Quantity Traded  \
0         GRAVITON RESEARCH CAPITAL LLP      BUY            56669   
1            ALPHA LEON ENTERPRISES LLP      BUY          3464347   
2            OHM INVESTMENT CORPORATION      BUY           300000   
3         GRAVITON RESEARCH CAPITAL LLP      BUY           970493   
4         GRAVITON RESEARCH CAPITAL LLP      BUY           178554   
..                                  ...      ...              ...   
92    PATALIPUTRA INTERNATIONAL LIMITED     SELL         19000000   
93        GRAVITON RESEARCH CAPITAL LLP     SELL           353309   
94  KANIPAKAM EDUCATION PRIVATE LIMITED     SELL           161600   
95              MARINA HOLDCO (FPI) LTD     SELL         25750000   
96                    IDBI BANK LIMITED     SELL           250000   

    Trade Price / Wght. Avg. Price Remarks  
0                           528.40       -  
1                            12.65       -  
2                           121.57       -  
3                            70.55       -  
4                           397.25       -  
..                             ...     ...  
92                            0.25       -  
93                          362.37       -  
94                           67.46       -  
95                          295.16       -  
96                            1.25       -  
				
			

Join The Conversation?