KiteTicker Thread Issue

Gafoor
Hi @sujith

I am trying to print ltp price of the symbol for which positions is open.
I want the program to keep on checking for active symbol and print there ltp.
The below program fails if there is a change in position symbol.
I think so the kws.close() is giving the issue

instrument_token_list = get_poisition_instrument_list(kite)
kws = KiteTicker(api_key, access_token)
ltp = LastTradePrice(tokens=instrument_token_list)
kws.on_ticks = ltp.on_ticks
kws.on_connect = ltp.on_connect
kws.on_close = ltp.on_close
# kws.connect()
kws.connect(threaded=True, disable_ssl_verification=False)
# count = 0
while True:

new_instrument_token_list = get_poisition_instrument_list(kite)

if instrument_token_list != new_instrument_token_list:
instrument_token_list = new_instrument_token_list
kws.close()
print("New Position Updated")
time.sleep(2)
ltp = LastTradePrice(tokens=instrument_token_list)
kws.on_ticks = ltp.on_ticks
kws.on_connect = ltp.on_connect
kws.on_close = ltp.on_close
kws.connect(threaded=True, disable_ssl_verification=False)

time.sleep(0.5)
  • rakeshr
    @Gafoor
    instrument_token_list = new_instrument_token_list
    kws.close()
    Why are you calling close method here? This will close the running websocket connection.
  • Gafoor
    I want to get data for new list
  • sujith
    If you want data for new tokens then just call subscribe again, you don't have to close connection for that.
    If you don't want data for existing tokens, then you can unsubscribe for old tokens and then call subscribe for new tokens.
  • Gafoor
    Hi @sujith

    I basically want to get the ticker data for only active positions. SO is there a better way to handle it?
    My approach is currently as follow
    > Make a new connection with the list of the current positions. (If the position is empty then the empty token list will be passed because of which there is an error)
    > Continuously check for the active positions and if the previous position and current position is different then unsubscribe the previous token list and subscribe to the new list.

    And this approach is not working properly.
  • sujith
    @Gafoor,
    You can't subscribe to an empty list of tokens. You need to add the check.
    The correct way to do this is to maintain a list of tokens at your end before doing subscribe and call unsubscribe for the ones that are being removed from that list.
Sign In or Register to comment.