Also, don't use threaded=True when connecting websocket if you don't need it. Otherwise,you have to use a infinite while loop to re-define the functions and assign callbacks. And websocket.connect() should be after assigning the callbacks not just after initializing websocket.
The issue is you have created another daemon thread using threaded = True, but that thread is not handled properly using initialization and callbacks. If you don't need another ticker thread, you can avoid assigning threaded=True and use main ticker thread as shown in example. Refer to FAQs here for all python WebSocket-related queries.
I am using a while loop, and I have inserted all the relevant pieces of code in it. But the websocket KiteTicker class can be defined outside the while loop right?
File "C:\Users\surya\Downloads\Documents\trade\trade.py", line 10, in computation if ticks['instrument_token'] == nse[i]: builtins.TypeError: list indices must be integers or slices, not str
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake) INFO:root:Session Terminated: connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
Currently I am getting this now, and seems to be a combination of things. Any directives so as to what is happening exactly? Am I passing the retrieval of the instrument_token value from ticks dictionary in the wrong manner?
You are not processing the ticks correctly. I told you that ticks is a list of dictionaries. So,it should be not ticks['instrument_token']==nse[i].
It should be like:
for tick in ticks:
if tick['instrument_token']==nse[i]:
pass
Dictionaries cannot be called like this. for tick in ticks: if tick['instrument_token']==nse[i]: pass It has to be called like this: for key, value in ticks.items(): pass
for k,v in dict.items() is used when you have to iterate over all the keys and values of a dictionary. In your case,you only need the instrument_token from every dictionary(tick) in the ticks. So,it will be called like i told in my above comment. The ticks received from websocket is a list of dictionaries,not a dictionary.
websocket
properly? Make sure, you are assigning callbacks at end. Refer the sample websocket code here.from kiteconnect import KiteTicker
from Connect import data, kite, loginflow
logging.basicConfig(level = logging.DEBUG)
websocket = KiteTicker(api_key = loginflow['api_key'], access_token = data['access_token'], reconnect_max_delay = 5)
websocket.connect(threaded = True)
def on_connect(ws, response):
logging.info(f'Connection Established: {response}')
def on_order_update(ws, data):
logging.info(f"Order I.D.: {data['order_id']}")
def on_error(ws, reason):
logging.error(f'Error: {reason}')
def on_reconnect(ws, attempts_count):
logging.warn(f'Reconnecting: {attempts_count}.')
def on_noreconnect(ws):
logging.error(f'Error 404.')
def on_close(ws):
logging.info(f'Session Terminated')
websocket.on_connect = on_connect
websocket.on_order_update = on_order_update
websocket.on_error = on_error
websocket.on_reconnect = on_reconnect
websocket.on_noreconnect = on_noreconnect
websocket.on_close = on_close
keyvalue = {'TRIDENT': [2479361], 'UNIONBANK':[2752769], 'IDBI': [377857], 'BHANSALI':[107265], 'IEX': [56321]}
websocket.subscribe([keyvalue['TRIDENT'][0], keyvalue['UNIONBANK'][0], keyvalue['IDBI'][0], keyvalue['BHANSALI'][0], keyvalue['IEX'][0]])
This is the code
threaded = True
, but that thread is not handled properly using initialization and callbacks. If you don't need another ticker thread, you can avoid assigningthreaded=True
and use main ticker thread as shown in example.Refer to FAQs here for all python WebSocket-related queries.
if ticks['instrument_token'] == nse[i]:
builtins.TypeError: list indices must be integers or slices, not str
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
INFO:root:Session Terminated: connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
Currently I am getting this now, and seems to be a combination of things. Any directives so as to what is happening exactly?
Am I passing the retrieval of the instrument_token value from ticks dictionary in the wrong manner?
It should be like:
for tick in ticks:
if tick['instrument_token']==nse[i]:
pass
for tick in ticks:
if tick['instrument_token']==nse[i]:
pass
It has to be called like this:
for key, value in ticks.items():
pass