Modify Subscription webSocket Streaming data

kiteapi
Can someone let me know how do you modify your websocket token subscription to the token on the fly, basically unsubscribing and subscribing to new tokens? I could not find one, hence asking.

Not asking for exact code, but any pointers to which code blocks should go where would be helpful or a thread which discusses this topic.
Tagged:
  • sujith
    You can check out the pykiteconnect documentation here.
    You can call it any number of times on the fly.
  • sujith
    You just need to make sure that the connection is still open.
  • kiteapi
    @sujith @rakeshr

    Went through few threads and came up with this, covers as a boiler plate for not having any logic inside main on_ticks, also covered how to unsubscribe and subscribe to new tokens if needed. All worker methods inside the while loop, trade logic and other methods called under on_ticks under while loop.

    Can you guys confirm if this looks fine?


  • rakeshr
    Flow looks fine.
  • kiteapi
    Please have a look at this, the flow is fine too? Found this somewhere, not able to recollect now :(


  • kiteapi
    @sujith @rakeshr

    Is there a way to on_connect without subscribing at the firs time, I would like to connect, but subscribe to the instrument_token later based on the trade logic to only the instruments needed.

    I even tried this:

    tokens0 = []
    def on_connect(ws, response):
    ws.subscribe(tokens0)
    ws.set_mode(ws.MODE_FULL, tokens0)

    and tried this:

    tokens0 = [10985474]
    def on_connect(ws, response):
    ws.subscribe(tokens0)
    ws.set_mode(ws.MODE_FULL, tokens0)
    ws.unsubscribe(tokens0)

    But, does not work :(

    Do I need to do on_connect again inside While True?
  • sujith
    sujith edited August 2020
    You can open websocket connection and then subscribe anytime when you need data.

    on_connect is just a callback called when on_connected event is triggered.
Sign In or Register to comment.