Exceptions when getting ltp using kite.ltp()

pracas
I have a web application which logs tick data of select instruments using a websocket. This works fine. Now i'm trying to add a feature to get one off LTP's of certain instruments using the kite.ltp() function. I'm using the function in the following format kite.ltp("NSE:RELIANCE")
This seems to work sometimes and throw the following exception at other times.
"kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`."

If i catch the exception and retry the function it seems to work sometimes but not every time.
while((retry_count < 3 )and(ltp == 0)):
try:
ltp = kite.ltp(symbol)[symbol]['last_price']
except Exception:
log("get ltp exception" + str(Exception.message))
if(ltp == 0):
retry_count = retry_count + 1
time.sleep(1)
return ltp
While i try the above LTP function, the websocket is not running. I would ideally like to run the websocket in the background and this function once in a while in the foreground. I expect the kite.ltp function to work everytime or not work at all.

What would be the best way to handle this? Am i missing something here? Say i create a access_token everytime i try to get the ltp, will my websocket running in the background stop because of the access_token change?
  • rakeshr
    @pracas
    We ran WebSocket and ltp quote call, in parallel for more than 15 minutes, didn't faced any issue.Are you generating multiple access_token individually for websocket and ltp quote call ?
  • Imran
    hii pracas
    "will my websocket running in the background stop because of the access_token change"

    if you change the access token then .......many a times websocket will stop ...(and sometimes not also).
  • pracas
    @rakeshr sorry about the delayed response..

    nope using the same access_token... access_token is getting generated, a thread for a websocket is started and every time a new script needs to be added to the websocket subscription, the script LTP and INSTRUMENT ID are first retrieved using the ltp function.

    the ltp function like i mentioned seems to be working sometimes when tried multiple times.
    Also it did not work for some specific scripts (APOLLOTYRE) during that test day. Will run the tests again on monday and update.

    In the meanwhile what if the script requested has not been traded that day, what will be returned? at the backend how does the ltp function fetch the price? could it be that its picking the ltp from the last tick and that there was no tick generated for that script recently?
  • Kathir85
    I am in the processing of automating my trading strategy based on technical indicators using KITE API. Please guide me on technical indicators library in Phython
  • rakeshr
    @pracas
    what if the script requested has not been traded that day, what will be returned?
    It will return an empty dictionary.
    At the backend how does the ltp function fetch the price? could it be that its picking the ltp from the last tick and that there was no tick generated for that script recently?
    Every calls of LTP(including all quote call ) is proxied through WebSocket streaming, that has an in-memory data structure for all such calls.So, it reads from map and return the required call.
Sign In or Register to comment.