binance-exchange/node-binance-api

Order Book Issue

cecilymiller opened this issue · 4 comments

I've issues to handle local order book. The docs says:

The first processed event should have U <= lastUpdateId+1 AND u >= lastUpdateId+1.

At the end of: https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md#how-to-manage-a-local-order-book-correctly

But the first check never passes

  1. I fetch market depth from http://api.binance.com/api/v3/depth

  2. I'm listening depth websocket stream like so

binance.websockets.depth("BNBBTC", (depth) => {
    //
});
  1. Then I have the following checks:
if (depth.U <= lastUpdateId + 1) {
  console.log("First check passes");
}

AND

if (depth.u >= lastUpdateId + 1) {
   console.log("Second check passes");
}

But the first check never passes. So, is there a mistake in the docs, or is the docs only for v1 and if so, how should I handle the order book when using API v3?

This is already handled in binance.websockets.chart, for anything else you will need to ask here https://t.me/binance_api_english

I'm sorry, what you mean by

This is already handled in binance.websockets.chart

Aren't those completely different datasets?

In the future I'll use telegram, I'm tying to get this solved in here if that's ok

Or are you saying the first check is redundant?

Hello Cecily, I think the problem is you keep fetching snapshots. You only need 1 snapshot and then start processing the events. The first event to be processed is the event where its U <= snapshot.lastUpdateId AND its u >= snapshot.lastUpdateId.

However, you also need to make sure that for each event processed AFTER the first event, currentEvent.U == previousEvent.u + 1.

If the previous condition breaks at any moment, you will need to fetch another snapshot and repeat.. etc

Hope that helps, that's how I implemented it. Feel free to ask for more insight.