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.