fyers_socket = data_ws.FyersDataSocket(
access_token=access_token, # Access token in the format "appid:accesstoken"
on_connect=onConnect, # Callback function to handle connection events
on_message=onMessage, # Callback function to handle incoming messages
on_error=onError, # Callback function to handle errors
on_close=onClose, # Callback function to handle connection close events
)
fyers_socket.connect()
fyers_socket.keep_running()
fyers_socket.subscribe(symbols=['NSE:RELIANCE-EQ'])
fyers_socket.subscribe(symbols=['NSE:ITC-EQ'])
fyers_socket.unsubscribe(symbols=['NSE:RELIANCE-EQ'])
If two subscribe commands are used back to back like this, the unsubscribe for first one fails.
Logs:
{"level":"ERROR","location":"[unsubscribe:1758] data_ws","message":{"code":-300,"message":"Please provide a valid symbol","s":"error"},"timestamp":"2024-07-20 14:05:05,218+0530","service":"FyersDataSocket"}
Prints:
Response: {'type': 'cn', 'code': 200, 'message': 'Authentication done', 's': 'ok'}
Response: {'type': 'ful', 'code': 200, 'message': 'Full Mode On', 's': 'ok'}
Connected!
Response: {'type': 'sub', 'code': 200, 'message': 'Subscribed', 's': 'ok'}
Response: {'ltp': 3110.3, 'vol_traded_today': 6570195, 'last_traded_time': 1721384998, 'exch_feed_time': 1721384999, 'bid_size': 6681, 'ask_size': 0, 'bid_price': 3110.3, 'ask_price': 0.0, 'last_traded_qty': 5, 'tot_buy_qty': 6681, 'tot_sell_qty': 0, 'avg_trade_price': 3128.39, 'low_price': 3105.6, 'high_price': 3179.95, 'lower_ckt': 0, 'upper_ckt': 0, 'open_price': 3172.0, 'prev_close_price': 3173.35, 'type': 'sf', 'symbol': 'NSE:RELIANCE-EQ', 'ch': -63.05, 'chp': -1.9869}
Response: {'type': 'sub', 'code': 200, 'message': 'Subscribed', 's': 'ok'}
Response: {'ltp': 474.55, 'vol_traded_today': 24977373, 'last_traded_time': 1721384978, 'exch_feed_time': 1721384978, 'bid_size': 0, 'ask_size': 28089, 'bid_price': 0.0, 'ask_price': 474.55, 'last_traded_qty': 15, 'tot_buy_qty': 0, 'tot_sell_qty': 28089, 'avg_trade_price': 474.64, 'low_price': 469.4, 'high_price': 479.4, 'lower_ckt': 0, 'upper_ckt': 0, 'open_price': 471.0, 'prev_close_price': 470.25, 'type': 'sf', 'symbol': 'NSE:ITC-EQ', 'ch': 4.3, 'chp': 0.9144}
Error: {'code': -300, 'message': 'Please provide a valid symbol', 's': 'error'}