Fastest way to fetch & store quotes from websocket

KesharDev edited July 5 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,))
def on_connect(ws, response):
ws.set_mode(ws.MODE_FULL, [738561])
def on_close(ws, code, reason):
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
    Yes,the KiteConnect python client handles auto reconnection for you.
    But ,it can't happen if you use 'stop' method as mentioned here:

    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 7
    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.
    Threaded ticker example is given here:

    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 7
    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.

    Thanks a lot, appreciate it.
    SRIJAN edited July 7
    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
  • tahseen
    tahseen edited July 13
    def on_ticks(ws, ticks):
    updateOrdersSQL_thread = Thread(target=updateQuotes,args=(ticks,))
    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.