Fetching nifty 50 historical option data

Whenever, i’m trying to fetch, index historical option data i’m getting null dataframe. But the code is working well for stock historical option data. I have also check on NSE website regarding my parameters.

Below is my code : -

symbol = "NIFTY"

start_date = "24-04-2023"

end_date = "25-04-2023"

instrument_type = "options"

expiry_date = "04-May-2023"

strike_price = 17900.00

option_type = "CE"

derivative_data = derivative_history(symbol, start_date, end_date, instrument_type,expiry_date,strike_price, option_type)

Update the nsepython library and try again. It will work.

There was a conditional error in the derivative_history function which I have fixed.

    if(instrumentType=="options"):
        instrumentType="OPTSTK"
        if("NIFTY" in symbol): instrumentType="OPTIDX"
        
    if(instrumentType=="futures"):
        instrumentType="FUTSTK"
        if("NIFTY" in symbol): instrumentType="FUTIDX"

I had never used options history that much so I did not noticed it earlier

Thank you for fixing the code.
I have another query, whenever i’m fetching historical equity data, latest data is fetched only till 20-03-2023.

Below is a snapshot of my code and the resulted dataframe.

@dexter The code you have fixed only retrieving recent historical option data of index.
But when i’m fetching data of year 2021 and before it is giving me null dataframe, will you please look into it.
Below i’m attaching the snapshot.

I will respond to all the queries of everyone at once after couple of days. I am going out for next two days. Pardon me

Hey dexter!
Did you looked into this, actually i’m making a model which requires historical data and i’m not able to fetch nifty historical option data before 2022.

Not yet. Will do in friday. Use nsefetch() meanwhile with the accurate URL.

Hi dexter
Have you fixed the issue?

Slight way. Busy in Gann Project. I will do it by tonight.

okay, also there is no showing of volume in index history !

The API which is pulled by Index History does not give volume. So how you will get volume?

2 posts were split to a new topic: I am still getting NULL dataframe

I ran

symbol = “NIFTY”
start_date = “01-06-2021”
end_date = “01-06-2021”
instrument_type = “options”
expiry_date = “03-Jun-2021”
strike_price = 13500
option_type = “CE”
derivative_data = derivative_history(symbol, start_date, end_date, instrument_type,
expiry_date, strike_price, option_type)

print(derivative_data)

and got

INFO:root:Starting Date: 2021-06-01 00:00:00
INFO:root:Ending Date: 2021-06-01 00:00:00
INFO:root:Total Number of Days: 0
INFO:root:Total FOR Loops in the program: 0
INFO:root:Remainder Loop: 0
INFO:root:End Loop
INFO:root:====
INFO:root:Starting Date: 01-06-2021
INFO:root:Ending Date: 01-06-2021
INFO:root:====
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   158  100   158    0     0   1203      0 --:--:-- --:--:-- --:--:--  1206
INFO:root:https://www.nseindia.com/api/historical/fo/derivatives?&from=01-06-2021&to=01-06-2021&optionType=CE&strikePrice=13500.00&expiryDate=03-Jun-2021&instrumentType=OPTIDX&symbol=NIFTY
INFO:root:{'data': [], 'meta': {'symbol': 'NIFTY', 'optionType': 'CE', 'expiryDate': '03-Jun-2021', 'strikePrice': '13500.00', 'instrumentType': 'OPTIDX', 'year': '2021', 'fromDate': '01-06-2021', 'toDate': '01-06-2021'}}
INFO:root:Finale
INFO:root:Length of the Total Dataset: 0
Empty DataFrame
Columns: []
Index: []

The format of this URL is correct. But Can you confirm if the URL structure is wrong or right?

https://www.nseindia.com/api/historical/fo/derivatives?&from=01-06-2021&to=01-06-2021&optionType=CE&strikePrice=13500.00&expiryDate=03-Jun-2021&instrumentType=OPTIDX&symbol=NIFTY

Will this fix work for BANKNIFTY and FINNIFTY ??

Why will it not? It should as long as NSE does not change URL structure.

while fetching manually, it is showing url structure as:-
https://www.nseindia.com/get-quotes/derivatives?symbol=NIFTY#eq-derivatives-historicaldata

1 Like

No that is not URL Structure.

See the datasource. I mean from where it is getting the data. check

See how I got it from inspect element,