Correctness of the example threaded websocket code

soumyadeep
https://gist.github.com/vividvilla/db62e69af44831a6c4126ed193ea5214
Is the example code correct because no on_error or on_close callbacks have been assigned.
Also in the lines below-
count = 0
while True:
count += 1
print("This is main thread. Will change webosocket mode every 5 seconds.")
if count % 2 == 0:
if kws.is_connected():
ws.subscribe(tokens)
print("Set mode to LTP")
kws.set_mode(kws.MODE_LTP, tokens)
else:
if kws.is_connected():
ws.subscribe(tokens)
print("Set mode to quote")
kws.set_mode(kws.MODE_QUOTE, tokens)

time.sleep(5)
Dont we have to resubscribe tokens ? so that the code becomes-
count = 0
while True:
count += 1
print("This is main thread. Will change webosocket mode every 5 seconds.")
if count % 2 == 0:
if kws.is_connected():
print("Set mode to LTP")
kws.set_mode(kws.MODE_LTP, tokens)
else:
if kws.is_connected():
print("Set mode to quote")
kws.set_mode(kws.MODE_QUOTE, tokens)

time.sleep(5)
  • Vivek
    Vivek edited August 2017
    Yeah it wasn't assigned to callback so it won't get called on error or connection close. If the connection is same and you have already subscribed once then you don't have to resubscribe. You just need to change the mode.

    PS: I have updated the gist
  • soumyadeep
    soumyadeep edited August 2017
    Also in the example websocket script, where do we write the I/O, file reading/writing codes? If you could please indicate in the example scripts: "##Write your calculations/strategies etc here:"
    For me, I would like to write streaming data in a file with minor calculations, for eg. making 1 min candles.
    That will be much helpful.
    Thanks
  • soumyadeep
    @Vivek need your expert advice please
Sign In or Register to comment.