Quantefakt

· 4 min

Who cancelled? Queue position in a backtest on L2 data

A matching engine that rebuilds the order book from L2 data sees the size at your price shrink, but not which orders left. Assume the worst, simulate latency, and when you replay the days you traded, remember that the recorded data now contains your own orders.


Your matching engine rebuilds the order book from L2 data. Then it has to answer a question the data cannot: who cancelled?

What L2 shows, and what it does not

L2 (level 2) data is the order book by price level: for every price, the total size resting there. It does not tell you which orders make up that total, or in which order they arrived.

How orders at the same price are filled depends on the venue, and often on the contract. Many match in price-time priority: the order that arrived first is filled first. At CME, for example, the E-mini S&P 500 matches that way, while the Three-Month SOFR futures match pro-rata: the order that bettered the market fills first, then fills are split by order size, and what is left over goes first in, first out (CME’s description). Under price-time priority a resting limit order has a place in a queue, and that place decides whether it gets filled. That is the case here.

You rest a limit order. You are not last in the queue. The size at your price shrinks. L2 shows you the new total, not which orders left.

A simulated order book rebuilt from L2 data. At the bid 100.01 the queue holds five orders of 100: two in front of you, you, and two behind you. The size drops from 500 to 300. Optimistic reading: the two in front cancelled, you are first. Conservative reading: the two behind you cancelled, you have not moved. Assume the worst: only a fill at your price moves you forward.

Two readings of the same number

The optimistic reading: the ones in front of you cancelled, so you moved up.

The conservative reading: they were behind you, and you have not moved.

Both fit the data. The optimistic one makes the backtest look better, because your order gets filled earlier and more often. That is exactly why it is dangerous: the fills you counted in the backtest are fills you may not get live.

Assume the worst

Unless you are last, a cancellation does not move you forward. Only a fill at your price does.

Then you get no surprises when you trade it live. If the strategy still works under the conservative reading, the real queue can only treat you better.

One exchange is the easy case

That works on a single exchange, such as a future, where every trade at your price happened in the book you are queued in.

US stocks are harder. They trade on many lit venues and in dark pools, and a print (a reported trade) at your price can come from another venue. It may not have touched your queue at all, so it is no proof that you moved forward.

Latency

Your order does not arrive in the book at the moment you decide to send it. The matching engine has to simulate that delay. Otherwise the backtest places your order in a queue it could never have reached in time.

The aftermath: you are your own competitor

After you trade, do the aftermath: replay the days you traded and compare the simulated fills with the real ones. That is how you find out whether the simulation is as conservative as you think.

But the recorded market data of those days now contains your own orders. Replay it as it is, and you are your own competitor: the simulation sees your real orders in the queue and in the prints, next to the simulated ones. The more precise you want the comparison, the more you have to strip your own deals out of the data.

Even L3 is not the truth

L3 data (market by order) shows every order with its own ID, so you know who cancelled. But a replay still holds the other participants fixed: they act as they did on the recorded day, not as they would have reacted to your order.

You can model the mechanics statistically, for example with Hawkes processes, where every order event makes further events more likely for a while (see the survey by Bacry, Mastromatteo and Muzy, Hawkes processes in finance). That reproduces how the order flow clusters. It does not reproduce why. The other participants have an intention, and intention cannot be modelled from statistics alone. The classic queue models make that explicit: in Cont, Stoikov and Talreja’s A stochastic model for order book dynamics (2010), the order flow is a set of independent Poisson processes, orders arriving at random with no strategy behind them.

So do the best you can, and still expect adverse selection: your order gets filled most often exactly when the price is about to move against you.

How does your simulator treat cancellations in front of you?

← All posts