The recent commit on the Spot API documentation has changed the logic of Spot order book management.
Previous logic:
- Open a stream to wss://stream.binance.com:9443/ws/bnbbtc@depth.
- Buffer the events you receive from the stream.
- Get a depth snapshot from https://api.binance.com/api/v3/depth?symbol=BNBBTC&limit=1000 .
- Drop any event where
u
is <=lastUpdateId
in the snapshot. - The first processed event should have
U
<=lastUpdateId
+1 ANDu
>=lastUpdateId
+1. - While listening to the stream, each new event’s
U
should be equal to the previous event’su
+1. - The data in each event is the absolute quantity for a price level.
- If the quantity is 0, remove the price level.
- Receiving an event that removes a price level that is not in your local order book can happen and is normal.
Current logic:
- Open a WebSocket connection to
wss://stream.binance.com:9443/ws/bnbbtc@depth
. - Buffer the events received from the stream. Note the
U
of the first event you received. - Get a depth snapshot from
https://api.binance.com/api/v3/depth?symbol=BNBBTC&limit=5000
. - If the
lastUpdateId
from the snapshot is strictly less than theU
from step 2, go back to step 3. - In the buffered events, discard any event where
u
is <=lastUpdateId
of the snapshot. The first buffered event should now havelastUpdateId
within its[U;u]
range. - Set your local order book to the snapshot. Its update ID is
lastUpdateId
. - Apply the update procedure below to all buffered events, and then to all subsequent events received.
To apply an event to your local order book, follow this update procedure: - If the event
u
(last update ID) is < the update ID of your local order book, ignore the event. - If the event
U
(first update ID) is > the update ID of your local order book, something went wrong. Discard your local order book and restart the process from the beginning. - For each price level in bids (
b
) and asks (a
), set the new quantity in the order book:- If the price level does not exist in the order book, insert it with new quantity.
- If the quantity is zero, remove the price level from the order book.
- Set the order book update ID to the last update ID (
u
) in the processed event.
Is this change actually implemented? These changes are not mentioned in the changelog, Do users actually need to change to the new logic?