raise error.ReactorNotRestartable() ReactorNotRestartable

mukulietlucknow
hi guys,

I am using the two approaches to stream the data but nothing is working. -

Approach 1
+++++++++++++++++++++++++++++++++++
import json
import logging
from kiteconnect import KiteTicker
import os


cwd = os.chdir("D:\Investments\AlgoTrading")

#generate trading session
access_token = open("access_token.txt",'r').read()
key_secret = open("api_key.txt",'r').read().split()

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

api_key = key_secret[0]
access_token = access_token


# Initialise.
kws = KiteTicker(api_key, access_token, debug=True)
tokens = [53501959]
print("Tokens length", len(tokens))


# Callback for tick reception.
def on_ticks(ws, ticks):
print(ticks)


# Callback for successful connection.
def on_connect(ws, response):
logging.debug("on connect: {}".format(response))
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL, tokens)


def on_close(ws, code, reason):
logging.error("closed connection on close: {} {}".format(code, reason))


def on_error(ws, code, reason):
logging.error("closed connection on error: {} {}".format(code, reason))


def on_noreconnect(ws):
logging.error("Reconnecting the websocket failed")


def on_reconnect(ws, attempt_count):
logging.debug("Reconnecting the websocket: {}".format(attempt_count))


def on_order_update(ws, data):
print("order update: ", data)


# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.on_error = on_error
kws.on_noreconnect = on_noreconnect
kws.on_reconnect = on_reconnect
kws.on_order_update = on_order_update

# kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)

# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
# kws.connect(disable_ssl_verification=True)
kws.connect()
+++++++++++++++++++++++++++++++++++++++++++++++++


Approch 2 -

from kiteconnect import KiteConnect
from kiteconnect import KiteTicker
import pandas as pd
import os

cwd = os.chdir("D:\Investments\AlgoTrading")

#generate trading session
access_token = open("access_token.txt",'r').read()
key_secret = open("api_key.txt",'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
kite.set_access_token(access_token)

#get dump of all NSE instruments
instrument_dump = kite.instruments("NSE")
instrument_df = pd.DataFrame(instrument_dump)

def tokenLookup(instrument_df,symbol_list):
"""Looks up instrument token for a given script from instrument dump"""
token_list = []
for symbol in symbol_list:
token_list.append(int(instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0]))
return token_list

#####################update ticker list######################################
tickers = ["INFY"]
#############################################################################

#create KiteTicker object
kws = KiteTicker(key_secret[0],kite.access_token)
tokens = tokenLookup(instrument_df,tickers)

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.


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


++++++++++++++++++++++++++++++++++++

In both cases, I am getting the following error -

2021-05-10 10:34:08,287 - kiteconnect.ticker - DEBUG - Start WebSocket connection.
Tokens length 1
2021-05-10 10:34:08+0530 [-] Log opened.
Traceback (most recent call last):

File "D:\Investments\AlgoTrading\sample.py", line 79, in
kws.connect()

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

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

File "C:\Users\91701\anaconda3\lib\site-packages\twisted\internet\base.py", line 1262, in startRunning
ReactorBase.startRunning(self)

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

ReactorNotRestartable

++++++++++++++++++++++++++++++++++++++

please, someone, help me, thanks in advance

  • sujith
    You can refer to the pykiteconnect FAQs here.
  • mukulietlucknow
    mukulietlucknow edited May 2021
    @sujith thanks for the reply. from the link I got some code info now
    I used this code below -
    +++++++++++++++++++
    from kiteconnect import KiteTicker
    import os

    cwd = os.chdir("D:\Investments\AlgoTrading")
    access_token = open("access_token.txt",'r').read()
    key_secret = open("api_key.txt",'r').read().split()

    api_key = key_secret[0]
    access_token = access_token
    kws = KiteTicker(api_key,access_token)

    def on_ticks(ws,ticks):
    print(ticks)
    print("\n")

    # token i am using here is of TCS
    def on_connect(ws,response):
    ws.subscribe([2953217])
    ws.set_mode(ws.MODE_QUOTE,[2953217])

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

    ++++++++++++++++++++++++++++++++

    I am getting the still the same error.

    I am also sure that my acces_token and api_key is correct
  • mukulietlucknow
    it looks like there is no issue in the code, is there any settings that I need to set somewhere.

    and can anyone tell me what could be the way to react to the Zerodha tech support?
  • rakeshr
    File "C:\Users\91701\anaconda3\lib\site-packages\twisted\internet\base.py", line 765, in startRunning
    raise error.ReactorNotRestartable()
    Looks like your local development environment issue i.e on your anaconda setup.
    Refer to FAQs on anaconda setup here.
  • mukulietlucknow
    I found the solution is related to the setup. for setting up the TCP connection in jupyter or spider. we need to restart our kernel then it will work fine.
Sign In or Register to comment.