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

mnabhishek
Hi @sujith

Getting this error after every 5secs when I run my code. If I do not place orders, this runs fine. But when I place orders, it crashes after 5 secs.
  • rakeshr
    @mnabhishek
    Are you placing order inside on_tick method?
  • mnabhishek
    Yes @rakeshr - Is this not correct? How should I go about it?
    Some pointers will be really helpful.
  • rakeshr
    @mnabhishek
    Yes @rakeshr - Is this not correct? How should I go about it?
    No, you shouldn't be putting any logic inside on_tick thread.You need to pass on the tick on another method asynchronically without blocking on_tick thread.You can look to this thread for examples.
  • mnabhishek
    Cool @rakeshr thanks.
    I hope such a structure is fine.

    def on_ticks(ws, ticks):
    calculation(ticks)
  • rakeshr
    rakeshr edited April 28
    @mnabhishek
    There are two ways to pass tick data from on_tick thread to perform any operation without blocking the main on_ticks thread.
    1> You can push tick data to Queue(use celery,rq,etc) as task queue manager and have another method that reads these Queue data and perform the tasks.
    eg:
    def on_ticks(ws,ticks):
    #Pass the tick data to Queue using celery,rq,etc
    #Using celery delay method here to call helper_method task
    helper_method.delay(ticks)

    def helper_method(ticks):
    #Perform require operation using tick data
    2>Create another thread and perform the required operation in 2nd thread using threaded=True
    P.S: Don't forget to assign ticker callback for new thread.
    import logging
    from kiteconnect import KiteTicker

    logging.basicConfig(level=logging.DEBUG)

    kws = KiteTicker("your_api_key", "your_access_token")

    def on_ticks(ws, ticks):
    logging.debug("Ticks: {}".format(ticks))

    def on_connect(ws, response):
    ws.subscribe([738561, 5633])
    ws.set_mode(ws.MODE_FULL, [738561])

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

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.connect(threaded=True)

    while True:
    #Perform required data operation using tick data
    def on_ticks(ws, ticks):
    ..........
    helper_method(ticks)
    .........

    def helper_method(ticks):
    .........
    Perform computation here
    ........
    #Assign callback
    kws.on_ticks=on_ticks


  • renjitharajan84
    @rakeshr I have tried your above sugggested logic to offload on_ticks and my new code looks like below. my issue here is, the lines after "while True:" are not getting executed.



    you may also note there is a compiler error for repeating the " def on_ticks(ws, ticks):" line. my main thread is given below for your reference.



    could you please look in to this and advise me what is wrong in my code? I am really stuck at this point on my journey of learning and desperately need help

    Thanks.
    3.png 36.6K
    22.png 79.9K
  • rakeshr
    @renjitharajan84
    lines after "while True:" are not getting executed.
    You are assigning ticker call back kws.on_ticks=on_ticks out side the while loop scope. It should be inside the while loop scope.
    Eg:
    while True:
    ......
    ticker methods
    .....
    #It should be within the scope of while loop (correct)
    kws.on_ticks=on_ticks
    #You are assigning it here,outside the scope(incorrect)
    kws.on_ticks=on_ticks
  • renjitharajan84
    @rakeshr thank you, please let me try this correction, will update the result soon..
  • renjitharajan84
    @rakeshr I have tried your suggestion, and the call back is working perfectly fine when i have changed the indent of "kws.on_ticks=on_ticks". My updated code looks like below
    while True:
    def on_ticks(ws, ticks):
    feed_data(ticks)
    print("i am from while True -> on ticks: feed_data module called!")

    def feed_data(ticks):
    for tick in ticks:
    token = tick['instrument_token']
    row = trd_portfolio[token]['row']
    sht.range('C'+str(row)).value = tick['last_price']
    sht.range('D'+str(row)).value = tick['ohlc']['open']
    sht.range('E'+str(row)).value = tick['ohlc']['high']
    sht.range('F'+str(row)).value = tick['ohlc']['low']
    sht.range('G'+str(row)).value = tick['ohlc']['close']
    sht.range('H'+str(row)).value = tick['volume']
    sht.range('I'+str(row)).value = datetime.datetime.now()
    print(datetime.datetime.now())
    print("i am from -> feed_data function")

    print("i am from main -> after kws.connect(threaded=True)")
    kws.on_ticks=on_ticks
    How ever the purpose is still not served here, it is not calling the on_ticks(ws, ticks): function under while loop, instead, only below two lines under the while loop are getting executed.
    print("i am from main -> after kws.connect(threaded=True)")
    kws.on_ticks=on_ticks


    could you please suggest the required changes in my code to trigger on_ticks(ws, ticks) function under while loop, else some logic to pass the ticks data to my "feed_data(ticks)" function.. would also work here

    Thanks a lot for your patience and time to support me.

    Regards,
    ACR
  • renjitharajan84
    hello @rakeshr, please be kind enough to provide me a solution, I am stuck at this point and need your help. continuously print("i am from main -> after kws.connect(threaded=True)") line is getting printed but excel sheet is not updating. (i suspect the on_ticks(ws, ticks): line inside while loop is not triggering)
  • sujith
    @renjitharajan84,
    Kite Connect is purely an execution platform. We don't provide support or assistance for writing code or strategies. It is outside the scope of this forum.

    If someone is willing they will answer if they are not willing to do so please don't force them. You can hire someone as well, there are many freelancers on the forum who write strategies.
  • renjitharajan84
    Dear @sujith,

    My apologies if you have mistaken my humble request to @rakeshr as a compulsion/forcing, I am very well aware that was a voluntary act from rakeshr and was trying to help me. I have approached him again only because, that was his code suggestions and getting unexpected results when tried.

    Also I could see from other threads that he has been very supportive and willing to help beginners always.

    Thanks.
  • rakeshr
    @renjitharajan84
    Your code looks correct. on_ticks ticker method should be called properly. But,we feel as it's continous while loop print("i am from main -> after kws.connect(threaded=True)") is printed much more times than print statement inside on_ticks, and feed_data methods. So, you can try removing all print statement outside of above two methods and then check. Something like below:
    while True:
    def on_ticks(ws, ticks):
    feed_data(ticks)
    print("i am from while True -> on ticks: feed_data module called!")

    def feed_data(ticks):
    ......
    Computaion
    .....
    print(datetime.datetime.now())
    print("i am from -> feed_data function")
    #comment out or remove any print statement outside of above two methods
    #print("i am from main -> after kws.connect(threaded=True)")
    kws.on_ticks=on_ticks
  • renjitharajan84
    Thank you so much @rakeshr, greatly appreciated! I will post the results soon, after trying this correction.
  • renjitharajan84
    @rakeshr you were right, the on_ticks(ws, ticks) function inside while loop was perfectly triggering. thank you so much for the patience & support once again.
  • renjitharajan84
    @rakeshr, I am able to stream the data now flawlessly with some minor errors, that I can handle alone. my main hurdle was to handle the overload of on_ticks event.
Sign In or Register to comment.