Change symbol subscribtion real time

CE7727
Hi,

I start with (say) 20 symbols subscription to websocket. At 9:30, few symbols are not required and at 9:45 further few symbols are not required. How can I do this?

My logic is as follows (in Python).
1. Execute kws.connect() before 9:15
2. Inside on_connect(), subscribe to all 20 symbols using ws.subscribe()
3. Inside on_ticks(), check if time is 9:30 or 9:45, if so then call ws.stop() and kws.connect()

My idea is, the moment kws.connect() is called at 9:30 and 9:45, on_connect() will be called again and the required symbols will be subscribed. This only works if all the symbols get unsubscribed the moment ws.stop() is called.

Please let me know if my understanding and logic is correct or not.

Thanks
Arindam
  • sujith
    The first thing you need to make sure while using the Kite Ticker is that you don't do any calculations inside on_tick.
    The only job on_tick has to do is offload the task to other threads and nothing more. If you do calculation or processing in that then you may be already losing some ticks there.

    Coming back to your query, you don't need to disconnect, you can just unsubscribe for tokens for which you don't need data and send another subscribe message with new tokens.
  • CE7727
    Thanks for the reply.
    I am not doing any calculation inside on_tick(). The symbols that should remain subscribed are being calculated in a different program. The information is passed via a db. I am only checking the time inside the on_tick() to change the subscr list.
    So when you say unsub the symbols, do i call unsub function inside the on_tick() function?
    Also, is there a way to unsub all symbols at once?
  • sujith
    It is the same as subscribe call. You can know more here.
  • CE7727
    I am asking, should I call the unsub inside on_click()? I have called the subscr 1st time round from inside on_connect(). Once streaming starts, I think on_connect() will not be called and only on_click() will be called?
  • rakeshr
    @CE7727
    Yes,you can send subscribe or unsubscribe tokens from on_tick method but make sure that, you don't block the main on_tick thread by performing any logic inside,you can call new method to subscribe/unsubscribe token from on_tick thread asynchronously.
    You can refer to this thread.
  • CE7727
    Hi,

    Thanks for the reply. I will be honest, I went through some of the posts on multithread and they say it fixes the issue but I could not find a post that actually says what to do. I understand that I have to do kws.connect(threaded=True) instead of kws.connect(). But what next. When I run kws.connect(), on_tick runs for ever and if connection gets lost due to internet issue then auto connect happens. So if i pass the threaded=True parameter, these entire thing will happen in the background. And so after the code line kws.connect(threaded=True), I will just check the time of day and my unsub list and call the unsubscribe function? So I am guessing I will have to keep an infinite loop running as well, otherwise how will the code check the time of day? The code will be something like this?

    while (pd.datetime.now().time() > pd.to_datetime(['09:14:00']).time[0] and pd.datetime.now().time() < pd.to_datetime(['15:16:00']).time[0]):
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.on_error = on_error
    kws.connect(threaded=True)
    sub_token=get_sub_list() #1st time subscribe all
    ws.subscribe(sub_token)
    if pd.datetime.now().time() == pd.to_datetime(['09:30:00']).time[0]:
    unsub_token=get_unsub_list() #2nd time unsub few
    ws.unsubscribe(unsub_token)
    if pd.datetime.now().time() == pd.to_datetime(['09:45:00']).time[0]:
    unsub_token=get_unsub_list() #3rd time unsub few more
    ws.unsubscribe(unsub_token)

    Also, I have insert_tick(ticks) function call inside on_tick(). It writes the ticks to mysql db. How to make sure this function also does not interefere the flow of on_tick() streams.

    Thanks
    Arindam
  • rakeshr
    rakeshr edited May 22
    @CE7727
    I will just check the time of day and my unsub list and call the unsubscribe function? So I am guessing I will have to keep an infinite loop running as well, otherwise how will the code check the time of day? The code will be something like this?
    You don't have to run infinitely loop parallelly. As said above, you can asynchronously call another helper method from on_tick thread, which subscribes or unsubscribe based on required logic.
    def on_tick(*args):
    .......
    helper_method()
    .......

    def helper_method():
    ......
    your condition
    subscribe/unsubscribe token/list of token
    ......
    You can check threaded example here.
  • CE7727
    Thanks, will try this out and see how it progresses.
Sign In or Register to comment.