order book handling problem

Hi every one

I have problem with order book data ! I get data from web socket and process it with this steps :

  1. get the ask and bids data from https://api.binance.com/api/v3/depth?symbol&limit=1000

  2. save these data in a local variable (variable name is ‘OrderBooks’)

  3. then get data from web socket and check lastUpdateID with this code

    //— get last update id
    var lastUpdateId = OrderBooks.lastUpdateId;

    //— drop any updates older than the snapshot
    if (data.u <= lastUpdateId) {

         console.log('discard data');
         return;        
    

    }

    if (data.U <= (lastUpdateId + 1) && data.u >= (lastUpdateId + 1)) {
    OrderBooks.lastUpdateId = data.u;
    }
    else if (data.U == (lastUpdateId + 1)) {
    OrderBooks.lastUpdateId = data.u;
    }

  4. process new update data in three conditions :

    4.1.  Update data if price level exist and have new quantity
    4.2. Remove where price level have zero quantity
    4.3. Insert in orderBooks List if new price level
    
  5. show first ten data of list

with these steps i have this problem :

thank you for your help

I think you should sub from the websocket before you call the API to cover the time gap between #2 and #3. You can refer to this https://github.com/binance-exchange/binance-websocket-examples/blob/master/src/lib/orderBook.js

Thank you for answer ,
I have correct data but I don’t know sorting or showing algorithm in Binance panel .

I put the update in snapshot list and sort that descending . but there is very difference between me and binance panel !

do you know what is algorithm or can explain step by step what do i do ?

thank you my friend

Already share with you the sample code (algorithm included) and you obviously know the instructions of maintaining a local order book. The rest of them are development questions and out of the scope of this forum