XCore HFT / Trading Lab
Trading risk includes the machinery
Quantitative execution, market-microstructure, and risk-control research from XTRSK.
What this problem really is
The majority of a prop‑trading desk’s edge sits in the model that predicts price movement, but the final profit‑and‑loss line is drawn by the execution machinery that turns a theoretical signal into a real trade. When the machinery is out of sync with the market – for example because an order is sent twice, a stale quote is used, or a recovery routine re‑issues a request that the venue has already filled – the model can be perfectly calibrated and still generate a loss. The loss is not a statistical variance; it is a deterministic leakage caused by the state‑management layer. In practice the leakage appears as unexpected fills, extra slippage, or outright order rejections that the risk‑monitor never anticipated.
The root cause is a mismatch between the internal representation of “what we have on the book” and the venue’s actual queue. Modern electronic markets are price‑time‑priority queues: each order occupies a position that confers a probabilistic right to capture the spread, but also carries an adverse‑selection cost that grows as the queue moves. If the system believes an order is still at the front of the line while the venue has already filled or cancelled it, the next decision – whether to amend, cancel or let it sit – will be based on a false premise. The resulting actions can double‑execute a trade, leave a ghost order that incurs fees, or cause the algorithm to over‑hedge.

How the mechanism works, step by step
First, an order request leaves the strategy layer and is handed to the gateway. The gateway stamps the request with a globally unique identifier and records the exact timestamp, price, size and venue. The identifier is used to make the submission idempotent: if the same identifier is received again, the venue must reject it as a duplicate, and the gateway must recognise the rejection as a no‑op.
Second, the gateway maintains a shadow order book that mirrors the venue’s queue. Every acknowledgement, fill, partial fill, or cancellation received from the venue is applied to the shadow book, and the event is appended to an immutable log. A reconciliation loop runs continuously, comparing the shadow state with a lightweight checksum obtained from the venue’s order‑status API. If a divergence is detected – for example the shadow book still shows an open order while the venue reports it as filled – the system triggers a corrective workflow that either rescinds the stale order or updates the internal state before any further decisions are made.
A worked example with real numbers
Consider a market‑making algorithm that posts a bid of 10 250 USD for 100 contracts on a futures contract. The latency from strategy to gateway is measured at 5 ms, and the venue’s order‑book depth at that price is 150 contracts, giving the algorithm a 66 % chance of being at the front of the queue. At 12:30:01.123 the gateway sends the order with identifier A1‑20231102‑001.
At 12:30:01.128 the venue acknowledges the order and assigns it a venue‑side identifier V‑98765. The shadow book now records the order as live. Five milliseconds later the market moves, and the algorithm decides to cancel the bid and replace it with a new price of 10 260 USD. The cancel‑replace message is sent with the same client identifier A1‑20231102‑001, relying on idempotence to avoid a duplicate.
Before the cancel reaches the venue, the gateway process crashes and is restarted. The restart script re‑initialises the gateway but, by mistake, does not reload the shadow book. Consequently the new instance believes there are no live orders for that symbol. At 12:30:01.140 the gateway receives the original cancel‑replace request from the strategy again, now with a fresh client identifier A1‑20231102‑002. Because the shadow book is empty, the gateway treats this as a brand‑new order and forwards a fresh bid of 10 260 USD for 100 contracts.
Meanwhile the original order at 10 250 USD is still alive on the venue, waiting to be filled. Over the next 200 ms the market ticks up and the original order is filled for the full 100 contracts at an average price of 10 251 USD. The newly issued order sits at the back of the queue and is later cancelled at a loss of 10 260 USD – 10 260 USD × 100 = 1 026 000 USD of exposure that never materialised. The net result is a realised loss of 1 000 USD (the spread capture that the model expected) plus the commission on the ghost order, entirely due to the failure to restore live order state after a restart.

Where it breaks in live markets
In a live, high‑frequency environment the failure modes are rarely isolated. Feed latency can cause the algorithm to act on a price that is already obsolete, while the gateway’s internal queue still believes the order is at the front. If the feed age exceeds a few milliseconds, the expected spread capture evaporates, but the system may still keep the order alive because it has not reconciled the stale quote.
Network partitions introduce another class of error. When a TCP connection to the venue drops, the gateway may buffer outbound messages. Upon reconnection, the buffered messages are flushed in bulk, potentially re‑issuing cancels that the venue never received, or sending new orders that duplicate those already on the book. Without a robust failover design that tracks which messages were acknowledged, the desk can end up with double execution or “zombie” orders that sit on the exchange for hours, accruing fees and attracting regulatory scrutiny.
Finally, the reject rate is a leading indicator of state mis‑alignment. A sudden spike in order‑rejection messages – for example “duplicate order” or “invalid order state” – often signals that the internal order‑book is out of step with the venue. If the monitoring stack does not surface this pattern quickly, the algorithm will continue to issue new orders based on a false premise, compounding the loss.
The operating path, stage by stage
The first stage is signal generation, where the model outputs a price level and size. The second stage is order construction, during which the client identifier is generated and the request is signed. The third stage is transmission, where the gateway writes the request to a durable queue and forwards it to the venue. The fourth stage is acknowledgement, where the venue returns a confirmation that the order has been accepted, rejected, or partially filled. The fifth stage is state update, where the shadow book and immutable event log are synchronised with the venue’s response.
A sixth stage – reconciliation – runs in parallel to the primary flow. It periodically queries the venue’s order‑status endpoint, computes a checksum of the internal shadow book, and raises an alert if the two diverge. The seventh stage is recovery, which may involve resending a cancel for a stale order, or re‑instating a missing order with the original client identifier to preserve idempotence. The final stage is post‑trade analytics, where the realised P&L is compared against the model’s expectation, and any discrepancy is fed back into the risk‑control dashboard.

