βœ–

Home Forums Trading view Hitting rate limit while fetching historical data for multiple stocks

Hitting rate limit while fetching historical data for multiple stocks

  • Author
    Posts
    • πŸ“ Question Details

      I’m building an supertrend algo where I need to fetch historical data for multiple stocks at startup.

      Right now I’m looping over all symbols and calling the historical API, but after a few calls I start getting rate limit errors from the broker.

      Because of this, some stocks don’t load properly and my indicators break.

      What’s the correct way to handle this when working with multiple symbols?

    • Yes, the rate limit varies from broker to broker (e.g.: Zeroth 3req/sec)

      This happens in almost every broker if we hit the API in the loop continuously.

      To solve this problem, you have to put the delay, if possible, of approx. 0.30 sec,

      also, we can do the batch fetching – hit the API in batch

      Below sample code you can refer:

      import time
      
      def fetch_all_data(symbols, fetch_func):
          data = {}
      
          for i, symbol in enumerate(symbols):
              try:
                  data[symbol] = fetch_func(symbol)
                  print(f"Fetched: {symbol}")
      
                  # small delay to avoid rate limit
                  time.sleep(0.5)
      
              except Exception as error:
                  print(f"Failed: {symbol} -> {error}")
      
          return data
      
      # Sample
      def mock_fetch(symbol):
          return f"data_for_{symbol}"
      
      symbols = ["AAPL", "MSFT", "GOOG", "TSLA"]
      
      result = fetch_all_data(symbols, mock_fetch)
      
      print(result)
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