Python script getting failed in between while fetching the data


Hi All,

I get the below error while i am fetching the data using API.

i am getting data for sometime & all of sudden the get the below error and when i re-run the code again it will run fine & will through the same error .. it continues
can anyone please help on resolving the issue.


ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (None)
Unhandled Error
Traceback (most recent call last):
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\", line 69, in
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kiteconnect-3.7.7-py3.7.egg\kiteconnect\", line 534, in connect

File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\", line 1261, in run
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\", line 1270, in mainLoop
--- ---
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\", line 896, in runUntilCurrent
call.func(*call.args, **
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\", line 519, in connectionLost
self._commonConnection.connectionLost(self, reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\", line 327, in connectionLost
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\protocols\", line 403, in connectionLost
ProtocolWrapper.connectionLost(self, reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\protocols\", line 125, in connectionLost
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\autobahn\twisted\", line 288, in connectionLost
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\autobahn\websocket\", line 3422, in _connectionLost
WebSocketProtocol._connectionLost(self, reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\autobahn\websocket\", line 1133, in _connectionLost
self._onClose(self.wasClean, WebSocketProtocol.CLOSE_STATUS_CODE_ABNORMAL_CLOSE, "connection was closed uncleanly (%s)" % self.wasNotCleanReason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\autobahn\twisted\", line 331, in _onClose
self.onClose(wasClean, code, reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kiteconnect-3.7.7-py3.7.egg\kiteconnect\", line 81, in onClose

File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kiteconnect-3.7.7-py3.7.egg\kiteconnect\", line 656, in _on_close

File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\", line 60, in on_close
builtins.TypeError: stop() takes 1 positional argument but 2 were given

My python Code :smile:

TOKENS = [54273]

def on_ticks(ws, ticks):
print("data loading in postgres")
new_insert_tick_statement = "INSERT INTO public.test_ticks(date, token, price) VALUES (%s, %s, %s)"
conn = psycopg2.connect(database="ticks", user="postgres", password="password", host="", port="5432")
cur = conn.cursor()
for v_tick in ticks: #cahnge datetime subtraction used for testing, remove price randint

def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).

# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_LTP, TOKENS)

def on_close(ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`

# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close

# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.

  • rakeshr
    DB insertion cur.execute(new_insert_tick_statement,(,v_tick['instrument_token'],v_tick['last_price'])) seems to be blocking on_tick method call.Can you create a separate thread for db insertion and pass on the tick to that method synchronously.You may check example here.
  • ZI4453
    please try : and except and capture the error...
    My suggestion would to know always raise exceptions_validate errors, if you are not sure of what to expect from 'ticks'.
    consider using 'exchange time stamp' instead of passing on local time to PostgreSQL ,
    i believe data landing time( to client end might not be useful until we try to understand round trip between exchange and client landing.
    threading dont seem to be an issue as script works fine when for rest of data stream. i suspect error to be in handling ticks(converting ticks to your db insertion)

Sign In or Register to comment.