βœ–

Home Forums General Questions How to fetch historical data from Zerodha API properly ?

How to fetch historical data from Zerodha API properly ?

  • Author
    Posts
    • πŸ“ Question Details
      I’m building an algo trading system and need to fetch historical data (like 1-minute candles) from Zerodha.
      I saw the historical_data function but not sure what’s the correct way to use it in production.
      Can someone share a simple and reliable way to fetch historical candles?
    • 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/.

Viewing 1 reply thread
×

Start a Discussion

Get help from the AlgoDelta community.

×

Welcome Back!

Enter your email to sign in or create an account. No passwords needed.

⏳

Pending Approval

⚠️

Delete This?

Are you sure you want to delete this? This action is permanent and cannot be undone.

Scroll to Top