NetworkException: Too many requests

UE6253
I am using kite.quote() api for appx 50 stocks in a loop and it worked for few times while testing the code but now started giving NetworkException: Too many requests error. I am sleeping for 2 sec for each quote request which is more than 1s limit set by zerodha I believe.
Ideally i would want to use WebSocket api but that wasnt working and issue is open for that as well. Any suggestions ???
  • rakeshr
    @UE6253
    We ran quote call for more than 100 stocks in loop with delay of 1 second, didn't faced any time-out.Please recheck your code, make sure you are not running other rate limit API request in parallel.
  • UE6253
    It is not working for me. Here is exception I get after 4-5 quotes. I am also attaching code here..


    File "", line 1, in
    runfile('D:/workspace_pyCharm/amit_portfolio_update_V3.py', wdir='D:/workspace_pyCharm')

    File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

    File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

    File "D:/workspace_pyCharm/amit_portfolio_update_V3.py", line 137, in
    allQuotes = module_Get_Live_Data_From_Zerodha.getAllQuotesFromZerodha(stock_names)

    File "D:\workspace_pyCharm\Module_Get_Live_Data_From_Zerodha.py", line 27, in getAllQuotesFromZerodha
    quote = self.kite.quote(fullid)

    File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect-3.7.4-py3.6.egg\kiteconnect\connect.py", line 546, in quote
    data = self._get("market.quote", {"i": ins})

    File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect-3.7.4-py3.6.egg\kiteconnect\connect.py", line 697, in _get
    return self._request(route, "GET", params)

    File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect-3.7.4-py3.6.egg\kiteconnect\connect.py", line 766, in _request
    raise exp(data["message"], code=r.status_code)

    NetworkException: Too many requests
  • UE6253
    UE6253 edited August 2018
    <code class="CodeInline">

    def getAllQuotesFromZerodha(self, stock_names):

    content = []
    for row in stock_names:
    fullid = row['fullid']
    quote = self.kite.quote(fullid)
    if quote:

    print(fullid, " -- ", quote)
    # s1 = json.dumps(quote)
    s1=json.dumps(quote, indent=4, sort_keys=True, default=str)
    data = json.loads(s1)
    lp = data[fullid]['last_price']
    volume = data[fullid]['volume']
    change = data[fullid]['net_change']
    prev_close = data[fullid]['ohlc']['close']
    change_percent = round((change*100)/prev_close, 2)
    # print("lp -- ", lp)
    # print("volume -- ", volume)
    # quote = self.kite.ltp(fullid)
    content2 = {}
    content2['fullid'] = fullid
    content2['l'] = '{}'.format(lp).replace(",", "");
    content2['c'] = '{}'.format(change).replace(",", "");
    content2['cp'] = '{}'.format(change_percent).replace(",", "");
    content2['pcls'] = '{}'.format(prev_close);
    content2['volume'] = '{}'.format(volume);
    print(content2)
    t.sleep(2)

    content.append(content2);

    return content

  • rakeshr
    @UE6253
    I ran above code in my env , with stock_names param as list of all Nifty 50 stocks, it ran without any error.Rate limit error can only come, in case quote var is empty and loop doesn't go inside if quote .So, better you can insert time delay just after quote call instead of at end of quote loop,
    quote = self.kite.quote(fullid)
    t.sleep(2)
    Also , print your stock_names param and check, it should be in format ['NSE:SBIN', 'BSE:XYZ']
  • UE6253
    That was a good catch Rakesh. sleep needed to be outside if loop since some of the quotes were coming empty. Thanks man..
This discussion has been closed.