Difficulty fetching Historical Data using DhanAPI

I have gone through all the Algo Trading videos uploaded through Dhan but not able to use the API to fetch historical data. Getting error for the expiry - {‘status’: ‘failure’, ‘remarks’: “expiry_code value must be [‘0’,‘1’,‘2’,‘3’]”, ‘data’: ‘’}

I have tried using “0”, ‘0’ and just 0 but getting the same error.

The following is my code
from dhanhq import dhanhq

client_id = “Used my client_ID”
access_token = “Used my access token”

dhan = dhanhq(client_id, access_token)

security_id = 13 # NIFTY 50
exchange_segment = “IDX_I” # correct for indices
instrument_type = “INDEX”
expiry_code = 0 # must be int, not string
from_date = “2023-01-01”
to_date = “2023-12-31”

data = dhan.historical_daily_data(
security_id,
exchange_segment,
instrument_type,
expiry_code,
from_date,
to_date
)

print(data)

Hi @Akshay_Pednekar ,

Welcome to madefortrade,

We request you to make the API call without entering the expiry code, and it should work fine.

Sample Code :

dhan.historical_daily_data(“13”,“IDX_I”,“INDEX”,“2023-01-01”,“2023-12-31”)

1 Like

can you share the code for downloading the same in excel sir?

dear shubhnam, i have been struggling to get the historical 5MIN BNF data. Python Code has been taken from chatGPT. finally i got to print data on the command prompt, however i need in excel for backtesting purpose. your assistance would be highly appreciated. thanks

Hi @Viswanathan_Kumar,

In Excel, you need to convert the response code on your own.