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

bsarvate
I am new to kite. I have been trying the github example of websocket usage in python since past 3 days. I have been setting the access token properly after generating every day. Subscribed to only one instrument in LTP mode. But every day after 3 hrs or so the connection gets aborted and ticks stop working.
The logging is in debug mode. But this is the only error I get.
DEBUG:root:Ticks: [{'tradable': True, 'mode': 'ltp', 'instrument_token': 408065, 'last_price': 1172.5}]
DEBUG:root:Ticks: [{'tradable': True, 'mode': 'ltp', 'instrument_token': 408065, 'last_price': 1172.7}]
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (None)

My internet connectivity is not an issue as I have been using internet all the while.
Could someone please help resolve the issue.
  • sujith
    We tried the same example and it seems fine.
    By default, websocket reconnects when it disconnects. Did you stop getting ticks after this?
  • bsarvate
    I never get any ticks after this. Also it gets totally disconnected. Session ends abruptly after about 3hrs of running. It has been happening every time I try your example. I have been using MacBook Pro (Retina, 15-inch, Mid 2015), 2.2 GHz Intel Core i7 processor, 16 GB 1600 MHz DDR3 memory.
    Kindly look into the issue as I am really interested in using your platform for algo trading if the connection issue gets resolved.

    Thanks.
  • Ashok121
    websocket give many informations but i need to use only ltp in my stredegy

    how to print only last_price from websocket for python?
  • Ashok121
    I got solution for ltp
    def on_ticks(ws, ticks):
    for tick in ticks:
    x=tick['last_price']

    but websocket is not getting closed.
  • bsarvate
    @sujith Could you please look into my issue 1006 - connection was closed uncleanly (None)
  • sujith
    Can you paste your complete code?
  • bsarvate
    I have pasted the code twice. But the comment is not yet getting reflected.
  • bsarvate
    @sujith I don't know why the code is not getting pasted. But I am just trying out the example given in zerodhatech/pykiteconnect for WebSocket usage. I have already mentioned this while filing the ticket. I just don't understand why are you asking this again. I tried your code line by line in ipython as well as from a python file. Every time after 3 hrs or so the connection gets aborted and ticks stop coming in. Please check.
  • bsarvate
    It's the same code which is there in zerodha pykiteconnect...

    import logging
    from kiteconnect import KiteTicker

    logging.basicConfig(level=logging.DEBUG)

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

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

    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe([738561, 5633])

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, [738561])

    def on_close(ws, code, reason):
    # On connection close stop the main loop
    ws.stop()

    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close

    # Infinite loop on the main thread. Nothing after this will run.
    # You have to use the pre-defined callbacks to manage subscriptions.
    kws.connect()
  • sujith
    If you don't call ws.stop() inside on_close then it will reconnect. You don't have to do anything.
  • Ashok121
    attached please find code and output.

    My requirement is to check stock price at every second and if price is reached at certain level, I want to go with buy or sell.

    If I dont use while loop and without ws.close(), kiteticker does not allow me do further operation for buy or sell.
  • zartimus
    zartimus edited April 2018
    @Ashok121
    1. Write that logic with dates inside on_ticks callback
    2. KiteTicker cannot be restarted if it is closed in the same process
    3. Writing an infinite while loop on top of an event loop is wrong
    Hope it makes sense
  • sujith
    @bsarvate,
    You can check out this thread.
  • bsarvate
    @sujith It's working now. Thank you so much..
This discussion has been closed.