My following historical data code is giving error, returning empty list/dataframe

CODE:- :upside_down_face:
import requests
import pandas as pd
import json

url = “https://api.dhan.co/charts/historical

headers = {

  • “Content-Type”: “application/json”,*
  • “access-token”: removed on purpose # Replace with your actual token*
    }

payload = {

  • “securityId”: “180854”, # CAMS*
  • “exchangeSegment”: “NSE_EQ”,*
  • “instrument”: “EQUITY”,*
  • “expiryCode”: 0,*
  • “oi”: False,*
  • “fromDate”: “2024-04-01”,*
  • “toDate”: “2024-04-23”,*
  • “interval”: “1m”*
    }

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

# Handle response
if response.status_code == 200:

  • data = response.json()*
  • print(“Raw response:\n”, data) # ← Print full JSON*
  • candles = data.get(“candles”, )*
  • print (candles, " ##################### candles ######### ")*
  • if candles:*
  •    df = pd.DataFrame(candles, columns=["timestamp", "open", "high", "low", "close", "volume"])*
    
  •    print(df.head())*
    
  • else:*
  •    print("✅ No candle data returned for selected dates.")*
    

else:

  • print(“:x: Error:”, response.status_code)*
  • print(“:envelope_with_arrow: Response:”, response.text)*
  • print(“:mag: Status Code:”, response.status_code)*

##############################################################

ERROR I AM GETTING IS: :upside_down_face:
Raw response:
{}
##################### candles #########
:white_check_mark: No candle data returned for selected dates.

#################
I am stuck due to this kindly send the solution to this as soon as possible. Thanks in advance

Hi @jayashree_doiphode ,

It appears that the Security ID you’re using for CAMS Equity may be incorrect. Please refer to the official list at Instrument List - DhanHQ Ver 2.0 / API Document to obtain the correct Security ID and try the Data API call again.

Please refer the below mentioned sample code :

{
“securityId”: “342”,
“exchangeSegment”: “NSE_EQ”,
“instrument”: “EQUITY”,
“interval”: “1”,
“oi”: false,
“fromDate”: “2024-04-01”,
“toDate”: “2024-04-23”
}

Happy Trading !