kws.connect() kiteconnect.ticker:Connection error: 1006 (or) twisted.internet.error.ReactorNotRestar

TRYRH
Able to connect to KITE API and perform few API commands.
====================================

>>> kite.ltp('NSE:TATAINVEST-BE')
DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /quote/ltp?i=NSE%3ATATAINVEST-BE HTTP/1.1" 200 None
{'NSE:TATAINVEST-BE': {'instrument_token': 1698817, 'last_price': 7198.95}}
>>>

However, Unable to get tick data. kws.connect() is erroring out. how to overcome this ?
=========
>>> # Assign the callbacks.
>>> kws.on_ticks = on_ticks
>>> kws.on_connect = on_connect
>>> kws.on_close = on_close
>>>
>>> kws.connect()
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
>>>
>>> kws.connect()
Traceback (most recent call last):
File "", line 1, in
File "/Users/arulkuma/anaconda3/envs/zapi/lib/python3.12/site-packages/kiteconnect/ticker.py", line 537, in connect
reactor.run(**opts)
File "/Users/arulkuma/anaconda3/envs/zapi/lib/python3.12/site-packages/twisted/internet/base.py", line 693, in run
self.startRunning(installSignalHandlers=installSignalHandlers)
File "/Users/arulkuma/anaconda3/envs/zapi/lib/python3.12/site-packages/twisted/internet/base.py", line 930, in startRunning
raise error.ReactorNotRestartable()
twisted.internet.error.ReactorNotRestartable
>>>

Code below:
=========

import logging
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker

logging.basicConfig(level=logging.DEBUG)
api_key=""
api_secret=""
changing_unique_session_id=""

kite = KiteConnect(api_key=api_key)

data = kite.generate_session(changing_unique_session_id, api_secret=api_secret)
kite.set_access_token(data["access_token"])


# Initialise
kws = KiteTicker(zapi_key, changing_unique_session_id)

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

def on_connect(ws, response):
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe([787969, 1698817]) #SHANTIGEAR, TATAINVEST-BE
# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, [1698817])

def on_close(ws, code, reason):
# On connection close stop the event loop.
# Reconnection will not happen after executing `ws.stop()`
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.
kws.connect()
Tagged:
Sign In or Register to comment.