I am streaming Symbol mode and Depth mode, both together. Using the python code attached. Can you please tell me if it is advisable by you. First it ran very smooth. But second day, during mid trade hours, a connection error happened and after connection interruption I received ticks only of depth mode, but not of Symbol mode. Below is the code snippet I am using.
from fyers_apiv3.FyersWebsocket import data_ws
import os
from datetime import datetime
import logging
from utils import create_file, get_ist
from time import strftime
client_id = open("client_id.txt", 'r').read()
access_token = open("access_token.txt", 'r').read()
######## LOGGER ####################
logger = logging.getLogger(__name__)
current_time = datetime.now()
log_file_path = os.path.join("logs", "LOGS_FYERS_COMBINED_" + current_time.strftime("_%d%b%Y-%I-%M%p").upper() + ".log")
create_file(log_file_path)
logging.basicConfig(filename=log_file_path, level=logging.INFO)
logger.info("Log file created")
#####################################
def onmessage(message):
"""
Callback function to handle incoming messages from the WebSocket.
"""
logger.info("datetime : " + strftime("%Y-%m-%d %H:%M:%S"))
logger.info("Ticks: {}".format(message))
def onerror(message):
"""
Callback function to handle WebSocket errors.
"""
print("datetime : " + strftime("%Y-%m-%d %H:%M:%S"))
print("Error:", message)
logger.info("Error: {}".format(message))
def onclose(message):
"""
Callback function to handle WebSocket connection close events.
"""
print("datetime : " + strftime("%Y-%m-%d %H:%M:%S"))
print("Connection closed:", message)
logger.info("Connection closed: {}".format(message))
def onopen():
"""
Callback function to subscribe to both DepthUpdate and SymbolUpdate.
"""
# Define symbols for both modes
depth_symbols = [
'NSE:TATAMOTORS-EQ', 'NSE:SBIN-EQ', 'NSE:HAVELLS-EQ', 'NSE:BHARTIARTL-EQ',
'NSE:NIFTY25MAR23150CE', 'NSE:NIFTY25MAR23150PE',
'NSE:NIFTY25MAR23100CE', 'NSE:NIFTY25MAR23100PE'
]
symbol_symbols = ["NSE:NIFTY50-INDEX"] + depth_symbols
# Subscribe to DepthUpdate
fyers.subscribe(symbols=depth_symbols, data_type="DepthUpdate")
logger.info("Subscribed to DepthUpdate: {}".format(depth_symbols))
# Subscribe to SymbolUpdate
fyers.subscribe(symbols=symbol_symbols, data_type="SymbolUpdate")
logger.info("Subscribed to SymbolUpdate: {}".format(symbol_symbols))
# Keep the WebSocket running
fyers.keep_running()
# Create a FyersDataSocket instance
fyers = data_ws.FyersDataSocket(
access_token=access_token,
log_path="",
litemode=False,
write_to_file=True,
reconnect=True,
on_connect=onopen,
on_close=onclose,
on_error=onerror,
on_message=onmessage
)
# Establish a connection to the Fyers WebSocket
fyers.connect()
check = 1