Viewed
Coding
Anthropic
Claude Opus 4.8
VS
Google
Gemini 2.5 Flash
Implement a Deterministic Limit Order Book Simulator
Write a single-file Python 3.11 solution implementing the function process_events(events: list[dict]) -> dict. Do not use external packages. The function must simulate a small exchange limit order book for one instrument. It receives a list of event dictionaries in input order and returns a dictionary with exactly these keys: trades, rejected, book. Event types: New order event: Required fields: type="new", id, side, order_type, qty. side is "buy" or "sell". order_type is "limit" or "market". qty is a positive integer. A limit order also requires price, a positive integer number of cents. Optional field tif is time-in-force: "GTC", "IOC", or "FOK". If absent, use "GTC" for limit orders and "IOC" for market orders. Market orders may not have tif="GTC" and may not rest on the book. Cancel event: Required fields: type="cancel", id. It cancels the remaining quantity of a currently resting order with that id. Matching rules: The book has bids and asks. Resting buy limit orders are bids; resting sell limit orders are asks. Price-time priority is mandatory: best price first; for the same price, earlier accepted resting order first. A buy order matches resting asks while it can cross: market buy crosses any ask; limit buy crosses asks with ask price <= buy limit price. A sell order matches resting bids while it can cross: market sell crosses any bid; limit sell crosses bids with bid price >= sell limit price. Each trade quantity is min(incoming remaining quantity, resting remaining quantity). Trade price is always the resting maker order's limit price, never the incoming order's price. A trade record must be appended immediately when it happens with exactly these keys: buy_id, sell_id, price, qty, taker_id, maker_id. Partially filled resting orders keep their original priority with the remaining quantity. Fully filled orders leave the book. Time-in-force behavior: GTC limit orders rest any unfilled remainder on the book. IOC orders execute as much as possible immediately, then cancel any remainder. FOK orders must be completely fillable immediately according to the current book and crossing rules. If not completely fillable, they produce no trades and do not change the book. If completely fillable, execute normally. FOK orders never rest. Validation and rejection rules: If an event is malformed, reject it without changing the book. Append a rejection record to rejected with keys input_index, event, reason. The reason may be a short human-readable string. Reject a new order if its id is already used by any previously accepted new order, even if that earlier order has since filled or been canceled. Reject cancel events for unknown ids or ids that are no longer resting. Reject non-integer, zero, or negative qty and price values. In Python, bool must not be accepted as an integer for these fields. Ignore extra fields on otherwise valid events. Return format: trades: list of trade records in execution order. rejected: list of rejection records in input order. book: a dictionary with keys bids and asks. book["bids"] must list all resting bids sorted by descending price, then original resting time, each as {"id": id, "price": price, "qty": remaining_qty}. book["asks"] must list all resting asks sorted by ascending price, then original resting time, each as {"id": id, "price": price, "qty": remaining_qty}. Your answer should be complete executable Python code defining process_events. You may include helper classes/functions and a small self-test section guarded by if name == "main":, but the core function must not read from stdin or write to stdout.