How to handle Connection breaks , Exception handling

ashxos
Dear All,

Im running my algo for last one week. Below are the errors which have occured till now on daily basis.
Is this issue at my Network end or the Zerodha End? I can see one long thread on timeout issue already going on.

I need input on handling below error messages which are occuring frequently in my program which is causing program to stop.

How do you all handle such kind of exceptions?
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)
<urllib3.connection.HTTPSConnection object at 0x000001C48D2EC550>, 'Connection to api.kite.trade timed out. (connect timeout=7)

requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Max retries exceeded with url
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.kite.trade', port=443): Max retries exceeded with url

<urllib3.connection.HTTPSConnection object at 0x000002915A22B280>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed')
  • sujith
    It is difficult to say where is the error coming from. Since there are a lot of variables involved. Your script must be able to wait for a few milliseconds or seconds and then retry if it is a GET call. If it is a POST/PUT/DELETE call then you need to check if the action has happened and then retry. For example, if you are placing an order and you receive an error then you need to check orderbook before doing a retry.
  • rajista
    I have received similar error today while using websockets

    INFO:root:Order extraction failed: HTTPSConnectionPool(host='api.kite.trade', port=443): Max retries exceeded with url: /orders (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions'))
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
  • rakeshr
    @rajista
    /orders (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions'))
    How frequent is this error?
    Maybe you can look into this answer.
  • rajista
    Its been happening everyday lately. Same error happened on AWS ec2 instance as well as on my windows 10 pc. This is the code that i am running, to save tick information into a database. Today also everything was running fine until 2.12PM when the max retries error popped up and i had to force close the python program on aws to close the websockets connection. After closing the websockets connection manually the error disappears.



    kws = KiteTicker(key_secret[0], kite.access_token)
    tokens = tokenLookup(instrument_df3, tickers)

    create_tables(tokens)


    def on_ticks(ws, ticks):
    insert_tick = insert_ticks(ticks)
    print(ticks)


    def on_connect(ws, response):
    ws.subscribe(tokens)
    ws.set_mode(ws.MODE_FULL, tokens)


    def running_status():
    now = datetime.now()
    start_now = datetime.now().replace(hour=9, minute=15, second=0, microsecond=0)
    end_now = datetime.now().replace(hour=15, minute=30, second=0, microsecond=0)
    f = (start_now < datetime.now() < end_now) and (now.isoweekday() != 6) and (now.isoweekday() != 7)

    return f


    while True:
    now = datetime.now()
    f = running_status()
    if (f):
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.connect()
    else:
    print(" Market is closed ")
    db.close()
    sys.exit()


Sign In or Register to comment.