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.
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.
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?
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.
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 and pd.datetime.now().time() < pd.to_datetime(['15:16:00']).time):
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.on_error = on_error
sub_token=get_sub_list() #1st time subscribe all
if pd.datetime.now().time() == pd.to_datetime(['09:30:00']).time:
unsub_token=get_unsub_list() #2nd time unsub few
if pd.datetime.now().time() == pd.to_datetime(['09:45:00']).time:
unsub_token=get_unsub_list() #3rd time unsub few more
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.