Reputation: 251
I made a simple Python script that should stream the Binance Bitcoin-USD orderbook. I followed the guide here. I subscribed to their websocket stream and started updating my local orderbook. The problem is that the size of the orderbook keeps growing, and i don't know if that's normal. It started from a length of around 1000 rows, after 13 hours it is at around 4000. Is this normal or am i doing something wrong?
Here is how i'm updating the orderbook:
1) Retrieve a copy of the partial orderbook from the API endpoint https://www.binance.com/api/v1/depth?symbol=BNBBTC&limit=1000
2) Take that data, convert it to a dictionary like the following Partial = {'asks:'{...}, 'bids': {...}}
, i do this because a dict is easier to update
3) Take every row in the update and update my local dict with the new data using price as the key. Then i make a loop through the dict and delete every row that has value 0.000000
Code:
#Here is the payload received by the websocket stream
Update = message['data']
#Update bids
for x in Update['b']:
Partial['bids'].update({x[0]: x[1]})
#Update asks
for x in Update['a']:
Partial['asks'].update({x[0]: x[1]})
#Remove rows where the value is 0
DelBids = ({k:v for k,v in Partial['bids'].items() if v != '0.00000000'})
DelAsks = ({k:v for k,v in Partial['asks'].items() if v != '0.00000000'})
Where Partial
is the dictionary where i'm storing the copy of the orderbook i retrieved from the API call (see point 1). Any kind of advice is appreciated!
Upvotes: 0
Views: 3210
Reputation: 36
well, the size growing and that's normal. As the price changing, you gonna increase the global range of your order book. But there is a problem with your process. In fact, as I understand your process, you receive the partial order book and update with the price as key your local order book. the problem is that you keep track of order that doesn't exist anymore. In fact, when you receive the partial order book, you have to delete in your local order book all the price in the range of the partial data, if not, you keep old order that doesn't exist anymore. If the partial order book go from 9200 to 10200 for example, you have to delete from your local from 9199,99 to 10000,01 for example. you can see that in the api documentation: "Get a depth snapshot from https://www.binance.com/api/v1/depth?symbol=BNBBTC&limit=1000 . Drop any event where u is <= lastUpdateId in the snapshot. The first processed event should have U <= lastUpdateId+1 AND u >= lastUpdateId+1. While listening to the stream, each new event's U should be equal to the previous event's u+1." the main problem with this kind of local order book, is that you can't trust order price/quantities oustside of the range of the partial order book.
Upvotes: 2