Mattheus Public Preview

Join Waitlist

Mattheus Public Preview

Current Blog

How to Model Limit Orders in a Trading Backtest

A limit-order backtest needs venue-specific priority, queue position, partial fills, cancellation timing, and a clear rule for data the simulation cannot observe.

Published Updated
A cobalt limit order waits behind violet queue blocks and receives a partial fill through a warm matching gate

By alyc

This article was prepared with AI assistance and checked through automated editorial and source review. No named human review is recorded.

A reliable limit-order backtest should not record a fill merely because the market traded at the order's price. It should first decide whether the order was active, marketable, accepted by the venue, and eligible under that venue's priority rules. It should then estimate how much opposing volume reached the order after higher-priority interest was satisfied.

The result is a fill path, not a binary flag. Store each simulated execution with its timestamp, venue, price, shares, queue estimate, source events, latency assumptions, and remaining order size. If the available data cannot support queue reconstruction, say so and use a declared conservative model.

Price protection is not an execution guarantee

The SEC defines a limit order as an instruction to buy or sell at a specified price or better. It also states that a limit order is not guaranteed to execute. Reaching the price is necessary in many cases, but it does not prove the order received a fill.

A resting buy order can remain unfilled when other orders are ahead at the same price, when trades occur on another venue, when the displayed size disappears before the simulated order arrives, or when the order is canceled or repriced. A marketable limit order can fill immediately, partially fill, route elsewhere, or leave a remainder, depending on its instructions and available liquidity.

Begin with the order lifecycle. Record decision time, transmission latency, venue receipt time, acceptance or rejection, working price, displayed status, routing instruction, time in force, modifications, executions, cancellations, and expiry. Align those events to the same market calendar and timestamp policy used by the market data.

Match the venue's priority rule

Priority is venue specific. Nasdaq Rule 4757 states that orders on its book are presented using a price, display, and time algorithm. Better prices rank first. At the same price, displayed orders rank in time priority, followed by non-displayed interest under the rule's stated ordering. The current Nasdaq Equity 4 rulebook is the primary source for that venue.

A backtest should therefore carry venue, order type, display attribute, price, receipt timestamp, and every event that can change priority. Some modifications receive a new timestamp. Reserve replenishment can create a new displayed order. Routed orders may become subject to another market's procedures. A single universal first-in-first-out assumption is not enough.

Do not infer your exact position from top-of-book size alone. That feed may omit non-displayed interest and may aggregate orders from many participants. The model's name should reflect its evidence, such as displayed_queue_estimate, rather than claiming an observed exchange queue when the data does not contain one.

Worked partial-fill example

Assume a buy limit order for 500 shares reaches one venue at $25.00. The historical feed shows 600 displayed shares already bid at $25.00. After the order becomes active, 900 shares of eligible sell volume execute against that venue at $25.00 before the buy order is canceled. Ignore hidden liquidity, order modifications, and cancellations ahead for this illustration.

Under a strict price-time queue estimate, the first 600 shares satisfy displayed interest ahead. The next 300 shares can reach the simulated order. The model records a 300-share partial fill and leaves 200 shares unfilled. Arithmetic: max(0, 900 - 600) = 300, capped at the order's 500-share size.

This is a synthetic example, not an observed execution result. The units are shares and US dollars. The assumptions are one venue, one price level, known active time, no better-priced interest, no hidden size, no queue cancellations, and no latency after acceptance. Relaxing any of those assumptions can change the answer.

Choose a fill model that fits the data

With market-by-order data, reconstruct individual visible orders and update the simulated position as additions, executions, cancellations, and replacements arrive. Even then, venue messages and hidden interest require careful interpretation.

With market-by-price depth, track aggregate displayed size ahead. A conservative rule can credit executions at the limit price only after displayed size present before arrival has been consumed. Decide how to treat cancellations ahead. Giving the simulated order credit for every cancellation is optimistic; giving it credit for none is conservative. Report both when the uncertainty matters.

With trades and best quotes only, exact queue position is not identifiable. Use coarse scenarios instead of false precision. One scenario can require the quote to trade through the limit. Another can allocate only a fraction of same-price volume after a minimum dwell time. The parameter belongs in the run manifest and sensitivity analysis.

Whatever the model, use only events available after the order became active. Reading the full bar's high, low, and volume before deciding the fill introduces look-ahead bias.

Model marketability, time in force, and cancellation

Classify an order at receipt against the relevant quote. The SEC's 2026 Rule 605 guidance distinguishes marketable, executable non-marketable, and non-marketable limit orders in worked examples. It also notes that an immediate-or-cancel instruction means the order should execute immediately or cancel.

For a day order, stop eligibility at the venue's session boundary. For IOC, cancel the unfilled remainder immediately after the execution attempt. For a good-til-canceled order, carry the order only if the data and strategy explicitly support that lifecycle. Rejections, trading halts, price bands, and session rules can make an order ineligible even when a bar appears to cross its limit.

Cancellation latency matters. A cancel decision at 10:00:00.100 does not prove the order disappeared at that instant. If a fill arrives before the venue processes the cancel, the simulated order can still execute. Preserve decision, send, receipt, and acknowledgement timestamps separately.

Measure the execution model, not just returns

The SEC's Rule 605 amendments require finer timing and expanded execution-quality reporting, including statistics for non-marketable limit orders from the time they become executable. A research backtest is not a regulatory report, but the measurement categories are useful prompts.

Track fill rate, partial-fill rate, time to first fill, time to completion, canceled shares, average fill price, adverse selection after the fill, and the fraction of results coming from optimistic queue assumptions. Keep explicit trading-cost assumptions beside the fill model. Maker rebates or fees do not repair an unrealistic fill.

Connect every simulated execution to the source events and model version through data lineage. A reviewer should be able to identify which trades, quotes, depth updates, and latency rules produced the fill.

Validate invariants before strategy logic

Useful tests include the following.

  • No fill occurs before venue acceptance or after cancellation acknowledgement.

  • A buy never fills above its active limit and a sell never fills below it.

  • Cumulative filled shares never exceed active order size.

  • Every partial fill decrements the remaining size by the same quantity.

  • Volume at other venues does not consume the queue unless routing is modeled.

  • A priority-changing modification receives the treatment required by the venue rule.

  • Running the model with less optimistic queue assumptions cannot increase same-price fills.

Test a full immediate fill, a partial fill, no fill despite a touched price, cancellation during a queue wait, a repricing event, a session expiry, and a data gap. Re-run the scenarios inside each walk-forward test so execution parameters are not chosen with knowledge of later results.

A limit-order simulation is credible when its uncertainty is visible. Use the most detailed data available, bind the logic to the actual venue rule, and publish results across conservative and less conservative fill assumptions rather than hiding queue uncertainty behind perfect executions.

Explore Markets with Infinite Context.

Explore Markets with Infinite Context.

Browse the public preview. Join the waitlist for gated trading features.

Browse the public preview. Join the waitlist for gated trading features.

Browse the public preview. Join the waitlist for gated trading features.

Dashboard interface preview