I am using the KiteConnect API for WebSocket streaming, which allows a maximum of 3,000 instruments to be subscribed to at a time. To manage a list of over 10,000 instruments that changes daily, I need to dynamically subscribe and unsubscribe to stay within this limit. This requires subscribing and unsubscribing every 10 seconds to capture all the data effectively.
The API documentation can be found here: https://kite.trade/docs/pykiteconnect/v4/
Could anyone help with an approach for efficiently handling this process?
import logging
from kiteconnect import KiteTicker
instrument_list = [1,2,3,4,......,11234]
logging.basicConfig(level=logging.DEBUG)
# Initialise
kws = KiteTicker("my_api", "my_access_token")
def on_ticks(ws, ticks):
# Callback to receive ticks.
print(ticks)
def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe(instrument_list)
# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, instrument_list)
def on_close(ws, code, reason):
# On connection close stop the event loop.
# Reconnection will not happen after executing `ws.stop()`
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()