Implement a simplified NASDAQ market data book for one symbol. You receive callbacks when orders are inserted, modified, or canceled. Each order has an id, side, price, and quantity. Implement GetPriceLevel(side, level_index), where level 0 is the best price: highest price for buy orders and lowest price for sell orders. Return the price and total quantity at that price level.
Function signature:
class OrderBook:
def on_order_insert(self, order_id: int, side: str, price: int, qty: int) -> None:
pass
def on_order_modify(self, order_id: int, price: int, qty: int) -> None:
pass
def on_order_cancel(self, order_id: int) -> None:
pass
def get_price_level(self, side: str, level_index: int) -> tuple[int, int] | None:
pass
Constraints:
-
side
is either
'buy'
or
'sell'
.
-
Order ids are unique while active.
-
Modifying an order may change both price and quantity.
-
Canceling an unknown order should be handled gracefully or specified as invalid.
-
If the requested level does not exist, return
None
.
Examples:
Insert buy id 1 price 100 qty 5; insert buy id 2 price 101 qty 3. get_price_level('buy', 0) returns (101, 3).
Insert sell id 3 price 105 qty 2; insert sell id 4 price 105 qty 7. get_price_level('sell', 0) returns (105, 9).