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.
Sign In or Register to comment.