The material on usage of websockets to fetch LTP is inadequate or complex.
Request you to kindly edit the following code to get the LTP of the given instrument
(NIFTY 02 MAY 21450 PUT). This can act as a good example for people looking for code to get LTP from websockets.
The LTP of the instrument can be stored in the variable ltp.
from dhanhq import marketfeed
from dhanhq import dhanhq
import pandas as pd
client_id = “myclientID”
access_token = “myaccesstoken”
dhan = dhanhq(client_id,access_token)
ltp = 0.0
instruments = [(2, “40989”)]
subscription_code = marketfeed.Ticker
async def on_connect(instance):
print(“Connected to websocket”)
async def on_message(instance, message):
print(“Received:”, message)
print("Subscription code :"subscription_code)
feed = marketfeed.DhanFeed(client_id,
access_token,
instruments
subscription_code
on_connect=on_connect,
on_message=on_message)
feed.run_forever()