FREE FINANCIAL EDUCATION
Cancel-and-replace race conditions
Reconcile fills that occur while cancellation is pending.
What you will learn
Prerequisites: Order acknowledgements, partial fills and target quantities.
- Reconcile fills that occur while cancellation is pending.
- Calculate a replacement quantity from target minus actual confirmed position.
- Avoid duplicate exposure from treating a cancel request as completion.
Mechanism and decision rules
Cancel-and-replace involves asynchronous events. A cancellation request can cross with an execution already occurring at the venue. The final response may confirm that only the remaining quantity was canceled, while additional fills arrived after the request. A strategy must process both execution and cancellation messages rather than assuming the button press instantly removed the order.
Replacement size should follow the intended total position minus actual filled quantity and any still-live quantity, under the broker’s supported workflow. Reusing a stale remainder can overtrade. Unique client identifiers, sequence handling and reconciliation help distinguish retries from genuinely new orders. The exact API semantics must be checked in the live environment.
Worked example · synthetic data
A synthetic buy order requests 100 units. Forty fill, leaving 60. A cancel request is sent, but 20 more fill before cancellation is confirmed for the remaining 40. The actual position is 60, not 40. If the desired total remains 100, a new replacement should request 40 under a fully reconciled state.
Submitting the stale 60-unit remainder would create 120 total units if it fills. Submitting a fresh 100 would create 160. The error arises from stale state, not from the market signal, so strategy profitability cannot compensate for it.
Failure modes and limits
Messages can arrive late, duplicated or out of order. Some APIs provide atomic replace semantics while others require separate cancel and new orders; do not assume equivalence. A timeout is uncertainty, not proof of failure. Reconciliation should include open orders and fills, not only the displayed position.
Practice case
An order for 200 fills 70, then another 30 during cancellation. The remaining 100 are confirmed canceled. Desired total is 150. What replacement quantity is appropriate, assuming no other live orders?
Reveal the worked answer
Actual filled quantity is 100. To reach 150, request 50 additional units. The original requested 200 and the stale 130 remainder are no longer the correct sizing references.
Check your understanding
Sources and next steps
The lesson, calculations and synthetic cases are original educational material. References explain underlying contract or chart conventions; none validates the profitability of a trading rule. Actual contract specifications, settlement obligations and charges must be checked before trading.
Explore OHLCV data in the chart lab · Explore volume-at-price and TPO data · Practice library
