Hey so I recently started a mini project and it’s been coming along slowly but surely. Anyway I have run into an issue I cannot figure out.
I am trying to keep a local record of the BTC-USDT orderbook. However after a little bit of time I notice that if the price goes down a little bit and then comes back up there are a couple ask prices that are not being removed. Essentially I have a couple ask prices that are lower than the highest or couple highest bid prices. This does not happen in the other direction IE, price goes up and comes back down; there are no bids higher than the best ask prices.
I also noticed that if the price goes down a bit, then up a bit (where there are a few asks that are lower than the best bid) then back down those prices that were lower than the best bid(s) do get removed but it’s a little too late. Again this doesn’t happen to the bid side (bid side always cleans up nicely) only the ask side. I also keep track of the
uID's to make sure I’m not missing any messages/updates.
Is this a known issue? I haven’t noticed it mentioned anywhere but should I just delete any asks that are lower than the best bid?
Just a little recap of what I’m doing (maybe I’m missing something? ) and as per the API documentation:
- I open a websocket to the diff. depth stream
- Buffer the incoming updates,
- Get an orderbook snapshot I get via REST
- First update applied has
U <= lastUpdateId +1and
u >= lastUpdateId +1
- As updates come in I make sure
Uor first update ID of that aggregate stream/message is equal to
uof the last message
- Apply these updates to my local orderbook (for both bids and asks) using the following rules (because the amount are absolute rather than relative):
- If the message price is in my orderbook and the amount is 0 I remove that price
- If the message price is in my orderbook but has an amount
!= 0I update that price amount with the new price amount
- If the message price is NOT in my order book and has an amount
!=0I add the price and amount to orderbook
- If the message price is NOT in my orderbook and the amount is
==0I ignore it
Initially everything looks fine and works/updates fine for a little bit but again after about 30-40 seconds I notice that if the market price goes down a little and back up there are some asks that are not updated and the best bids are greater than the best asks which is not possible. I made sure all my messages are being applied and that the
U ID’s of incoming messages are equal to
u +1of the old messages so no messages are being lost.
The issues does not seem to happen in the other direction, if market price goes up and back down there are no bids that are higher than the best asks.
Also if the market price goes down, then up, then back down most of the time the asks that were not supposed to be there do get removed or replaced.
Is this a known issue or am I missing something? Should I just automatically remove any asks that are below the best bid after a bid passes that ask price?
Any help is greatly appreciated and I would like to thank you in advance.