General WebSocket - Subscribe
This guide explains how to subscribe to orders, trades, positions, EDIS, price alerts and login updates on the General WebSocket.
USE CASE
Choose which updates you want, such as orders, trades or positions, right after the connection opens.
Details
Convert the json to string and send this message to the socket to subscribe for the action_data mentioned.
message
message = json.dumps({ "T": "SUB_ORD", "SLIST": action_data, "SUB_T": 1 })
T Type: String
value: SUB_ORD (Fixed)
action_data Type: List/Array
Value: orders trades positions edis pricealerts login
Based on the list passed in action_data, web_socket data will be received.
SUB_T Type: Integer
Value: 1 (value 1 is for subscribing and -1 for unsubscribe)
Once subscribed successfully, for any action triggered, data will be received through the socket. If a callback function is defined, it receives that data. The response is a string in Python and an ArrayBuffer in Node. In another language, convert the payload to a string, then parse it.
Sample response
{'code': 1605, 'message': 'Successfully subscribed', 's': 'ok'}
Type: string
Value
{"orders":{"client_id":"XP0001","exchange":10,"fy_token":"10100000001628","id":"23121800292158","id_fyers":"df013f50-6925-4e2d-ba0f-0becf1229298","instrument":0,"lot_size":1,"offline_flag":false,"oms_flag":"K:1","ord_source":"W","ord_status":20,"ord_type":2,"ordertag":"2:Untagged","org_ord_status":4,"pan":"FYxxxxxx0S","precision":2,"price_multiplier":1,"product_type":"CNC","qty":1,"qty_multiplier":1,"qty_remaining":1,"report_type":"New Ack","segment":10,"status_msg":"New Ack","symbol":"NSE:BECTORFOOD-EQ","symbol_desc":"MRS BECTORS FOOD SPE LTD","symbol_exch":"BECTORFOOD","tick_size":0.05,"time_epoch_oms":1702887690,"time_exch":"NA","time_oms":"18-Dec-2023 13:51:30","tran_side":1,"update_time_epoch_oms":1702887690,"update_time_exch":"01-Jan-1970 05:30:00","validity":"DAY"},"s":"ok"}
- The response is a string in Python and an
arraybufferin Node. Check the format you receive from the websocket library for your language. - Convert the data to a string if it is not already a string, then parse that string to JSON. The JSON will contain one of these keys:
orders,trades, orpositions. - The key identifies the update type.
ordersis an order update,tradesis a trades update, andpositionsis a positions update. - In the FYERS SDK, the raw socket response is parsed to keep required keys and remove unnecessary keys.
Parsed Data
{
"s": "ok",
"orders": {
"clientId": "XP03754",
"id": "23121800388066",
"qty": 1,
"remainingQuantity": 1,
"type": 2,
"fyToken": "10100000002705",
"exchange": 10,
"segment": 10,
"symbol": "NSE:PRAJIND-EQ",
"instrument": 0,
"offlineOrder": false,
"orderDateTime": "18-Dec-2023 16:33:24",
"orderValidity": "DAY",
"productType": "CNC",
"side": 1,
"status": 4,
"source": "W",
"ex_sym": "PRAJIND",
"description": "PRAJ INDUSTRIES LTD",
"orderNumStatus": "23121800388066:4"
}
}- All the keys information for orders updates are available there: Link
- All the keys information for trades updates are available there: Link
- All the keys information for positions updates are available there: Link
Internal mapping from raw keys to the final parsed data:
{
"position_mapper": {
"symbol": "symbol",
"id": "id",
"buy_avg": "buyAvg",
"buy_qty": "buyQty",
"buy_val": "buyVal",
"sell_avg": "sellAvg",
"sell_qty": "sellQty",
"sell_val": "sellVal",
"net_avg": "netAvg",
"net_qty": "netQty",
"tran_side": "side",
"qty": "qty",
"product_type": "productType",
"pl_realized": "realized_profit",
"rbirefrate": "rbiRefRate",
"fy_token": "fyToken",
"exchange": "exchange",
"segment": "segment",
"day_buy_qty": "dayBuyQty",
"day_sell_qty": "daySellQty",
"cf_buy_qty": "cfBuyQty",
"cf_sell_qty": "cfSellQty",
"qty_multiplier": "qtyMulti_com",
"pl_total": "pl",
"cross_curr_flag": "crossCurrency",
"pl_unrealized": "unrealized_profit"
},
"order_mapper": {
"client_id": "clientId",
"id": "id",
"id_parent": "parentId",
"id_exchange": "exchOrdId",
"qty": "qty",
"qty_remaining": "remainingQuantity",
"qty_filled": "filledQty",
"price_limit": "limitPrice",
"price_stop": "stopPrice",
"tradedPrice": "price_traded",
"ord_type": "type",
"fy_token": "fyToken",
"exchange": "exchange",
"segment": "segment",
"symbol": "symbol",
"instrument": "instrument",
"oms_msg": "message",
"offline_flag": "offlineOrder",
"time_oms": "orderDateTime",
"validity": "orderValidity",
"product_type": "productType",
"tran_side": "side",
"org_ord_status": "status",
"ord_source": "source",
"symbol_exch": "ex_sym",
"symbol_desc": "description"
},
"trade_mapper": {
"id_fill": "tradeNumber",
"id": "orderNumber",
"qty_traded": "tradedQty",
"price_traded": "tradePrice",
"traded_val": "tradeValue",
"product_type": "productType",
"client_id": "clientId",
"id_exchange": "exchangeOrderNo",
"ord_type": "orderType",
"tran_side": "side",
"symbol": "symbol",
"fill_time": "orderDateTime",
"fy_token": "fyToken",
"exchange": "exchange",
"segment": "segment"
}
}- Subscribe only after the connection is open. You receive data only for the actions you pass in
SLIST, and you can list several actions in one message. - A message with
code1605andsset tookconfirms the subscription. - In the Python SDK, subscribe with
data_typevaluesOnOrders,OnTrades,OnPositionsandOnGeneral. Join several with commas, for exampleOnOrders,OnTrades. The SDK converts them intoSLIST. - Price alert, EDIS and login messages are general events and arrive under their own top-level key, for example
pricealerts. See General Events. - The raw message and the SDK-parsed message use different key names, for example
org_ord_statusbecomesstatusandtran_sidebecomesside. Use the key mappers above to translate between them. SUB_T1subscribes and-1unsubscribes. See General Socket - UnSubscribe.