ltp error while fetching from API for option contract

chethanthewarrior
while fetching ltp for option contract, NIFTY21O2118200PE(instrument_token: 8987138) using API call kite.ltp(instrument_token)[str(instrument_token)]['last_price']
multiple attempts to fetch ltp at intervals of 0.2s failed, which broke the flow of algo.

last_traded_price error, retry, retry count 0
last_traded_price error, retry, retry count 1
last_traded_price error, retry, retry count 2
last_traded_price error, retry, retry count 3
last_traded_price error, retry, retry count 4
last_traded_price error, retry, retry count 5
last_traded_price error, retry, retry count 6
last_traded_price error, retry, retry count 7
last_traded_price error, retry, retry count 8
last_traded_price error, retry, retry count 9

what is the workaround this!? why is the failure to fetch data observed? this issue is observed frequently
  • sujith
    You need to use Websocket API and not poll the HTTP ltp API.
    You can know more here.
  • chethanthewarrior
    I understand that I need to use Websocket. Does that mean other API calls are not reliable?
    Also, I need the price of the option contract for one time and I feel it's logical to just call the ltp API the time I need the price rather than continuously stream the data. Is my understanding wrong?
    I am concerned about the error to fetch the last_traded_price of an instrument using ltp API.

    I am not polling ltp API continously. I am doing it only when there is error to fetch data

    while retry_count < 10 and last_traded_price == 0:
    try:
    last_traded_price = kite.ltp(instrument_token)[str(instrument_token)]['last_price']

    except KeyError:
    print("last_traded_price error, retry, retry count %d" % retry_count, flush=True)

    if last_traded_price == 0:
    retry_count += 1
    time.sleep(0.2)
  • sujith
    Quote and LTP APIs are limited to 1 request/second. You may either club the instrument tokens and send one request for all at once or use Websocket API.

    You can refer to the API rate limits in the FAQs.
  • chethanthewarrior
    Hi Sujith,
    "Quote and LTP APIs are limited to 1 request/second"
    -> I am certain there was no prior request for LTP for any instrument at least in the last 5 minutes from the point of failure to fetch the ltp.
    -> will change code to wait for 1s to retry after an exception is seen but this does not validate the failure observed.
  • chethanthewarrior
    "You may either club the instrument tokens and send one request for all at once or use Websocket API"
    -> I told in the prior reply that my requirement for ltp was one time event as per my code logic, and that requirement might not arise in the foreseeable future.
    "Also, I need the price of the option contract for one time and I feel it's logical to just call the ltp API the time I need the price rather than continuously stream the data. Is my understanding wrong?"
    You recommend having an active Websocket connection for this scenario?
  • rakeshr
    last_traded_price error, retry, retry count 0
    last_traded_price error, retry, retry count 1
    last_traded_price error, retry, retry count 2
    .....
    Can you paste the error stack trace that occurred, instead of your internal debug message?
    You can use Quote APIs, but make sure you are within the prescribed rate limit.
  • chethanthewarrior
    My bad, I did not log the exception message, will do that the next time when I observe this.

    "You can use Quote APIs, but make sure you are within the prescribed rate limit"
    -> Thank you, noted!
  • chethanthewarrior
    KeyError Exception while ltp is fetched for instrument token: 9368066. Option Contract: NIFTY21OCT18250CE
    Time when last_traded_price is called: 2021-10-26 15:04:59.41
    last_traded_price = kite.ltp(instrument_token)[str(instrument_token)]['last_price']
    KeyError: '(9368066,)'

    When the same is fetched using historical_data API:
    Input Exception: kiteconnect.exceptions.InputException: invalid token

  • chethanthewarrior
    Ok, looks like I caught the issue. A comma was getting inserted to the instrument token when ltp was called from a particular function, due to a bug in the code.
    I sense this invalidated the instrument token.
This discussion has been closed.