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!