How to stop and start ticker data parsing

Observer
I am new to using kite api and am running the following code.

def on_ticks(ws, ticks):
# Callback to receive ticks.
for ticker in ticks:
print(ticker['last_price'])

def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (NIFTY).
ws.subscribe([256265])

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

def on_close(ws, code, reason):
# On connection close stop the event loop.
# Reconnection will not happen after executing `ws.stop()`
ws.stop()

kws = KiteTicker(kite.api_key, kite.access_token)
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect(threaded=True)

Once I start running it, NIFTY LTP is printed on the console every second. I want to know how to stop this parsing. And once I stop this process, can I re-run .connect() again to fetch the prices. As in I want to know can I switch this process on and off?
  • rakeshr
    You can use the stop() method to stop the WebSocket streaming.
    can I re-run .connect() again
    The ticker can't be restarted in the same process, so you need to re-spun the whole program again.
  • Observer
    So I need to restart kite session again or kill the console as well?
  • rakeshr
    No, the access token is valid for a day. So, you can use the same access token for a day, just restart the whole WebSocket program again.
Sign In or Register to comment.