Prepare request payload
payload = {
"securityId": "13", # Update with the actual security ID (Nifty 50)
"exchangeSegment": "I", # Use correct exchange segment
"instrument": "INDEX", # Update as per your need
"expiryCode": 0, # Should match your instrument's expiry code
"fromDate": "2022-01-08",
"toDate": "2022-02-08"
}
response = dhan.historical_daily_data(
security_id= payload["securityId"],
exchange_segment=payload["exchangeSegment"],
instrument_type=payload["instrument"],
expiry_code=0,
from_date=payload["fromDate"],
to_date=payload["toDate"]
)
if response.get("status") != "success":
logging.error("Failed to fetch historical data: %s", response)
return None
I have written this code but getting an error
INFO:root:Fetching historical data for backtest…
ERROR:root:Failed to fetch historical data: {‘status’: ‘failure’, ‘remarks’: {‘error_code’: ‘DH-905’, ‘error_type’: ‘Input_Exception’, ‘error_message’: ‘Missing required fields, bad values for parameters etc.’}, ‘data’: {‘errorType’: ‘Input_Exception’, ‘errorCode’: ‘DH-905’, ‘errorMessage’: ‘Missing required fields, bad values for parameters etc.’}}