KiteTicker closing loop

Ashok121
I have following code for KiteTicker to fetch live tick data: It runs successfully
but I am not able to come from this loop. Refer attached image for output.i.e. how to close this loop. Can you please suggest correction in below code? I want to use this data for placing order at particular time.


from kiteconnect import KiteTicker
kws = KiteTicker("my_api_key","my_access_token")
def on_ticks(ws, ticks):
for tick in ticks:
x=tick['last_price']
print('RELIANCE PRICE=',x)
def on_connect(ws,response):
ws.subscribe([738561])
ws.set_mode(ws.MODE_LTP, [738561])
def on_close(ws,code,reason):
ws.close()
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.connect()
Tagged:
  • sujith
    @Ashok121,
    You can check out the example here.
    You need to call ws.stop() inside on_close.
  • Ashok121
    See attachment ws.stop() is inside on_close only. But still same issue
  • zartimus
    @Ashok121 Check this.
    Yup ws.stop() has to happen inside on_close callback. But to trigger on_close callback based on some logic, you need to call ws.close() somewhere right?
  • Ashok121
    Can you let me know where to put ws.stop and where ws.close?
  • zartimus
    zartimus edited April 2018
    @Ashok121 Please read the above message. Check into the link specified
  • Ashok121
    If I call ws.close() on on_connect, it's closing loop without printing stock price.

    Could you please provide exact location?
  • Ashok121
    Ashok121 edited April 2018

    I got the location for ws.close(). Please find attached script.

    Now one more issue, while using while loop, at every one second tick data is not being produced. refer output of script also.

    Pls help.
  • zartimus
    @Ashok121 What purpose does the while loop serve here? Error clearly states that the reactor is not restartable and still you close the ticker on on_ticks and attempts to restart. This clearly defeats the purpose of websocket. If your idea is for processing ticks only if a specific condition is met, why dont you specify that condition inside on_ticks callback and remove the infinite while loop.
This discussion has been closed.