In price-time priority electronic order matching at Optiver and Tower Research, limit orders resting at the Best Bid or Best Ask queue up in strict First-In-First-Out (FIFO) sequence.
Suppose your trading algorithm posts a limit order of size at price . Ahead of your order in the queue rests an initial volume . Subsequently, an event stream of external executions and cancellations occurs at price :
(timestamp, 'trade', volume): Consumes resting liquidity in FIFO order.(timestamp, 'cancel', volume): Volume canceled ahead of your position reduces (up to ). Cancellations do not affect your own resting shares.Your Task
Implement simulate_queue_fills(initial_ahead: int, own_qty: int, events: list[tuple[float, str, int]]) -> dict that simulates the queue and returns a dictionary with:
'first_fill_time': float timestamp when your order begins executing (or None if no shares were executed).'full_fill_time': float timestamp when your order is completely filled (or None if unfulfilled).'filled_volume': int total number of shares filled.'remaining_volume': int shares remaining unfilled.Constraints
Examples
events = [
(1.0, 'trade', 30),
(2.0, 'cancel', 20),
(3.0, 'trade', 60)
]
# initial_ahead = 50, own_qty = 40
# t=1.0: trade 30 -> ahead = 20, own = 40
# t=2.0: cancel 20 -> ahead = 0, own = 40
# t=3.0: trade 60 -> ahead=0, fills all 40 of own! first_fill=3.0, full_fill=3.0