ReactorNotRestartable : Web Socket

satheesh7134
Issue

Hi Everyone

I ran the below code to generate tick data for one of the tokens that I am interested in, I didn't get any output on the console. So I stopped the execution and when it was stopped I got this message "connection error - 1006 - connection was closed uncleanly".

Ever since I got that message I am not able to run the code again successfully. When I try to run the code now, I am getting ReactorNotRestartable now error message. I would need help to stop the Websocket in a proper way and re-start it whenever there is a issue with connection or whenever I feel so.

If someone can help that would be great


Code
from kiteconnect import KiteTicker
import os
import csv

#cwd = os.chdir("E:\\Algorthmic Trading\\Zerodha_Training")

class Streaming_Ticks:

def __init__(self):

access_token = open("access_token.txt",'r').read()
key_secret = open("key_info.txt",'r').read().split()
self.kite = KiteConnect(api_key=key_secret[0])
self.kite.set_access_token(access_token)
self.kws = KiteTicker(key_secret[0],self.kite.access_token)

def on_ticks(ws,ticks):
# Callback to receive ticks.
#logging.debug("Ticks: {}".format(ticks))
print(ticks)

def on_connect(ws,response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
#logging.debug("on connect: {}".format(response))
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL,tokens) # Set all token tick in `full` mode.
#ws.set_mode(ws.MODE_FULL,[tokens[0]]) # Set one token tick in `full` mode.

if __name__ == "__main__":

cwd = os.chdir("E:\\Algorthmic Trading\\Zerodha_Training")

tick_data = Streaming_Ticks()
token_list= []

with open('parameter_file.csv') as param_file:
param_reader = csv.DictReader(param_file, )
for row in param_reader:
token_list.append(int(row['token']))
print(token_list)

tick_data.kws.on_ticks = tick_data.kws.on_ticks
tick_data.kws.on_connect = tick_data.kws.on_connect
tick_data.kws.connect()

Error Message

File "C:\Users\Satheesh\AppData\Local\Temp\ipykernel_20600\3949352367.py", line 1, in
tick_data.kws.connect()

File "C:\Users\Satheesh\anaconda3\envs\ztraining\lib\site-packages\kiteconnect\ticker.py", line 536, in connect
reactor.run(**opts)

File "C:\Users\Satheesh\anaconda3\envs\ztraining\lib\site-packages\twisted\internet\base.py", line 1314, in run
self.startRunning(installSignalHandlers=installSignalHandlers)

File "C:\Users\Satheesh\anaconda3\envs\ztraining\lib\site-packages\twisted\internet\base.py", line 1296, in startRunning
ReactorBase.startRunning(cast(ReactorBase, self))

File "C:\Users\Satheesh\anaconda3\envs\ztraining\lib\site-packages\twisted\internet\base.py", line 840, in startRunning
raise error.ReactorNotRestartable()

ReactorNotRestartable
  • satheesh7134
    Hi Srijan

    I checked my instrument token, it is a correct one. I am able to fetch the historical data based on this token. I guess I need to stop the Reactor, before I can re-run my code. I am not sure how to do this in Windows.
  • SRIJAN
    SRIJAN edited July 2022
    The instrument token solution was for the websocket error of parsing tokens.

    Your issue has been discussed in the first part of the thread. You can use the default python setup instead of anaconda environment as told in that thread.
  • satheesh7134
    Hi Srijan

    Noted, will try and hopefully it works
  • satheesh7134
    Hi Srijan

    One more thing, the code is not pulling any data. The console is blank, can you please check and let me know if I had made any mistake to the callback function
  • SRIJAN
    SRIJAN edited July 2022
    The code looks fine at the first glance.

    Try to run just a simple websocket code,like shown here and check :
    https://github.com/zerodha/pykiteconnect#websocket-usage

    If there is any problem when running sample code,you might ask here.

    Otherwise,you have to debug your code yourself.
    We don't provide programming support on this forum.
  • satheesh7134
    Code that works


    from kiteconnect import KiteConnect
    from kiteconnect import KiteTicker
    import os
    import csv


    #cwd = os.chdir("E:\\Algorthmic Trading\\Zerodha_Training")

    class Streaming_Ticks:

    def __init__(self, token_list):

    self.access_token = open("access_token.txt",'r').read()
    self.key_secret = open("key_info.txt",'r').read().split()
    self.kite = KiteConnect(api_key=self.key_secret[0])
    self.kite.set_access_token(self.access_token)

    self.tokens = token_list

    def on_ticks(self,ws,ticks):
    # Callback to receive ticks.
    #logging.debug("Ticks: {}".format(ticks))
    print(ticks)

    def on_connect(self, ws,response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    #logging.debug("on connect: {}".format(response))
    ws.subscribe(self.tokens)
    ws.set_mode(ws.MODE_FULL,self.tokens) # Set all token tick in `full` mode.
    #ws.set_mode(ws.MODE_FULL,[tokens[0]]) # Set one token tick in `full` mode.

    def get_TickData(self):

    kws = KiteTicker(self.key_secret[0],self.access_token)
    kws.on_ticks = self.on_ticks
    kws.on_connect = self.on_connect
    kws.connect()

    if __name__ == "__main__":

    cwd = os.chdir("E:\\Algorthmic Trading\\Zerodha_Training")


    token_list= []

    with open('parameter_file.csv') as param_file:
    param_reader = csv.DictReader(param_file, )
    for row in param_reader:
    token_list.append(int(row['token']))

    tick_data = Streaming_Ticks(token_list)
    tick_data.get_TickData()
  • AlgoTraderXYZ
    Hi satheesh,
    Are you running in juptyer notebook? Just restart the kernel.
  • satheesh7134
    Hi AlgotraderXYZ

    No, I am using Spyder

    The restart works.
  • satheesh7134
    Hi Srijan

    The code that I pasted works now. Figured it out in the end

    I am getting the tick data

    Thanks
Sign In or Register to comment.