'Zerodha KiteConnect API producing 1006 Error

I am building a dashboard for live tickers

I am using zerodha Kiteconnect to fetch SE Options & Futures live data for that ... The client for whom I am building the dashboard has provided me the api key, and also provides me the access token daily .... The situation is he uses the same credentials in his own program (that he runs separately in his own laptop) to fetch live ticker data

When I use KiteConnect to fetch instrument dump it works, below code executes successfully,

  
   from kiteconnect import KiteTicker, KiteConnect

   access_token = '*********' # changes every day
   api_key = '*********'

   kite = KiteConnect(api_key=key_secret, access_token = access_token)
   instrument_list = kite.instruments(exchange=kite.EXCHANGE_NFO)

but when I use KiteTicker (WebSocket Streaming) with same credentials as shown in code below it produces 1006 Connection error:

kws = KiteTicker(api_key, access_token=kite.access_token)

####### define the callbacks ############

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

    # Set tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, instrument_tokens)


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

    # close the connection after some time
    if (time.time() - begin_time) > 120: # run for 2 minutes
        write_json(ticks_list)
        print("close called")
        ws.close()


def on_close(ws, code, reason):
    # On connection close stop the event loop.
    # Reconnection will not happen after executing `ws.stop()`
    print("-------- Stopping ------------")
    ws.stop()
    print("--------- Stopped ----------")


### define
ticks_list = [] # will hold list of JSON objects
##### Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close

begin_time = time.time()

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

The exact error produced is:

Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)

Can you please guide me as to why is this happening? Also is it possible to use the same credentials in parallel by two different programs from different IP to fetch live tick data...

Thanks



Solution 1:[1]

Are you placing an order inside on_tick method? you shouldn't be putting any logic inside on_tick thread.You need to pass on the tick on another method asynchronically without blocking on_tick thread.

There are two ways to pass tick data from on_tick thread to perform any operation without blocking the main on_ticks thread.
1> You can push tick data to Queue(use celery,rq,etc) as task queue manager and have another method that reads these Queue data and perform the tasks. eg:

def on_ticks(ws,ticks):
    #Pass the tick data to Queue using celery,rq,etc
    #Using celery delay method here to call helper_method task
    helper_method.delay(ticks)

def helper_method(ticks):
    #Perform require operation using tick data

2>Create another thread and perform the required operation in 2nd thread using threaded=True P.S: Don't forget to assign ticker callback for new thread.

import logging
from kiteconnect import KiteTicker

logging.basicConfig(level=logging.DEBUG)

kws = KiteTicker("your_api_key", "your_access_token")

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

def on_connect(ws, response):
    ws.subscribe([738561, 5633])
    ws.set_mode(ws.MODE_FULL, [738561])

def on_close(ws, code, reason): 
    ws.stop()

kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.connect(threaded=True)

while True:
   #Perform required data operation using tick data
   def on_ticks(ws, ticks):
       ..........
       helper_method(ticks)
       .........

   def helper_method(ticks):
       .........
       Perform computation here
       ........
  #Assign callback
  kws.on_ticks=on_t

Solution 2:[2]

I was getting the same error while using KiteConnect WebSocket, So the issue in the tokens list - so first you have to change token list values type in Int.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TradeHull
Solution 2 Krishna Gupta