I have developed a Python script to fetch the LTP (Last Traded Price) and other details from the live market feed. However, the script is not returning any data as expected. Below, I have shared the code along with the output. Could you please help me resolve this issue?
code:—
import asyncio
import json
import websockets
import MarketDataFeedV3_pb2 as pb # Your compiled Protobuf module
import ssl
ACCESS_TOKEN = ‘my ACCESS_TOKEN’
ws_url = “wss://api.upstox.com/v3/feed/market-data-feed” # Make sure this is ws:// not wss://
INSTRUMENT_KEYS = [“101000000”]
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
def decode_protobuf(buffer):
resp = pb.FeedResponse()
resp.ParseFromString(buffer)
return resp
async def subscribe_to_feed():
print(“
Connecting to WebSocket…”)
async with websockets.connect(
ws_url,
ssl=ssl_context,
additional_headers={"Authorization": f"Bearer {ACCESS_TOKEN}"}
) as websocket:
print("✅ WebSocket connection established")
sub_request = {
"guid": "my-guid-001",
"method": "sub",
"mode": "ltpc",
"instrumentKeys": INSTRUMENT_KEYS
}
print("📦 Sending subscription request:", sub_request)
await websocket.send(json.dumps(sub_request))
print("📡 Subscription sent:", INSTRUMENT_KEYS)
print("🔄 Starting tick reception loop...")
while True:
raw_data = await websocket.recv()
buffer = raw_data if isinstance(raw_data, bytes) else raw_data.encode()
response = decode_protobuf(buffer)
print("🧭 Decoded message type:", response.type)
print("📊 Market Info:", response.marketInfo.segmentStatus)
print("📈 Feed count:", len(response.feeds))
if response and response.feeds:
for symbol, feed in response.feeds.items():
print(f"🔑 {symbol}")
if feed.HasField("ltpc"):
print(" 📊 LTP:", feed.ltpc.ltp)
elif feed.HasField("fullFeed"):
ff = feed.fullFeed
if ff.HasField("marketFF"):
print(" 📊 Market LTP:", ff.marketFF.ltpc.ltp)
elif ff.HasField("indexFF"):
print(" 📊 Index LTP:", ff.indexFF.ltpc.ltp)
else:
print("⏳ No tick data in this frame.")
if name == “main”:
asyncio.run(subscribe_to_feed())
Below are the multiple run OUTPUT after changing instrumentKeys:—
Connecting to WebSocket…
WebSocket connection established
Sending subscription request: {‘guid’: ‘my-guid-001’, ‘method’: ‘sub’, ‘mode’: ‘ltpc’, ‘instrumentKeys’: [‘NSE_EQ|INE002A01018’]}
Subscription sent: [‘NSE_EQ|INE002A01018’]
Starting tick reception loop…
Decoded message type: 2
Market Info: {‘NCD_FO’: 2, ‘BCD_FO’: 2, ‘NSE_INDEX’: 2, ‘NSE_EQ’: 2, ‘BSE_INDEX’: 2, ‘BSE_FO’: 2, ‘MCX_FO’: 2, ‘NSE_FO’: 2, ‘NSE_COM’: 2, ‘BSE_EQ’: 2, ‘MCX_INDEX’: 2}
Feed count: 0
No tick data in this frame.
Connecting to WebSocket…
WebSocket connection established
Sending subscription request: {‘guid’: ‘my-guid-001’, ‘method’: ‘sub’, ‘mode’: ‘ltpc’, ‘instrumentKeys’: [‘NSE_EQ|INE002A01018’]}
Subscription sent: [‘NSE_EQ|INE002A01018’]
Starting tick reception loop…
No tick data in this frame.
Connecting to WebSocket…
WebSocket connection established
Sending subscription request: {‘guid’: ‘my-guid-001’, ‘method’: ‘sub’, ‘mode’: ‘ltpc’, ‘instrumentKeys’: [‘101000000’]}
Subscription sent: [‘101000000’]
Starting tick reception loop…
Decoded message type: 2
Market Info: {‘NCD_FO’: 2, ‘BCD_FO’: 2, ‘NSE_INDEX’: 2, ‘NSE_EQ’: 2, ‘BSE_INDEX’: 2, ‘BSE_FO’: 2, ‘MCX_FO’: 2, ‘NSE_FO’: 2, ‘NSE_COM’: 2, ‘BSE_EQ’: 2, ‘MCX_INDEX’: 2}
Feed count: 0
No tick data in this frame.