I need to get the LTP of nifty continuously
instruments = [(0, ‘13’)]
subscription_code = marketfeed.Ticker
ltp_value = None # Initialize a variable to store the LTP
async def on_connect(instance):
print(“Connected! in callback”)
async def on_close(instance):
print(“###C LOSED ############”)
async def on_message(ws, message):
global ltp_value
if message is not None and ‘LTP’ in message:
ltp_value = float(message[‘LTP’])
print(“Current price (on_message):”, ltp_value)
print(“Current price (on_message):”, ltp_value)
def get_ltp():
global ltp_value
return ltp_value
user_ID = <MY_USER_ID>
access_token =<ACCESS_TOKEN
print(“Subscription code :”, subscription_code)
feed = marketfeed.DhanFeed(user_ID,
access_token,
instruments,
subscription_code,
on_connect=on_connect,
on_message=on_message,
on_close=on_close)
feed.run_forever()
asyncio.create_task(feed.run_forever())
But after 1-2 iteration the connection is getting closed…
Connected! in callback
Connected! in callback
Current price (on_message): 22502.0
Current price (on_message): 22502.0
Current price (on_message): 22502.0
Disconnected: Subscribe to Data APIs to continue
Current price (on_message): 22502.0
Connection has been closed
Connected! in callback
Connected! in callback
Current price (on_message): 22502.0
Current price (on_message): 22502.0
Current price (on_message): 22502.0
Disconnected: Subscribe to Data APIs to continue
Current price (on_message): 22502.0
Connection has been closed
Traceback (most recent call last):
File “/Users/raviverma/Desktop/python/marketfeed.py”, line 46, in
asyncio.create_task(feed.run_forever())
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/tasks.py”, line 381, in create_task
loop = events.get_running_loop()
^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
Please help me how can i keep on getting the Nifty LTP continuously.