I was facing the problem of frequent socket timeout. to solve it, Inside on_close() function, I am restarting the main function again but it triggers a new error. twisted.internet.error.ReactorNotRestartable: Any idea how to solve it?
@saurabh3679 No, need to restart socket connection again from on_close.You can just remove call back for on_close and socket will be re-connected. Also, frequent disconnection with time-out shouldn't happen, can you paste your complete WebSocket code here?
Once connected, on_ticks() function works. Here is the on_tick() function. It passes the dataframe to a thread function my_algo where I do the calculations on the dataframe and detect if I need to place the order or not
def on_connect(ws, response): global token ws.subscribe([token]) ws.set_mode(ws.MODE_FULL, [token]) logger.info("Connected to the kiteconnect server. Server response {}".format(response))
This is the on_close() function in my script
def on_close(ws, code, reason): global readyForOrder logger.critical("Server closed the connection!") readyForOrder = True logger.critical("********** Reconnecting to Websocket*************") kws2 = KiteTicker(api_key, access_token, reconnect_max_tries=5, reconnect_max_delay=5, connect_timeout=5) # Assign the callbacks. kws2.on_ticks = on_ticks kws2.on_connect = on_connect kws2.on_close = on_close kws2.connect()
@saurabh3679 Any unhandled exception in your code will result in on_error which in turn calls on_close. If you need the reconnection logic to kick in, just don't use ws.stop() inside on_close. So, not need of Ticker assignment and method callbacks again inside on_close method.You can just remove it.
No, need to restart socket connection again from on_close.You can just remove call back for on_close and socket will be re-connected.
Also, frequent disconnection with time-out shouldn't happen, can you paste your complete WebSocket code here?
python algotrading.py live <stock code>
This is the live function inside algotrading.py Once connected, on_ticks() function works. Here is the on_tick() function. It passes the dataframe to a thread function my_algo where I do the calculations on the dataframe and detect if I need to place the order or not This is the on_connect() function in my script This is the on_close() function in my script
Will test it today when the market opens
any suggestions what to do @rakeshr
Any unhandled exception in your code will result in on_error which in turn calls on_close. If you need the reconnection logic to kick in, just don't use ws.stop() inside on_close. So, not need of Ticker assignment and method callbacks again inside on_close method.You can just remove it.
Please share the correct sample ticker example as given links are not working
https://github.com/zerodhatech/pykiteconnect/blob/kite3/examples/threaded_ticker.py
@saurabh3679
You can check sample ticker example here.