Cross-Exchange Best Quote
MediumDesign ยท Citadel
Implement OrderBook, which tracks live limit orders on several exchanges. Orders never match each other.
add(order_id, exchange, side, price, qty)places an order;sideis"bid"or"ask".cancel(order_id)removes a live order and returnsTrue, or returnsFalseif no live order has that id.replace(order_id, price, qty)gives a live order a new price and quantity, keeping its exchange and side, and returnsTrue. For any other id it returnsFalseand changes nothing.bbo(exchange)andnbbo()return[bid_price, bid_qty, ask_price, ask_qty]for that exchange, or for all exchanges together: the highest bid and lowest ask, each with the total quantity of live orders at exactly that price. An empty side gives-1, 0.
Example 1
- Input:
- OrderBook(), add(1, "NYSE", "bid", 100, 5), add(2, "NYSE", "bid", 100, 3), add(3, "ARCA", "bid", 101, 4), add(4, "ARCA", "ask", 103, 2), bbo("NYSE"), nbbo(), cancel(3), nbbo()
- Output:
- [null, null, null, null, null, [100, 8, -1, 0], [101, 4, 103, 2], true, [100, 8, 103, 2]]
- Explanation
- NYSE's two bids at 100 total 8. ARCA's bid at 101 leads the national book until it is cancelled.
Example 2
- Input:
- OrderBook(), add(1, "A", "ask", 50, 2), add(2, "B", "ask", 50, 7), nbbo(), replace(1, 49, 1), nbbo(), cancel(1), replace(1, 40, 9), cancel(1)
- Output:
- [null, null, null, [-1, 0, 50, 9], true, [-1, 0, 49, 1], true, false, false]
- Explanation
- A cancelled order cannot be replaced, and a replace on it adds nothing.
Example 3
- Input:
- OrderBook(), add(1, "A", "bid", 105, 1), add(2, "B", "ask", 100, 1), bbo("C"), nbbo()
- Output:
- [null, null, null, [-1, 0, -1, 0], [105, 1, 100, 1]]
Constraints
1 <= order_id <= 10^9, andaddnever reuses anorder_idexchangeis a non-empty string of at most 10 letters1 <= price, qty <= 10^9- At most
10^5calls in total