Controls that act before the damage
The first line of defence is to make every order submission idempotent. By embedding a cryptographically random client identifier that never repeats, the venue can safely reject duplicates, and the gateway can detect accidental re‑submission before it reaches the market.
Second, continuous reconciliation between internal and venue state catches mismatches within a few milliseconds. The reconciliation engine must be able to ingest both the event stream from the gateway and the periodic snapshots from the venue, and raise a deterministic exception when the shadow book reports an order as live while the venue reports it as filled or cancelled.
Third, a failover design that preserves the exact in‑flight message set across process restarts eliminates double execution. This is achieved by persisting the outbound queue to a write‑ahead log before acknowledging any transmission, and replaying only those messages that lack a venue acknowledgement after a crash.
Fourth, monitoring of feed age, queue depth and reject rate provides early warning. If the median age of market data exceeds a configurable threshold, the algorithm should automatically switch to a degraded mode that reduces order size or pauses new submissions.
Fifth, an immutable event trail – stored in a tamper‑evident ledger – guarantees that every state transition can be audited, which is essential for post‑mortem analysis and regulatory compliance.
Sixth, practising degraded‑mode operation in simulation ensures that the team knows exactly how the system should behave when a component is unavailable, rather than improvising under pressure.

How to measure whether it is working
The primary metric is the duplicate‑order rate, defined as the number of venue‑rejected duplicate messages divided by total order submissions. A healthy system keeps this rate below 0.01 %. The second metric is the stale‑state exposure, measured as the notional value of orders that remain live in the shadow book after the venue has reported them as filled or cancelled. This should be close to zero, with a tolerance of one contract per instrument.
Latency‑age distribution is another key indicator: the 95th percentile of market‑data age must stay under the maximum tolerable latency for the strategy, typically 2 ms for ultra‑low‑latency market making. Queue‑depth variance, computed as the standard deviation of the venue‑reported depth at the order price over a rolling five‑minute window, should be stable; sudden spikes often precede state mismatches.
Finally, the reconciliation lag – the time between a venue state change and its reflection in the shadow book – must be measured continuously. A lag exceeding 1 ms on a sub‑millisecond strategy is a red flag that the control loop is too slow. By tracking these quantitative signals, the desk can verify that the protective machinery is functioning as intended.
The portfolio view
When the execution machinery operates correctly, the realised P&L of a portfolio tracks the model‑predicted P&L within a narrow band that reflects market friction, such as spread and commission. When state‑management errors appear, the deviation widens dramatically, often in a pattern that mirrors the size of the duplicate or stale orders.
For a diversified portfolio of 30 instruments, a 0.5 % increase in duplicate‑order rate can translate into a multi‑million‑dollar drag on performance, because the error compounds across all venues and instruments. Conversely, a well‑engineered idempotent submission pipeline can reduce that drag to negligible levels, allowing the pure alpha of the model to shine through.
Risk‑adjusted metrics such as Sharpe ratio and information ratio become more reliable once the noise introduced by execution leaks is removed. Moreover, the capital allocation framework can be tightened because the desk no longer needs to hold excessive buffers against execution‑related tail risk. In effect, robust machinery turns a “model‑only” risk assessment into a true portfolio‑wide risk picture.
—
If your current order‑management stack could silently double‑execute a 100‑contract order during a gateway restart, what concrete steps will you take today to make the submission process truly idempotent?
About the research behind this lesson
The seminar frames electronic markets as price-time-priority queues. It separates queue value into spread capture versus adverse-selection cost and the option value of retaining a place in line.
Applied to this lesson: An order lifetime therefore cannot be based on elapsed time alone: the system must reassess whether its queue position, expected spread and adverse-selection risk still justify keeping the order alive.
Two live systems, one desk
PULSE-X — power-packed HFT: high-frequency momentum on XAUUSD and EURUSD. System details · Live account
NIC-X — ultra-low drawdown: displaced-print arbitrage for large capital. System details · Live account
Start now: leave your email and request a free trial — we will send the pricing, the connection route and the trial terms. Pricing
Join the market discussion: Telegram @squawklive
Source: High-Frequency Trading and Modern Market Microstructure
Educational content only. Trading leveraged products involves risk.
Chat with XTRSK
Chat ready
Start a chat and the XTRSK team will be notified immediately.