Current Blog
How to Model Queue Position in a Trading Backtest
A queue-aware backtest tracks price priority, time priority, executions, cancellations, replacements, and the evidence available at each event.
This article was prepared with AI assistance and checked through automated editorial and source review. No named human review is recorded.
A queue-aware backtest should not fill a resting limit order merely because trades occurred at its price. The order must first reach the front of the eligible queue under the venue's matching rule. Executions, cancellations, replacements, hidden liquidity, latency, and missing market-data events can all change that result.
Store the venue, instrument, side, price, quantity, order acceptance timestamp, local receive timestamp, displayed quantity ahead, every later event at that price, executed quantity, fees, and remaining quantity. Label whether the queue position is reconstructed exactly, bounded as an interval, or estimated from a model.
Start with the venue's matching rule
Nasdaq describes its equity book as price-time priority for displayed limit orders. Orders at the same price execute in the order received, while non-displayed shares execute after displayed shares at that price. That rule is a starting point for a Nasdaq equity simulation, not a universal market rule.
CME Group documents several matching algorithms, including FIFO, pro rata, and hybrid variants. A futures backtest must load the algorithm for the product and historical date. Reusing a FIFO queue for a pro rata contract changes both fill probability and fill size.
Reconstruct the order book from sequenced events
The Nasdaq TotalView-ITCH specification describes a sequenced feed with add, execute, cancel, delete, and replace messages. Those events let a researcher track the displayed life of an order and update quantity ahead at one price level.
Process messages in exchange sequence order. Use exchange timestamps for market ordering and local receive timestamps for what the strategy could know. Apply the strategy's outbound latency before placing its simulated order in the reconstructed book. The order joins behind displayed quantity already present when the venue accepts it.
A replacement deserves explicit treatment. If the venue assigns a new order reference or the applicable rule resets priority, do not preserve the old queue position. Keep the original and replacement events in the data-lineage record.
Worked FIFO example
Consider a synthetic Nasdaq buy order for 3,000 shares at $25.00. When the order is accepted, 12,000 displayed shares are ahead at that price. Later, executions remove 7,500 shares ahead and identified cancellations remove another 2,000. The reconstructed quantity ahead is now 2,500 shares.
An incoming sell executes 1,000 shares at $25.00. All 1,000 shares consume quantity ahead, so the strategy receives no fill and 1,500 shares remain ahead. A later sell executes 3,000 shares. The first 1,500 clear the remaining queue ahead and the next 1,500 fill half of the strategy order. The model records a 1,500-share fill and 1,500 shares still resting.
The numbers are illustrative, not empirical results. They assume complete order-level messages, FIFO priority for the eligible displayed orders, no hidden quantity with superior priority, and an accepted strategy order at the stated time. Fees and adverse selection belong in the separate trading-cost model.
Do not turn aggregate depth into false precision
Market-by-price data shows total quantity at a level but usually does not identify which cancellation occurred before or after the strategy order. Subtracting every cancellation from quantity ahead is optimistic. Assuming every cancellation happened behind the order is conservative. Report both paths as bounds when the data cannot resolve the location.
Trade prints alone are weaker still. A print at the limit price does not establish the venue, displayed queue, hidden interest, or whether the strategy order had arrived. Bar volume cannot support an exact queue claim. In that case, use a documented fill model and label its outputs as estimates.
Keep trigger, queue, and fill logic separate
The strategy decision creates an order request. Venue acceptance places the order into an eligible queue after latency and rule checks. Later events reduce quantity ahead or fill the order. Keeping these states separate prevents a signal timestamp from becoming an impossible execution timestamp.
Use the same price constraints and partial-fill accounting as the limit-order model. Halt and resume events also matter. A queue should not keep processing ordinary executions while the instrument is in a non-trading state, and the post-halt book may require a fresh reconstruction under the halt model.
Test the queue model with invariants
Run explicit checks against every simulated fill.
No order fills before venue acceptance.
Quantity ahead never becomes negative.
Executed quantity never exceeds the accepted order quantity.
A cancellation reduces quantity ahead only when the data identifies it as ahead or the model labels that assumption.
Replace events apply the venue's historical priority rule.
Sequence gaps stop exact reconstruction until recovery completes.
Hidden and reserve interest are not invented from displayed depth.
Price, quantity, fees, and cash changes reconcile after every partial fill.
Also test a sequence gap, an order accepted between two feed messages, a cancel behind the strategy, a replace that loses priority, a partial execution, a trading halt, and a pro rata product. The result is auditable only when the matching rule, event stream, latency assumption, queue updates, and final ledger can be reconstructed.




