symbol on `<symbol>@depth<levels>` stream payload

on “partial book depth streams” (<symbol>@depth<levels>) stream payload there is no field indicating the symbol associated with the update. how can i distinguish updates when subscribing to multiple symbols?

here is the documentation example payload:

{
  "lastUpdateId": 160,  // Last update ID
  "bids": [             // Bids to be updated
    [
      "0.0024",         // Price level to be updated
      "10"              // Quantity
    ]
  ],
  "asks": [             // Asks to be updated
    [
      "0.0026",         // Price level to be updated
      "100"             // Quantity
    ]
  ]
}

Use “/stream?streams=<stream name>” instead of “/ws/<stream name>”

1 Like

Depending on the ws implementation you are using, you may also be able to register a different end point for each currency and thus forward corresponding events to differents consumers. That’s how I do.

There is definitely a field missing, if you want to subscribe to multiple symbols within the same connection, there is no way to distinguish what update is for what symbol.
@dino I assume it wouldn’t be complicated to simply add the “symbol” field?

Have you tried with stream ? For example:

websocat -v ‘wss://stream.binance.com/stream?streams=bnbusdt@depth5/btcusdt@depth5’

It’ll list the stream name with the symbol:

{“stream”:“bnbusdt@depth5”,“data”:{“lastUpdateId”:4900617809,“bids”:[[“296.18000000”,“2.16760000”],[“296.17000000”,“19.10230000”],[“296.16000000”,“1.68780000”],[“296.15000000”,“1.49200000”],[“296.14000000”,“23.24970000”]],“asks”:[[“296.21000000”,“1.95260000”],[“296.22000000”,“26.34810000”],[“296.23000000”,“18.71710000”],[“296.24000000”,“33.00000000”],[“296.27000000”,“14.71500000”]]}}

{“stream”:“btcusdt@depth5”,“data”:{“lastUpdateId”:12516016522,“bids”:[[“32389.91000000”,“0.47673300”],[“32386.50000000”,“0.21300000”],[“32386.45000000”,“0.04719000”],[“32386.01000000”,“1.24895400”],[“32386.00000000”,“1.30018400”]],“asks”:[[“32389.92000000”,“1.66866500”],[“32389.98000000”,“0.02000000”],[“32390.30000000”,“0.04000000”],[“32391.55000000”,“0.00350000”],[“32392.49000000”,“0.04629700”]]}}

@aisling It means opening a new websocket connection for each channel you subscribe to, or at least you don’t have the flexibility to subscribe/unsubscribe after the connection has been opened (most of the time when your program start you don’t know in advance what channels/symbols you will need, because it depends on what the user needs, what are his actions while the program is already running).

This lack of fields is only with the Spot API from what I see in the documentation, on the futures API side the channel has all the necessary information.

@TraderVK You should be able to subscribe/unsubscribe during a websocket connection, please refer to https://binance-docs.github.io/apidocs/spot/en/#live-subscribing-unsubscribing-to-streams, the “id” is customisable, so you can use it to identify your symbols.

@aisling Yes the “Live Subscribing/Unsubscribing to streams” part is what I use.
I do set the “id”, but the “id” isn’t returned in the websocket messages that we receive (for any channel, not just the depth one).

{
  "lastUpdateId": 160,  // Last update ID
  "bids": [             // Bids to be updated
    [
      "0.0024",         // Price level to be updated
      "10"              // Quantity
    ]
  ],
  "asks": [             // Asks to be updated
    [
      "0.0026",         // Price level to be updated
      "100"             // Quantity
    ]
  ]
}

@TraderVK Ok, thanks for more details. I think in this case you can add a callback that can associate the received stream update with the expected symbol. You can see an example here:
https://github.com/binance-exchange/binance-toolbox-python/blob/master/spot/multi_subs_market_ws.py

*the script recurs lib “binance-connector”

@aisling I’m coding things from scratch, not using this library, I see the callback argument in binance-connector is simply creating a new connection for every channel (not the “Live Subscribing” case then).
I will use the Diff. Depth Stream endpoint instead, it has the “s” (symbol) field.
Thanks again for taking the time to answer.

Just encountered same problem.

It’s 10 months since the author started this thread. Why has Binance not added symbol to the message?

I totally agree this is a major drawback of the SPOT Partial Book Depth Streams!
Come on guys, it’s 2024, could you please add the symbol field inside this stream?
This is a really important feature to be added!

I think everything you need is there!

pair = data['stream'].split("@")[0]
print("Symbol:", pair)

As a concrete example with UBWA, but also works with all other libraries:

from unicorn_binance_websocket_api import BinanceWebSocketApiManager

ubwa = BinanceWebSocketApiManager()

ubwa.create_stream(channels="depth5", markets="bnbusdt", output="dict")

while True:
    data = ubwa.pop_stream_data_from_stream_buffer()
    if data:
        try:
            pair = data['stream'].split("@")[0]
            print("Symbol:", pair)
        except KeyError:
            print("Symbol not found:", data)

Output:
Symbol not found: {‘result’: None, ‘id’: 1}
Symbol: bnbusdt
Symbol: bnbusdt
Symbol: bnbusdt