I have a websocket connection which only listens to order updates i.e. kws.on_order_update callback. It is not listening to ticks. How do I close this websocket after 3:30 PM? Since there would be no order updates after 3:30PM, I am unable to call ws.stop after market hours
You can have a multi-threaded setup, which keeps checking for the current time in a loop and closes the websocket connection based on an condition. Eg:
..... your_websocket_code ..... kws.connect(threaded=True) while True: if (current_time > market_close_time): kws.stop() # Maybe add some sleep as well to avoid a high resource-consuming continuous loop
Eg: