Python script getting failed in between while fetching the data

VinayAkuthota

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:
---------------

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\PostgresDataLoad.py", line 69, in
kws.connect()
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\kiteconnect-3.7.7-py3.7.egg\kiteconnect\ticker.py", line 534, in connect

File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\base.py", line 1261, in run
self.mainLoop()
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\base.py", line 1270, in mainLoop
self.runUntilCurrent()
--- ---
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\base.py", line 896, in runUntilCurrent
call.func(*call.args, **call.kw)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\internet\tcp.py", 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\tcp.py", line 327, in connectionLost
protocol.connectionLost(reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\protocols\tls.py", line 403, in connectionLost
ProtocolWrapper.connectionLost(self, reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twisted\protocols\policies.py", line 125, in connectionLost
self.wrappedProtocol.connectionLost(reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\autobahn\twisted\websocket.py", line 288, in connectionLost
self._connectionLost(reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\autobahn\websocket\protocol.py", line 3422, in _connectionLost
WebSocketProtocol._connectionLost(self, reason)
File "C:\Users\vinay_akuthota\AppData\Local\Programs\Python\Python37-32\lib\site-packages\autobahn\websocket\protocol.py", 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\websocket.py", 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\ticker.py", 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\ticker.py", line 656, in _on_close

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






My python Code :smile:
-------------------------------------------------------



TOKENS = [54273]


def on_ticks(ws, ticks):
##print(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="127.0.0.1", port="5432")
cur = conn.cursor()
for v_tick in ticks: #cahnge datetime subtraction used for testing, remove price randint
cur.execute(new_insert_tick_statement,(datetime.now(),v_tick['instrument_token'],v_tick['last_price']))
conn.commit();



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

# 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()`
ws.stop(TOKENS)

# 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.
kws.connect()








  • rakeshr
    @VinayAkuthota
    DB insertion cur.execute(new_insert_tick_statement,(datetime.now(),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(datetime.now()) 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)

    @VinayAkuthota
Sign In or Register to comment.