Not getting historical data for index through v1 or v2 (works for equity)

import requests
import json

# Replace with your DhanHQ access token
access_token=""


symbol = "NIFTY" 
exchange_segment = "IDX_I"  
instrument = "INDEX"
expiry_code = 0
from_date = "2024-09-15"
to_date = "2024-09-16"

url = "https://api.dhan.co/charts/historical"
headers = {
    "Content-Type": "application/json",
    "access-token": access_token
}
data = {
    "symbol": symbol,
    "exchangeSegment": exchange_segment,
    "instrument": instrument,
    "fromDate": from_date,
    "expiryCode":   expiry_code,
    "toDate": to_date
}

response = requests.post(url, headers=headers, json=data)

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print("Error:", response.status_code, response.text)

This is v1 above which results in {} empty response for getting index historical data.

For v2

import requests
import json

# Replace with your DhanHQ access token
access_token =  ""


security_id = "13" 
exchange_segment = "IDX_I"  
instrument = "INDEX"
expiry_code = 0
from_date = "2024-09-16"
to_date = "2024-09-17"

url = "https://api.dhan.co/v2/charts/historical"
headers = {
    "Content-Type": "application/json",
    "access-token": access_token
}
data = {
    "securityId": security_id,
    "exchangeSegment": exchange_segment,
    "instrument": instrument,
    "fromDate": from_date,
    "expiryCode":   expiry_code,
    "toDate": to_date
}

response = requests.post(url, headers=headers, json=data)

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print("Error:", response.status_code, response.text)

I’m getting error message: Error: 500 {“errorType”:“Data_Error”,“errorCode”:“DH-907”,“errorMessage”:“System is unable to fetch data due to incorrect parameters or no data present”}

Can someone please point out. I’m able to get the historical data for equity though. Thanks.

Hello @Mitaanshu_Agarwal

Yes, I find the similar issue here, checking this. Will update you on this.

1 Like

Sure, looking forward @Hardik

@Hardik is there any update for this?

@Hardik i’m still stuck here. Can you check?

Hello @Mitaanshu_Agarwal

This is fixed, sorry missed the thread. Can you check and confirm if you are still facing this issue?

Hi Hardik,

I’m Still facing the issue
{
“securityId”: “13”,
“exchangeSegment”: “IDX_I”,
“instrument”: “INDEX”,
“expiryCode”: 0,
“fromDate”: “2023-08-24”,
“toDate”: “2023-08-24”
}

{
“errorType”: “Data_Error”,
“errorCode”: “DH-907”,
“errorMessage”: “System is unable to fetch data due to incorrect parameters or no data present”
}

1 Like

Hello @Nirmal_Soni

Looks like you have kept fromDate and toDate as the same. If you look into the documentation, it requires atleast 1 day gap, as the toDate is non-inclusive.

(post deleted by author)