Hello Team Fyers,
I have implemented your Live Market Data code, including the session generation and authentication process using the auth_code. However, I am encountering an issue where the live market data is not being fetched as expected. Could you please assist me in identifying whether there is an issue with the code or with the authentication process?
Thank you for your support.
from fyers_apiv3.FyersWebsocket import data_ws
def onmessage(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(message):
"""
Callback function to handle WebSocket errors.
Parameters:
message (dict): The error message received from the WebSocket.
"""
print("Error:", message)
def onclose(message):
"""
Callback function to handle WebSocket connection close events.
"""
print("Connection closed:", message)
def onopen():
"""
Callback function to subscribe to data type and symbols upon WebSocket connection.
"""
# Specify the data type and symbols you want to subscribe to
data_type = "SymbolUpdate"
# Subscribe to the specified symbols and data type
symbols = ["MCX:CRUDEOIL24OCTFUT"]
fyers.subscribe(symbols=symbols, data_type=data_type)
# Keep the socket running to receive real-time data
fyers.keep_running()
Replace the sample access token with your actual access token obtained from Fyers
wsaccess_token = f’{client_id}:{access_token}’
Create a FyersDataSocket instance with the provided parameters
fyers = data_ws.FyersDataSocket(
access_token=wsaccess_token, # Access token in the format "appid:accesstoken"
log_path="", # Path to save logs. Leave empty to auto-create logs in the current directory.
litemode=False, # Lite mode disabled. Set to True if you want a lite response.
write_to_file=False, # Save response in a log file instead of printing it.
reconnect=True, # Enable auto-reconnection to WebSocket on disconnection.
on_connect=onopen, # Callback function to subscribe to data upon connection.
on_close=onclose, # Callback function to handle WebSocket connection close events.
on_error=onerror, # Callback function to handle WebSocket errors.
on_message=onmessage # Callback function to handle incoming messages from the WebSocket.
)
Establish a connection to the Fyers WebSocket
fyers.connect()
Output
Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}
Response: {‘type’: ‘ful’, ‘code’: 200, ‘message’: ‘Full Mode On’, ‘s’: ‘ok’}