webSocket data once in 1 min or 5 min

kiteapi
I did a search on this forum since yesterday night, but could not find an answer, hence the query.

I am trying to build https://kite.trade/forum/discussion/8159/option-chain#latest and as per @sujith 's suggestion I am using webStocket.

While am able to get real time tick data with on_connect, I need the data once in lets say 1 minute or 5 minutes, I understand I can get continuous tick data and then convert them in 1 min or 5 min data, but rather I was wondering if I could just connect and get the data once a min or in 5 mins (as OI is updated once in a min or 5 mins). I assumed that on_close would close the connection after one (1) tick, but I see no difference and see continuous tick data with or without on_close.

Here is what I have tried:

1. I tried adding sleep inside on_ticks - but it does not work with even 5 sec sleep, as TCP connection gets closed and throws the error:

Connection error: 1006 - connection was closed uncleanly (None)
Connection closed: 1006 - connection was closed uncleanly (None)

2. I can use kite.quote as I need data for once in 1 min, but I found that tick data is faster (am sure that is why dev support from Zerodha suggest to use that) and can get data for 80 (I know you support 3000 per user, my req is not that much yet) instruments in 1 second, whereas same would take around 40 seconds with kite.quote.

3. on_close does not close the connection after one tick (may be my interpretation was wrong?), or am using it incorrectly? https://github.com/zerodhatech/pykiteconnect

*******************

def on_ticks(ws, ticks):
print(ticks)
def on_connect(ws, response):
ws.subscribe(list_instru_token)
ws.set_mode(ws.MODE_QUOTE, list_instru_token)
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()
*******************
  • sujith
    In the websocket API, if you don't need ticks for a while then you can just unsubscribe for ticks and subscribe whenever you need it. You shouldn't close connection or hold the thread.

    In Quote API, you can fetch data for multiple items at a time. You can know more here.
  • Simon
    What could be the reason for tick happening only once and stops there? PFB console log and python code.

    Console log
    DEBUG:root:Ticks: [{'tradable': True, 'mode': 'quote', 'instrument_token': 857857, 'last_price': 782.9, 'last_quantity': 2, 'average_price': 0.0, 'volume': 0, 'buy_quantity': 0, 'sell_quantity': 0, 'ohlc': {'open': 788.0, 'high': 791.4, 'low': 779.3, 'close': 782.9}, 'change': 0.0}]


    Python Code
    # 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.
    ws.subscribe([857857])

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_QUOTE, [857857])


    def on_close(ws, code, reason):
    # On connection close stop the main 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()
  • rakeshr
    'average_price': 0.0, 'volume': 0, 'buy_quantity': 0, 'sell_quantity': 0,
    Looks like, you were trying to check this in non-market hours.
    During non-market hours, you would only receive the last cached tick.
  • Simon
    Tried both market and non-market hours, no luck with Python client. But Java client worked like a magic. I'm happy for now that I started receiving ticks without hicks. :smiley:
  • Simon
    Just now checked again from python, and it's working. :smiley: After couple of days struggling to get started, today seems to be awesome. :smiley: Thanks much for prompt reply. Kite API team is doing a wonderful job in democratising APIs.
This discussion has been closed.