App type: Connect (paid, active subscription). API key: XXXXX. Login (generate_session) succeeds and HTTP APIs (instruments, profile) work fine with the resulting access_token. But KiteTicker WebSocket connection always fails with: 1006 — connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden)). Tested on 3 different networks (home broadband, SIM-router, mobile hotspot) — same error on all. kiteconnect library is latest (5.2.2). Could you please check if WebSocket streaming is enabled for this app/API key on your end?
Please verify that you are using a Kite Connect (paid) app's API_Key. Please generate a fresh access_token for the same API_Key and check. You may refer to a similar discussion here.
As per the new algo regulations, a static IP is required for order placement only. The WebSocket market data streaming and other endpoints, like order book and positions can still be accessed from any IP address.
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([128083716, 128104964])
# Set RELIANCE to tick in `full` mode. ws.set_mode(ws.MODE_FULL, [128104964])
def on_close(ws, code, reason): # On connection close stop the main loop # Reconnection will not happen after executing `ws.stop()` print(f"Closed: {code} {reason}") ws.stop()
Please review the WebSocket example code and ensure you are using the correct instrument token from the instrument file to check for any errors. Additionally, you can refer to the Python WebSocket streaming FAQs for further guidance.
from kiteconnect import KiteTicker
logging.basicConfig(level=logging.DEBUG)
# Your actual credentials
API_KEY = "---"
ACCESS_TOKEN = "---" # today's token -- regenerate if this has expired
# Initialise
kws = KiteTicker(API_KEY, ACCESS_TOKEN)
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([128083716, 128104964])
# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, [128104964])
def on_close(ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`
print(f"Closed: {code} {reason}")
ws.stop()
def on_error(ws, code, reason):
print(f"Error: {code} {reason}")
# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.on_error = on_error
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()
is there any fault in this simple code