Updating websocket ticker

pranavwagh
Hello Experts,
This might be a noob question (so, apologies), but I am trying to find a way on how can I update the ticker subcriptions while the ticker is running without stopping and restarting it.
I don't have anything special in subscription, this is how I subscribe it.
==
def on_connect(ws, response):
logging.debug("Successfully connected. Response: {}".format(response))
print(tokens)
ws.subscribe(tokens)
==
I tried doing " ws.subscribe(tokens)" in on_ticks (with a different token list), but it doesn't seem to work. Any suggestions?
  • SRIJAN
    SRIJAN edited March 2022
    Subscribe method is working fine. Paste your code where you subscribe in on_ticks.
  • SRIJAN
    Also,you can print the subscribed tokens to check.
  • pranavwagh
    Hello @SRIJAN . Thanks! I had a typo, because of which "tokens" didn't have a right value. However, in your opinion it's okay to do this in on_tick? or there is a better place to update the subscribed tokens? in this implementation, it seems subscribe method is being called a lot of times (yes, I've added the code to ensure that it's not called every tick, but still it's called every 30 seconds), is there any other way to do it? where I can use subscribe again, only after tokens are really updated.
  • SRIJAN
    SRIJAN edited March 2022
    Simple,just put all the conditions to subscribe/unsubscribe in if statement. You can refer this thread:
    https://kite.trade/forum/discussion/4875/web-socket-dynamic-subscription
  • pranavwagh
    Yeah, that's obvious, but it might not work in scenarios where conditional evaluation is slow, example is my use case where I want to subscribe to tokens of my current positions. Which means, I have to call a webservice to get the current positions, which takes time, and would cause on_tick to fail (I hope my understanding that on_ticks is time sensitive is correct)
  • SRIJAN
    Then,you have to use threaded ticker.
  • pranavwagh
    so.. what I did was:

    ==
    def on_ticks(ws, ticks):

    if tokens!=current_position_tokens
    ws.subscribe(current_position_tokens)
    tokens=current_position_tokens
    ==

    where current_position_tokens was updated elsewhere outside on_ticks:

    Thanks for your help as always @SRIJAN !
Sign In or Register to comment.