Every orderbook message contains a 32-bit integer checksum (represented as a base-10 string). This checksum is used to compare a client's orderbook state versus the matching engine's orderbook state.
Should these checksums differ between client and server, the Websocket client is encouraged to re-subscribe to the channel to fetch the new orderbook snapshot in order to re-build the orderbook state.
- The preimage of the checksum is generated by concatenating each order's price and size, sorted by their price levels
- Only the best 100 price levels are included for purposes of checksumming.
- Format:
<best_bid_price>:<best_bid_size>:<best_ask_price>:<best_ask_size>:<second_best_bid_price>:<second_best_bid_size>:...
Example:
- orderbook:
{ "bids": ["9", "2"]], "asks": [["10", "1"] }
- resulting concatenation:
9:2:10:1
// Code Example
import zlib
def checksum(bids, asks):
preimage = ""
iterations = max(len(bids), len(asks))
for index in range(min(iterations, 100)):
if len(bids) > index:
price, size = bids[index]
preimage += price + ":" + size + ":"
if len(asks) > index:
price, size = asks[index]
preimage += price + ":" + size + ":"
preimage = preimage[:-1] # strip last colon
crc = zlib.crc32(preimage.encode("utf8")) & 0xFFFFFFFF
return crc
print(checksum([["9", "2"]], [["10", "1"]])) # 1226559413