No matter how much I change my code or parameters, I am gain and again getting an empty dataset

AJ2610
I am very new to coding and Zerodha API, I subscribed to it in the evening.
First of all, Iearnt from the internet that I could fetch the historical data for as long as I wanted, but when I tried to fetch 2yr long data for 15 minute candle. My IDE showed an error that it can't exceed 200 days.

Second, I don't know why how much I am changing my code, the symbol, the dates or the interval. My code is returning everytime an empty dataset. Following is a slice of my coding logic and the output.
Please explain.

CODE
.........
def fetch_historical_data(symbol, interval='day', from_date='2023-11-01 09:15:00', to_date='2023-11-18 15:30:00'):
url = f"https://api.kite.trade/instruments/historical/{symbol}/{interval}"
params = {
'from': from_date,
'to': to_date
}
headers = {
'X-Kite-Version': '3',
'Authorization': f'token {api_key}:{access_token}'
}

response = requests.get(url, params=params, headers=headers)

if response.status_code == 200:
return response.json()['data']
else:
print(f"Failed to fetch data for symbol {symbol}. Status code: {response.status_code}, Response: {response.text}")
return None

symbol = '605697'
historical_data = fetch_historical_data(symbol)

if historical_data:
df = pd.DataFrame(historical_data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])

df['timestamp'] = pd.to_datetime(df['timestamp'])

df.set_index('timestamp', inplace=True)

# Print the DataFrame
print(df)


OUTPUT

Empty DataFrame
Columns: [open, high, low, close, volume]
Index: []
  • sujith
    Can you enable debug logs as mentioned here?
    And paste the complete stack trace here. Make sure to remove app and client specific tokens.
  • AJ2610
    GOT RESOLVED. THERE WAS SOME ERROR FROM MY SIDE IN CODING. SORRY.
    THANK YOU FOR YOUR RESPONSE @sujith

    And can you please guide me about how old the data can be extracted for different timeframes?
  • sujith
    You can refer to the FAQs here.
Sign In or Register to comment.