import threading
from fyers_apiv3 import fyersModel
from fyers_apiv3.FyersWebsocket import data_ws
class FyersLiveStream:
def __init__(self, symbols, access_token):
print(f"stream {symbols}, accesstoken {access_token}")
self.symbols = symbols
self.access_token = access_token
self.fyers = None
def onmessage(self, message):
"""
Callback function to handle incoming messages from the FyersDataSocket WebSocket.
Parameters:
message (dict): The received message from the WebSocket.
"""
print("======= Response:", message)
def onerror(self, message):
"""
Callback function to handle WebSocket errors.
Parameters:
message (dict): The error message received from the WebSocket.
"""
print("===== Error:", message)
def onclose(self, message):
"""
Callback function to handle WebSocket connection close events.
"""
print("======= Connection closed:", message)
def onopen(self):
"""
Callback function to subscribe to data type and symbols upon WebSocket connection.
"""
print("===== On open connection")
print(f"Requesting streaming of symbols {self.symbols}")
# Specify the data type and symbols you want to subscribe to
data_type = "SymbolUpdate"
# Subscribe to the specified symbols and data type
symbols = ["NSE:NIFTY50-INDEX", "NSE:NIFTYBANK-INDEX"]
self.fyers.subscribe(symbols=symbols, data_type=data_type)
# Keep the socket running to receive real-time data
self.fyers.keep_running()
def start_stream(self):
"""
Function to start the Fyers WebSocket stream.
"""
# Create a FyersDataSocket instance with the provided parameters
self.fyers = data_ws.FyersDataSocket(
access_token=self.access_token,
log_path="",
litemode=False,
write_to_file=False,
reconnect=True,
on_connect=self.onopen,
on_close=self.onclose,
on_error=self.onerror,
on_message=self.onmessage
)
# Establish a connection to the Fyers WebSocket
self.fyers.connect()
Issue: I have used API 3, apis and used sample provided in the documentation, I am getting callback to the onconnect, where I would subscribe to "SymbolUpdate". I have run python script during market hours, but I am not getting the feed from websocket
All I see is this, when I execute
===== On open connection
Requesting streaming of symbols ['NSE:SBIN-EQ', 'NSE:ADANIENT-EQ']