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.
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']
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.
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.
"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?
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.
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
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.
You can know more here.
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)
You can refer to the API rate limits in the FAQs.
"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.
-> 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?
You can use Quote APIs, but make sure you are within the prescribed rate limit.
"You can use Quote APIs, but make sure you are within the prescribed rate limit"
-> Thank you, noted!
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
I sense this invalidated the instrument token.