Try this,
I had a code to write symbol name and ltp to an excel sheet from websocket, see if it helps you.
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 custom_message(msg):
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 a specific process you need to connect to get the updates on
run_process_foreground_symbol_data(access_token)
if __name__ == '__main__':
main()