Search code examples
apiwebsocketbinance

Binance Websocket depth channel too many updates for given ids


I am using the Binance Websocket API and when I subscribe to depth channels (such as via wss://stream.binance.com:9443/stream?streams=btcusdt@depth/), I occasionally get an update that has too many updates for the given first id ('U') and last id ('u'):

{"stream":"btcusdt@depth","data":{"e":"depthUpdate","E":1536066285499,"s":"BTCUSDT",
"U":238495725,
"u":238495734,
"b":[
["7369.47000000","0.00000000",[]], --238495725
["7369.46000000","0.00000000",[]], --238495726
["7369.45000000","0.00000000",[]], --238495727
["7369.19000000","0.90770800",[]], --238495728
["7365.20000000","0.20000000",[]], --238495729
["7363.71000000","0.54130000",[]], --238495730
["7362.94000000","0.00000000",[]], --238495731
["7358.00000000","0.00000000",[]]] --238495732
,"a":[
["7369.47000000","0.02302600",[]], --238495733
["7369.70000000","0.00000000",[]], --238495734
["7408.90000000","3.93000000",[]]] --238495735 WHICH IS NOT OKAY (>u)
}}

I have also noticed that occasionally I will get more ids than updates such as in the following:

{"stream":"btcusdt@depth","data":{"e":"depthUpdate","E":1536068517031,"s":"BTCUSDT",
"U":238514909,
"u":238514912,
"b":[],
"a":[
["7366.54000000","0.00000000",[]], --238514909
["7367.09000000","0.00000000",[]]] --238514910
}}

I am trying to add these updates to a database so having too many ids is less of a problem for me than having too few. If anyone could explain why this happens or how I might be misunderstanding update ids it would be much appreciated.

Thanks in advance!


Solution

  • So it turns out that "update IDs" do not correspond to order IDs and multiple order book entries can be changed in a single update.

    Furthermore, the order of the given bids and asks isn't the actual order that they were received but rather they are descending order for bids and ascending for asks and the state of the order book can not be deterministically gotten from the websocket API :/