I am using fyers-apiv2=2.0.5, where as the method custom_message(msg) is registered as a listener
using ws.websocket_data=self.custom_message
After running the code, I am getting all subscribed symbols data like- Response:[{‘symbol’: ‘NSE:TCS-EQ’, ‘timestamp’: 165…
But the beheviuor of this method custom_message(msg) is uncontrolled, and notice two issue
Issue 1 - even code ws.websocket_data=self.custom_message is removed, and run the code again, still I am getting all subscribed symbols data
Issue 2 - Any code written within custom_message(msg) is not executing or no output, but getting all subscribed symbols data, even data being logged without print(msg) statement
Custom message function is just used to modify the output of websocket data. For example, Here is an example code of outputting only symbol name and ltp value in a csv file, which executes inside the custom message function,
import csv
from fyers_api.Websocket import ws
def run_process_foreground_symbol_data(access_token):
'''This function is used for running the symbolData in foreground
1. log_path here is configurable this specifies where the output will be stored for you
2. data_type == symbolData this specfies while using this function you will be able to connect to symbolwebsocket to get the symbolData
3. run_background = False specifies that the process will be running in foreground'''
data_type = "symbolData"
symbol = ["NSE:SBIN-EQ"] ##NSE,BSE sample symbols
fs = ws.FyersSocket(access_token=access_token,run_background=False,log_path="")
fs.websocket_data = custom_message
fs.subscribe(symbol=symbol,data_type=data_type)
fs.keep_running()
def run_process_foreground_order_update(access_token):
'''This function is used for running the order_update in background
1. log_path here is configurable this specifies where the output will be stored for you.
2. data_type == orderUpdate this specfies while using this function you will be able to connect to orderwebsocket to get the orderUpdate
3. run_background = False specifies that the process will be running in foreground '''
data_type = "orderUpdate"
fs = ws.FyersSocket(access_token=access_token,run_background=False,log_path="")
fs.websocket_data = custom_message
fs.subscribe(data_type=data_type)
fs.keep_running()
def custom_message(msg):
# print(msg[0]['symbol'])
# print(msg[0]['ltp'])
out = csv.writer(open('test.csv', 'a'))
out.writerow([msg[0]['symbol'], msg[0]['ltp']])
def main():
header = ["SYMBOL", "LTP"]
with open('test.csv', 'w', encoding='UTF8') as f:
writer = csv.writer(f)
writer.writerow(header)
### Insert the accessToken and app_id over here in the following format (APP_ID:access_token)
access_token = "" #appid:accesstoken
run_process_foreground_symbol_data(access_token)
if __name__ == '__main__':
main()
Hello @vikram_sri , the above provided code is working to write the data into a csv file, but the same is not being run in background i.e. when run_background = True
Yes, even I need custom_*message(msg) for processing *of websocket data, but it is not invoking with the given code. this is the issue
Basically, I want to invoke the custom_*message(msg) when data received in ws.py @ method *def __on_message(self, ws, msg):, that’s it, which is not happening