Hello Team,
I am having weired scenario when I am trying to subscribe multiple symbol at some intervals. For first symbol it works fine but for next symbols its mixing ltp prices with each other symbols., Please find below sample code,
from fyers_apiv3.FyersWebsocket import data_ws
import time
def fetch_data_for_symbol(symbol):
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
fyers.unsubscribe(symbols=symbol, data_type=data_type)
time.sleep(3)
fyers.subscribe(symbols=symbol, 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
access_token = "VWXXXXXJV-100:XXXX"
# 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()
symbols = ['MCX:CRUDEOIL24SEPFUT', 'MCX:CRUDEOIL24SEP6000CE']
for symbol in symbols:
print(symbol)
fetch_data_for_symbol([symbol])
time.sleep(10)
Output of the program:
MCX:CRUDEOIL24SEPFUT
Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}
Response: {‘type’: ‘lit’, ‘code’: 200, ‘message’: ‘Lite Mode On’, ‘s’: ‘ok’}
Response: {‘type’: ‘sub’, ‘code’: 200, ‘message’: ‘Subscribed’, ‘s’: ‘ok’}
Response: {‘ltp’: 6129.0, ‘symbol’: ‘MCX:CRUDEOIL24SEPFUT’, ‘type’: ‘sf’}
MCX:CRUDEOIL24SEP6000CE
Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}
Response: {‘type’: ‘lit’, ‘code’: 200, ‘message’: ‘Lite Mode On’, ‘s’: ‘ok’}
Response: {‘type’: ‘sub’, ‘code’: 200, ‘message’: ‘Subscribed’, ‘s’: ‘ok’}
Response: {‘ltp’: 263.3, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
Response: {‘ltp’: 6131.0, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
Response: {‘ltp’: 263.5, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
Response: {‘ltp’: 6131.0, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
Response: {‘ltp’: 263.5, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
Response: {‘ltp’: 6131.0, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
Response: {‘ltp’: 263.5, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
Response: {‘ltp’: 6131.0, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
Response: {‘ltp’: 263.5, ‘symbol’: ‘MCX:CRUDEOIL24SEP6000CE’, ‘type’: ‘sf’}
If you see carefully : its mixing prices of CRUDEOIL24SEPFUT & CRUDEOIL24SEP6000CE