why im not getting live tick >>import websocket
import threading
import logging
import ssl
import certifi
import gzip
import json
import time
from MarketDataFeed_pb2 import FeedResponse
MARKET_DATA = 1
MARKET_INFO = 2
class AutoLTPWebSocket:
def \__init_\_(self, access_token, instruments, ws_url, tick_callback=None):
self.access_token = access_token
self.instruments = instruments
self.ws_url = ws_url
self.tick_callback = tick_callback
self.ws = None
self.ssl_path = certifi.where()
self.subscribed = False # subscription acknowledgment flag
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
def on_open(self, ws):
logging.info("🔗 WebSocket connected successfully.")
sub_msg = {
"guid": "auto_sub",
"method": "sub",
"data": {
"mode": "ltpc", # last traded price feed
"instrumentKeys": self.instruments
}
}
ws.send(json.dumps(sub_msg))
logging.info(f"✅ Subscription sent for: {self.instruments}")
def on_message(self, ws, message):
try:
logging.debug(f"Raw WS message: {message\[:300\]}")
if isinstance(message, str):
message = message.encode("latin1")
\# Detect subscription acknowledgment
if b'"method":"sub_ack"' in message:
self.subscribed = True
logging.info("✅ Subscription acknowledged by server")
\# Handle gzip
try:
data = gzip.decompress(message)
except OSError:
data = message # not gzipped
feed_response = FeedResponse()
feed_response.ParseFromString(data)
for feed in feed_response.feeds:
if feed.feed_type == MARKET_DATA:
ltp = feed.market_data.last_traded_price / 100.0
instrument = feed.market_data.instrument_key
ts = time.time()
logging.info(f"📈 {instrument}: {ltp} @ {ts}")
if self.tick_callback:
self.tick_callback({
"instrument": instrument,
"ltp": ltp,
"timestamp": ts
})
elif feed.feed_type == MARKET_INFO:
logging.info(f"ℹ️ Market Info: {feed.market_info}")
except Exception as e:
logging.error(f"❌ Message parsing error: {e}, raw={message\[:200\]}")
def on_error(self, ws, error):
logging.error(f"❌ WebSocket error: {error}")
def on_close(self, ws, close_status_code, close_msg):
logging.warning(f"🔒 WebSocket closed: {close_status_code} - {close_msg}")
self.subscribed = False
def start(self):
"""Start WebSocket in a new thread with SSL verification"""
def run():
try:
websocket.enableTrace(False)
self.ws = websocket.WebSocketApp(
self.ws_url,
header=\[f"Authorization: Bearer {self.access_token}"\],
on_open=self.on_open,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close
)
logging.info("🚀 WebSocket thread started")
self.ws.run_forever(
ping_interval=20,
sslopt={
"ca_certs": self.ssl_path,
"cert_reqs": ssl.CERT_REQUIRED
}
)
except ssl.SSLError:
logging.warning("⚠️ SSL verification failed. Retrying without verification.")
self.ws.run_forever(
ping_interval=20,
sslopt={"cert_reqs": ssl.CERT_NONE}
)
except Exception as e:
logging.error(f"❌ WebSocket fatal error: {e}")
t = threading.Thread(target=run, daemon=True)
t.start()
return t >> should enable DMA ?