We used the websocket and i want to connect and close the websocket.
def on_connect(ws, response):
ws.subscribe([instrument_token])
ws.set_mode(ws.MODE_FULL, [instrument_token])
def on_close(ws, code, reason):
print("Connection closed:", code, reason)
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.connect()
#want to connect the websocket at 9:15 AM to start collecting data
while True:
current_time = datetime.now().time()
if current_time.hour == 9 and current_time.minute == 15:
break
#want to stop collecting data and to close the websocket connection at 15:30 AM
while True:
current_time = datetime.now().time()
if current_time.hour == 10 and current_time.minute >= 20:
kws.close()
print("Connection closed")
break
by above code i can't connect and close the websocket automatically at a given time, can you share the solution with some example.
April 12