Error while live streaming data

enigma
While streaming live data the connection is getting closed with the following error ::
"Can't stop reactor that isn't running.")
twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running.
Before this another error occurs ::
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
Unhandled Error
Can anyone please help me out with this? I don't understand why this error is occuring while live streaming the data.

I am using kiteconnect v3.0, pykiteconnect
  • enigma
    @rakeshr
    Thanks for the reply. I went through this thread but couldn't get a suitable solution.

    But I saw in one comment that you have mentioned to remove threaded=True from kws.connect( threaded=True)

    I am using this threaded option because I am using Tkinter alongwith kiteconnect. Since tkinter also has a mainloop I had to use this, otherwise I was not able to update LTP in tkinter window.

    One thing to mention is that I am not using threading module for this case. Putting kws.connect(threaded=True) serves my purpose of properly updating the LTP in tkinter window.

    Do you have any suggestions for this?
  • rakeshr
    @enigma
    You might be blocking main thread by not passing on to another thread in correct way.Can you go through this thread to see an example of multi-threading.
  • neerleo88
    Below is the code to print ticker only and it is based on your documentation itself . No additional logic implemented. Please let me know where blocking in on_tick() callback because this time also , within 30 min I am getting the same error message.

    If the code(which is exactly like your documentation) is buggy the provide me the exact code line which has bug and why

    Please Note : I didn't use on_close() on purpose because I don't want the execution to stop. And that is too according to your suggestion provided in different discussion thread.

    import time
    import logging
    from kiteconnect import KiteConnect
    from kiteconnect import KiteTicker
    import pandas as pd
    api_key="************"
    api_secret="************************"
    kite=KiteConnect(api_key=api_key)
    access_token="******************"
    kite.set_access_token(access_token)
    tokens=[260105]
    kws=KiteTicker(api_key, access_token)

    def on_ticks(ws,ticks):
    data1 = pd.DataFrame(ticks)
    print(data1[["timestamp", "last_price"]])

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

    def on_close(ws,code,reason):
    ws.stop()

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.connect()

    Below are the are the error messages occurs at different time
    0 2019-01-16 09:25:39    27503.35
    timestamp last_price
    0 2019-01-16 09:25:40 27501.5
    Connection error: 1006 - connection was closed uncleanly (None)
    Connection closed: 1006 - connection was closed uncleanly (None)
    timestamp last_price
    0 2019-01-16 09:25:52 27499.3
    timestamp last_price
    0 2019-01-16 09:25:53 27501.2
    0 2019-01-16 09:59:39    27522.95
    timestamp last_price
    0 2019-01-16 09:59:40 27523.65
    timestamp last_price
    0 2019-01-16 09:59:41 27523.55
    Connection error: 1006 - connection was closed uncleanly (None)
    Connection closed: 1006 - connection was closed uncleanly (None)
    Connection error: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
    Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
    timestamp last_price
    0 2019-01-16 10:00:43 27533.15
    timestamp last_price
    0 2019-01-16 10:00:44 27531.1
    timestamp last_price
    0 2019-01-16 10:00:45 27532.75
    0 2019-01-16 10:37:10    27532.95
    timestamp last_price
    0 2019-01-16 10:37:11 27533.2
    timestamp last_price
    0 2019-01-16 10:37:12 27530.6
    Connection error: 1006 - connection was closed uncleanly (None)
    Connection closed: 1006 - connection was closed uncleanly (None)
    timestamp last_price
    0 2019-01-16 10:37:28 27531.65
    timestamp last_price
    0 2019-01-16 10:37:29 27534.15
    timestamp last_price
    0 2019-01-16 10:37:30 27534.05
    Please do not say here that these error only occurring at my side and everywhere its working fine BECAUSE below is the link for another discussion thread where someone else has faced the same issue and there also you guys didn't provide the solution

    https://kite.trade/forum/discussion/5426/error-while-live-streaming-data#latest

    If these evidence are not enough then please let me know what else is required and how I can provide them
  • neerleo88
    @zartimus
    I know you guys just closed my last thread without providing the solution. So i am putting here again the issue

    @enigma
    Did you find any solution for this


  • razcads
    @neerleo88 what I have seen is that Connection error: 1006 - connection was closed uncleanly is very common and is thrown by websocket quite regularly. U may end up losing few ticks but otherwise there is no impact, the websocket recovers by itself and reconnects and ticks continue.. I would say log the error and ignore it. My guess is that when websocket is unable to subscribe to parent due to network fault or simple overload it get disconnected by server and reconnects.. anyway that's something @zartimus or @sujith need to confirm.
  • neerleo88
    thanks @razcads for replying.

    The problem is , I am converting these ticks in 5min candles and after that I do many calculation like body and shadow length,ATR etc. These calculation are necessary to generate buy or sell call. So if there are missing ticks frequently then all the output of those calculations will be changed , which generate wrong Trade Calls.

    Do you have any trick to overcome this issue or get data for those missing ticks?

    @zartimus @sujith Can you confirm this behavior of your websocket ?
  • razcads
    @neerleo88 I dont think there is much you can do.. this is inherent problem with any websocket. Usually the data loss is less than a few seconds.. you should be able to live with that I think, its very very very rare that huge movements will occur with small gaps in ticks. Dont sweat over this, i am very sure your trade calls will not go wrong with such miniscule misses.
  • neerleo88
    @razcads Thanks for the suggesstion :)
  • enigma
    @neerleo88
    Hi,

    I still didn't get any solution. The thread which @rakeshr has mentioned didn't provide any suitable solution for the problem.
    Hi @razcads ... Can you give some solution to handle the error.. Like any code sample if you could.
  • razcads
    razcads edited January 2019
    You can try this.
    def on_error(ws,code,reason):
    message = ("Error from KiteTicker %s, Error code %s, Error reason %s" %(ws,code, reason))
    if code == '1006':
    # or just write this error to log or any other mechanism
    print message
    else:
    # if you want to abort processing for other errors
    print message
    ws.close()
  • drunkNifty
    @neerleo88 Are you really convinced with 'blatantly rude and unprofessional' suggestion 'Ignore the errors' given by @razcads ? I am NOT!
    We are talking about algo trading here (and that's the bussiness opportunity Zerodha is monetizing with our money). Algo trading doesn't have discretion of an human being to swallow these errors gracefully. I have already endured big holes in my trading account due to crappy APIs and my discussion is still unanswered (as expected, as it takes balls to acknowledge failures and brains to rectify them).
    On top of that, these guys are always ready with the template response "Hello {{foolish_customer.name}}, everything seems to be working fine on our end, please check your network." :p
  • enigma
    enigma edited January 2019
    Sorry for delayed reply. I would like to know if I put in my code kws.connect( threaded=True) this will create one thread. So whenever at runtime I want to add new token/token_list should I be executing this code again?

    @drunkNifty :: Were you able to develop full code without these kinds of errors. If you have can you share some suggestions.. that would be really appreciated.

    @razcads :: Surely I will try and let you know
Sign In or Register to comment.