Hi Team,
Can someone please help here, I am not getting tick by tick data for commodity scripts when using Websocket connection in APIv3? Surprisingly, same code works for NSE (Equity and Options) data.
Do you have any idea about in-correct data receiving from websocket?
Sharing complete script here,
from fyers_apiv3 import fyersModel
from fyers_apiv3.FyersWebsocket import data_ws
import xlwings as xw
import threading
sheet = None
t = 1000
while t > 0 and sheet is None:
try:
xw.Book('auto_fyers.xlsx').set_mock_caller()
sheet = xw.Book.caller().sheets['Data']
cred_sheet = xw.Book.caller().sheets['fyers_login']
except Exception as str_error:
print(t, str_error)
t -= 1
client_id = cred_sheet['B2'].value
access_token = cred_sheet['B6'].value
#fyers = fyersModel.FyersModel(token=access_token,is_async=False,client_id=client_id,log_path="")
#data = {"symbols":"NSE:TCS-EQ"}
#data = {"symbols":"MCX:CRUDEOIL23NOV7300CE"}
#data = {"symbols":"NSE:BANKNIFTY23O1844000CE"}
#print (fyers.quotes(data))
#ce_ltp = (fyers.quotes(data)["d"][0]["v"]["lp"])
#data = {"symbols":"NSE:BANKNIFTY23O1844000PE"}
#data = {"symbols":"MCX:CRUDEOIL23NOV7200PE"}
#pe_ltp = (fyers.quotes(data)["d"][0]["v"]["lp"])
#print (str(ce_ltp) + " " + str(pe_ltp))
low_pe = 1000
low_ce = 1000
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)
print("Response:", message['symbol'] )
print("Response:", message['ltp'] )
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"
#data_type = "OnOrders,OnTrades,OnPositions,OnGeneral"
#data_type = "DepthUpdate"
# Subscribe to the specified symbols and data type
#symbols = ['NSE:SBIN-EQ', 'NSE:ADANIENT-EQ']
#symbols = ['MCX:CRUDEOIL23NOV7400CE', 'MCX:CRUDEOIL23NOV7400PE']
#symbols = ['MCX:CRUDEOIL23NOV7300CE', 'NSE:BANKNIFTY23OCT44000CE']
#symbols = ['NSE:BANKNIFTY23OCT44000CE', 'NSE:BANKNIFTY23OCT44000PE']
#symbols = ["NSE:NIFTY50-INDEX" , "NSE:NIFTYBANK-INDEX"]
symbols = ["MCX:CRUDEOIL23DECFUT", "MCX:CRUDEOIL23NOV7400CE", "MCX:NATURALGAS23NOVFUT"]
fyers_ws.subscribe(symbols=symbols, data_type=data_type)
# Keep the socket running to receive real-time data
fyers_ws.keep_running()
# Replace the sample access token with your actual access token obtained from Fyers
access_token = client_id + ":" + access_token
# Create a FyersDataSocket instance with the provided parameters
fyers_ws = 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_ws.connect()
Output:
Response: MCX:CRUDEOIL23NOV7400CE Response: 250.0 Response: MCX:NATURALGAS23NOVFUT Response: 275.5 Response: MCX:CRUDEOIL23DECFUT Response: 7181.0
Getting this o/p only once and that is showing incorrect figures.