I am using the below code to get NIFTY Live feed. But i am unable to get it. Please correct me, if anything is wrong.
Request @Hardik : Please share some lines of code to get live feed of NIFTY
Thanks
import asyncio
import websockets
import json
import time
DHAN_ACCESS_TOKEN = “”
DHAN_CLIENT_ID = “”
url = f"wss://live-feed.dhan.co/ws?token={DHAN_ACCESS_TOKEN}&clientId={DHAN_CLIENT_ID}&authType=2"
Example subscription message for NIFTY
SUBSCRIBE_MSG = {
“RequestCode”: 1, # 1 = Subscribe to live feed
“DataType”: 1, # 1 = LTP data
“Symbols”: [
{“ExchangeSegment”: 1, “SecurityId”: “1333”} # Replace with actual ID
]
}
async def run():
async with websockets.connect(url) as ws:
await ws.send(json.dumps(SUBSCRIBE_MSG))
print(“ Subscribed to live NIFTY data”)
while True:
try:
msg = await ws.recv()
if isinstance(msg, bytes):
print(f"🔴 Binary: {msg.hex()}")
else:
data = json.loads(msg)
print(f"📈 {time.strftime('%H:%M:%S')} - LTP: {data}")
except Exception as e:
print(f"Error: {e}")
break
asyncio.run(run())