Error in getting LTP data

I am trying to get LTP data using following code but only getting data for the first stock “reliance”:

watchlist = [
‘RELIANCE’, ‘HDFCBANK’, ‘ICICIBANK’, ‘BHARTIARTL’,
‘INFY’, ‘SBIN’, ‘LICI’, ‘ITC’, ‘LT’ ]

for stock_name in watchlist:
print(stock_name)

ltp_data = tsl.get_ltp_data(names=stock_name)[stock_name]

Error :

RELIANCE
1328.8
HDFCBANK
Exception at calling ltp as {‘status’: ‘failure’, ‘remarks’: {‘error_code’: None, ‘error_type’: None, ‘error_message’: None}, ‘data’: ‘’}
Traceback (most recent call last):
File “G:\python\3. Session3 - Codebase\3. Session3 - Codebase\Dhan codebase\algo1 - test.py”, line 57, in
ltp_data = tsl.get_ltp_data(names=stock_name)[stock_name]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: ‘HDFCBANK’

PLS HELP

Hi @sunita_sriv ,

We need to use time.sleep(1) because the LTP data can be fetched only once per second, as the API has a rate limit of one request per second.

Refer the below code -

from Dhan_Tradehull import Tradehull
import time 
import datetime
import pdb
client_code = ""
tsl = Tradehull(client_code, mode="pin_totp", pin="", totp_secret="")
watchlist = ['RELIANCE', 'HDFCBANK', 'ICICIBANK', 'BHARTIARTL','INFY', 'SBIN', 'LICI', 'ITC', 'LT' ]

for stock_name in watchlist:

    time.sleep(1)
    ltp_data = tsl.get_ltp_data(names=stock_name)
    print(f"{stock_name} : {ltp_data}")

Output -

1 Like

Thank you @Tradehull_Imran

1 Like