hi here is the code:-
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 = ['NSE:BANKNIFTY23SEP44400CE']
fyers.subscribe(symbols=symbols, data_type=data_type, channel = 15)
# 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
access_token = “accesstoken.eyJpc3MiOiJhcGkubG9naW4uZnllcnMuaW4iLCJpYXQiOjE2OTU2MjI3NTEsImV4cCI6MTY5NTY1Mjc1MSwibmJmIjoxNjk1NjIyMTUxLCJhdWQiOiJbXCJ4OjBcIiwgXCJ4OjFcIiwgXCJ4OjJcIiwgXCJkOjFcIiwgXCJkOjJcIiwgXCJ4OjFcIiwgXCJ4OjBcIl0iLCJzdWIiOiJhdXRoX2NvZGUiLCJkaXNwbGF5X25hbWUiOiJYSzI1NzY1Iiwib21zIjoiSzEiLCJoc21fa2V5IjoiOTg1N2IwOWMzOWM0MWExOWI5YTlhNTk1MTllZjIyMzA0MTM4ZmIwOGE0MjViMmRjZWMxMjVmYWUiLCJub25jZSI6IiIsImFwcF9pZCI6IjhER0tLOEpaVTQiLCJ1dWlkIjoiMzI4NDEzMTBlNmJjNDNmNTljM2U0NWY0NDkzNjQ2OWQiLCJpcEFkZHIiOiIwLjAuMC4wIiwic2NvcGUiOiIifQ.ZBUx9QsINKoidrawHvyzERaufeT7SiG22ybenErEZYU”
Create a FyersDataSocket instance with the provided parameters
fyers = data_ws.FyersDataSocket(
access_token=access_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=True, # 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()
outcome:-
Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}
Response: {‘type’: ‘lit’, ‘code’: 200, ‘message’: ‘Lite Mode On’, ‘s’: ‘ok’}
no ticks afterwards