Ticks Missing Post 12:30 PM Everyday


I have recently started running an example code for Websocket streaming and recording ticks in a database for only one instrument (Nifty 50 index) on python client (version 3.7.7). The code is multithreaded and kite.connect() does not block the main thread (I record ticks in the database in a separate thread). However, every day there is a problem wherein, I get the following error while the code is running

Connection error: 1006 - connection was closed uncleanly (None)

The interesting bit is that this happens generally after 12:30 pm (one day it happened after 1 pm, another day after 12:50 pm and today at 12:23 pm). Post the connection break, it generally reconnects after 3-5 seconds, and then records ticks for about a minute but after that, there are no ticks recorded for almost the next 10 minutes (another consistent and interesting bit!), post which the same error happens again, and the cycle continues.

Why does this happen every day after 12:30 pm that too in 10 min intervals? I am using multithreading and recording ticks for only one instrument hence I do not believe that might be the issue. Please help resolve this issue at the earliest.

P.S. I am using python3 on a MacOS 2015 version (the database I record to is psql based and stored locally). I use a new access token everyday before market opens.

  • rakeshr
    Connection error: 1006 - connection was closed uncleanly (None)
    Above error comes, if on_tick method is blocked while receiving fresh tick or in case of internal error/errors in the called main method.Here,it can happen that, post 12 PM/above mentioned time there might be more ticks(more activity) and you calling secondary thread to pass on the tick might be blocking main thread in case of receiving fresh tick before passing tick to secondary thread.
    So, can you log the number of ticks received in that specific interval in case of disconnection error?
  • aceBox
    @rakeshr thanks for the reply. I am actually only doing 1 operation in the on_tick method - adding the ticks in a queue (inbuilt python datastructure) The other thread is a consumer thread (producer consumer model) and is not being called from on_tick. Again, there are no ticks received during the error time, hence I cannot log the number
  • aceBox
    aceBox edited September 4
    Same error happened today as well at 12:44 PM.

    This is how kite connect is called:
    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close

    # connect for listening with threading enabled
    kws.connect(threaded = True)
    BTW, this also happens after the market closes (hence, cannot be explained by more ticks); Also, currently there is a check for the main thread to exit after 15:35, but that does not happen once the error starts popping up
  • sujith
    Can you paste your on_close here?
  • aceBox
    @sujith @rakeshr I figured it out! Quite stupid really, but my laptop has a setting of going to sleep after the display turns off. Now this happens generally after 3 hours, and when it happened, the executing instance used to give this error. Changed the setting to never go to sleep and now it works fine! Thanks all.
Sign In or Register to comment.