Fastest way to fetch & store quotes from websocket

KesharDev
KesharDev edited July 2022 in Python client
I want to store quotes in a global variable. What is the best/fastest way? Also will my code reconnect with websocket, if there is a problem?

I have subscription to around 600 symbols.
websocketQuotes = {}
def updateQuotes(ticks):
global websocketQuotes
global instrument_symbol_dic

for tick in ticks:
tradingSymbol = instrument_symbol_dic[int(tick["instrument_token"])]
websocketQuotes[tradingSymbol] = tick
def on_ticks(ws, ticks):
updateOrdersSQL_thread = Thread(target=updateQuotes,args=(ticks,))
updateOrdersSQL_thread.start()
def on_connect(ws, response):
ws.subscribe([738561])
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)
  • SRIJAN
    Yes,the KiteConnect python client handles auto reconnection for you.
    But ,it can't happen if you use 'stop' method as mentioned here:

    https://github.com/zerodha/pykiteconnect#websocket-usage

    https://github.com/zerodha/pykiteconnect/blob/20dbfd0152267b8da2a5afa17b737ca847b61814/kiteconnect/ticker.py#L342

    If you want to just store quotes ,use threaded ticker,and call updateQuotes directly,rather than starting new thread for every tick.
  • KesharDev
    KesharDev edited July 2022
    Can you please share 1 example of threaded ticker @SRIJAN

    Currently, according to my comparision. Quote is faster than Websocket by 0.3 second on an average. But in theory, it can't be. So probably my code is wrong.
  • SRIJAN
    Threaded ticker example is given here:
    https://kite.trade/forum/discussion/comment/25535/#Comment_25535

    And as explained multiple times, it's impossible for market quotes to return a tick before websocket as quote API just fetches the latest tick from websocket. If a tick has not been streamed on the websocket,how will quote API fetch that tick, something that doesn't exist??
  • KesharDev
    KesharDev edited July 2022
    Sorry to bother you.

    Yes @SRIJAN I understood, it is impossible. But my code is probably wrong, and somehow quote is fetching the results faster. I am sure, once I fix my code, websocket will be faster as expected.

    The main difference here, I guess is. The quote is fetched from websocket on Kite server. But the websocket on my side is slower maybe? Since i did not implement it correctly.

    Can you please try to run my code, and see, If "websocket" is able to outperform quote by comparing "last_trade_time"?

    You just need to edit line 1 and 2, to use your own API. and u can just run the code and see the results.

    Or you can just read the code and let me know, whats wrong.


    https://drive.google.com/file/d/1vHALJQKxuFekIz90_sIClkSFWSfpvDJU/view?usp=sharing

    Thanks a lot, appreciate it.
  • SRIJAN
    SRIJAN edited July 2022
    The continuous polling of quotes is delaying ticks,and making some ticks miss too.

    So,your observation that market quotes are faster than websocket ticks is just an illusion.
  • KesharDev
    Do you know how I can fix it in Python? How to code it properly?

    I am not understanding this example properly https://kite.trade/forum/discussion/comment/25535/#Comment_25535
  • tahseen
    tahseen edited July 2022
    @KesharDev
    def on_ticks(ws, ticks):
    updateOrdersSQL_thread = Thread(target=updateQuotes,args=(ticks,))
    updateOrdersSQL_thread.start()
    You are initializing and starting thread inside on_ticks, which is not correct. You are fundamentally launching a new thread every time a request of ticks come. Bad idea to be honest

    Use python queue
    Initial updateQuotes as thread in main program. And inside it you pass ticks to it via queue

    And in the on_ticks method, you push ticks to this queue

Sign In or Register to comment.