NetworkException in kite.margins() call

mlearner
mlearner edited June 3 in Python client
I got following exception during market hours when I was trying to fetch margin for my account.

Any idea why this occurred and how can I prevent it? Should I just reattempt kite.margins() call when this kind of exception occurs?
margin = kite.margins()

File "/home/ubuntu/.local/lib/python3.8/site-packages/kiteconnect/connect.py", line 315, in margins

return self._get("user.margins")

File "/home/ubuntu/.local/lib/python3.8/site-packages/kiteconnect/connect.py", line 817, in _get

return self._request(route, "GET", params)

File "/home/ubuntu/.local/lib/python3.8/site-packages/kiteconnect/connect.py", line 886, in _request

raise exp(data["message"], code=r.status_code)

kiteconnect.exceptions.NetworkException: request WC1ZW1wDSlFL4TRDi1GTKbfQ3uRGlzl3 timed out
Tagged:
  • rakeshr
    Are you trying to run this during post-mid-night?
    As post-midnight OMS runs the BOD process till around 6:30 AM and during that time it will not accept any requests.
  • mlearner
    No @rakeshr, this exception occurred during market hours on 2 Jun '21.
  • rakeshr
    Is it continuously happening? If not, then you need to handle this network time-out exception at your end and make the same request again with a few ms delay.
    kiteconnect.exceptions.NetworkException
  • mlearner
    No it is not continuously happening. Happened just once. Sure, will handle this exception and will make a request again with some delay. Thanks.
  • mlearner
    One more doubt, since I can only make a finite recursive calls in Python (999 to be precise), if the network issue between Kite and OMS persists for a longer duration say for 5 min or so(could be realistically even higher), all my retry attempts would be over in this much time.

    Can you please suggest a way to handle such unforeseen API downtime in future?
  • sujith
    You can retry with exponential backoff method which is like first time it will retry of 5 seconds, 10 seconds, and then 15 seconds and so on.
  • mlearner
    @sujith - Got it. Thanks.
This discussion has been closed.