To fetch the historical data from zerodha’s api below is the sample code that you can refer and modify it accordingg to your requirenment.
def fetch_historical(kite, token, interval="minute", days=7):
try:
to_date = datetime.now()
from_date = to_date - timedelta(days=days)
data = kite.historical_data(
instrument_token=token,
from_date=from_date,
to_date=to_date,
interval=interval
)
# FIX: strip timezone from all historical candle dates for consistent comparison
for c in data:
if c.get("date") and hasattr(c["date"], "tzinfo") and c["date"].tzinfo:
c["date"] = c["date"].replace(tzinfo=None)
return data
except Exception as error:
print(f"Historical fetch failed for token {token}: {error}")
return []
fetch_historical(kite = kite,token=209604 , interval="minute", days=5)
Since the from_date and to_date are necessary parameters gives the candle data between this 2 dates in format datetime.
There are the 2 more parametrs we can pass to the kite.historical_data that is IO ( for IO data ) and continuous ( for continuous data ), but they are optional also sees thatin offical documentation https://www.kite.trade/docs/.