Implement Order Book (L2) support
masterTo support order books, implement a handler (e.g., _book) that processes the exchange's book data format.
For exchanges that send the full book in every update (rather than incremental deltas), the handler should:
- Convert price and amount fields to
Decimal. - Store the bids and asks in a sorted dictionary structure (e.g., using
sdfor sorted dicts). - Call
await self.book_callback(symbol, L2_BOOK, False, False, msg['ts'])to notify the client of the update.
async def _book(self, msg):
symbol = self.exchange_symbol_to_std_symbol(msg['ch'].split('.')[1])
data = msg['tick']
self._l2_book[symbol] = {
BID: sd({
Decimal(price): Decimal(amount)
for price, amount in data['bids']
}),
ASK: sd({
Decimal(price): Decimal(amount)
for price, amount in data['asks']
})
}
await self.book_callback(symbol, L2_BOOK, False, False, msg['ts'])