Hitting rate limit while fetching historical data for multiple stocks

Asked by Sejpalsinh Jadeja · SOLVED
1 reply 811 views Jump to solution Reply
Sejpalsinh Jadeja Original Post
AlgoDelta Team

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?

Posted Apr 24
Reply

    1 Reply

    Tech Support AlgoDelta
    Tech Support AlgoDelta Automation Expert ·

    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)
    Verified Solution
    • Ohk got it, thanks for the answer and code this solve my issue. – Sejpalsinh Jadeja Team Jun 17
    Markdown supported · ```json for code blocks · paste or drop screenshots

    Be specific and kind. Never share API keys, passwords or personal account details.

    Welcome to the community

    Log in or create your account in under a minute.

    1. 1Mobile
    2. 2E-mail
    3. 3Password
    +91

    Have a password?

    By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.