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.

Requesting historical data with parameters:
FINNIFTY security_id: 27
exchange_segment: IDX_I
instrument_type: INDEX
expiry_code: 0
from_date: 2024-11-06
to_date: 2024-11-07
Response: {‘status’: ‘failure’, ‘remarks’: {‘error_code’: ‘DH-907’, ‘error_type’: ‘Data_Error’, ‘error_message’: ‘System is unable to fetch data due to incorrect parameters or no data present’}, ‘data’: {‘errorType’: ‘Data_Error’, ‘errorCode’: ‘DH-907’, ‘errorMessage’: ‘System is unable to fetch data due to incorrect parameters or no data present’}}

Hello @ronit_suthar

Can you check if you are using Historical Data in 1 day timeframe or intraday minute data. Also, try increasing the date range and let me know if you are still facing this issue.

def historical_data(self):
try:
equity_historical_data = self.dhan.historical_daily_data(
security_id=self.idx_id,
exchange_segment=self.exchange_segment,
instrument_type=self.instrument_type,
expiry_code=0,
from_date=self.from_date,
to_date=self.to_date
) today = datetime.today().date()
self.from_date = (today - timedelta(days=3)).strftime(‘%Y-%m-%d’)
self.to_date = today.strftime(‘%Y-%m-%d’)

“I need the historical data for the last 3 days. I’ve been trying to access it since yesterday, but I’m still encountering the same issue. Could you please verify on your end if you’re using the historical data in the 1-day timeframe? I am facing the problem specifically with Finnifty and security_id ‘27’.”

Hello @ronit_suthar

Tried this and it is working fine. Can you confirm, for which Security ID are you trying to fetch and the error that you are getting.

I am facing the problem specifically with Finnifty and security_id ‘27’.”

From Date: 2024-11-03
To Date: 2024-11-18
Payload: {‘securityId’: ‘27’, ‘exchangeSegment’: ‘IDX_I’, ‘instrument’: ‘INDEX’, ‘fromDate’: ‘2024-11-03’, ‘toDate’: ‘2024-11-18’}
Full Response Data: {‘status’: ‘failure’, ‘remarks’: {‘error_code’: ‘DH-907’, ‘error_type’: ‘Data_Error’, ‘error_message’: ‘System is unable to fetch data due to incorrect parameters or no data present’}, ‘data’: {‘errorType’: ‘Data_Error’, ‘errorCode’: ‘DH-907’, ‘errorMessage’: ‘System is unable to fetch data due to incorrect parameters or no data present’}}
High: None
Low: None
Open: None
Close: None

@ronit_suthar Noted. Will try replicating this scenario.

anyupdate @Hardik

@Hardik anyupdate

Hello @ronit_suthar

Yes, checked this. Looks like there is an issue with FinNifty, will update you with an RCA here soon.

@Hardik anyupdate