Quant developer HFT course · Module 9: Trading systems
The life of an order
The question
A strategy decides to buy. Walk me through everything that happens to that order until it is done, and what the trading system must keep track of.
Vocabulary first
- Instrument: what is traded, a futures contract, a share, an option. The exchange identifies it by a number or a symbol.
- Bid and ask (or offer): the prices at which someone is willing to buy and sell. The best bid and best ask are the top of book; the gap between them is the spread.
- Tick size: the minimum price increment, fixed per instrument. Prices are always a whole number of ticks (Module 10).
- Limit order: buy or sell a quantity at a given price or better. If it cannot trade at once, it rests in the book.
- Market order: trade immediately at whatever prices are available. Rare in professional trading, because the price is unbounded.
- Time in force: how long an order lives. Day orders rest until the session ends; IOC (immediate or cancel) trades what it can at once and cancels the rest; FOK (fill or kill) trades in full at once or not at all.
- Fill (execution): a trade against the order. An order can be filled in several partial fills.
- Maker and taker: the resting order provides liquidity (maker); the order that trades against it takes it (taker). Exchanges often charge them different fees.
The lifecycle
- Intent. The strategy decides to buy 10 at 100.25.
- Risk checks. The gateway checks the order against limits (lesson 4). A failure stops it here.
- Send. The order gets a client order id, is recorded as pending new, and is encoded and sent on the session.
- Acknowledgement. The exchange accepts it and assigns its own order id: the order is new (live, resting). Or it rejects it: invalid price, closed market, risk limit at the exchange.
- Fills. Each fill reports price and quantity. The order becomes partially filled, then filled when its quantity is exhausted.
- Changes. The strategy may cancel the remainder or replace it (change price or quantity). Each request is pending until the exchange confirms it.
- End. Filled, cancelled, expired or rejected. The order is done; its fills feed the position and profit and loss.
The races
What makes order management hard is that requests and events cross in flight:
- Fill during cancel. The strategy sends a cancel; before it reaches the exchange, the order trades. The exchange sends a fill, then rejects the cancel because the order is already filled ("too late to cancel"). The system must count the fill and not assume the cancel worked.
- Replace and priority. On most venues, reducing quantity keeps the order's place in the queue, while changing the price or increasing quantity loses it, because the order is effectively new.
- Acknowledgement after fill. On some protocols, an aggressive order's fills may be reported before or in the same message as its acknowledgement.
- Unknown state. After a disconnect, the system does not know what happened to orders in flight. Session protocols resend missed messages on reconnection; until reconciled, those orders count against risk as if they might all fill.
So the position used for risk is not just filled quantity: it includes open quantity that could still fill, and pending requests are treated pessimistically.
What the system keeps
For each order: client id, exchange id once known, instrument, side, price, original and remaining quantity, filled quantity and average price, state, and pending requests. Lookups are by client id (for the strategy) and by exchange id (for incoming messages), usually hash maps into an order pool with stable handles (Modules 2 and 3).
For the account: positions per instrument, both filled and including open orders; P&L; and counts against rate limits.
Independently, a drop copy from the exchange or broker (a separate feed of all the firm's executions) is reconciled against the system's own record, catching any discrepancy.
Answering the question
Walk the states in order, and then spend the time on the races and the bookkeeping. Interviewers ask this to see if you understand that the trading system's view of an order is always slightly behind the exchange's, and that the code must be correct in that gap.
Check yourself
Question 1 of 3
An IOC buy for 10 can trade 6 immediately. What happens to the other 4?
Immediate or cancel trades what it can at once and cancels the remainder. Fill or kill would have rejected the whole order since 10 were not available.
Question 2 of 3
The strategy sends a cancel, then receives a fill for the full quantity, then a cancel reject. What is the final state?
The exchange processes events in its own order. The fill happened first there, so the order is filled and the cancel is rejected.
Question 3 of 3
Why must open orders count towards the position used for risk checks?
Risk must assume orders in the book may trade before the system can stop them.
In the interview
Say your own answer out loud first, then open the one we would give.
Describe the lifecycle of an order.
The strategy expresses intent; the gateway runs pre-trade risk checks, records it as pending new and sends it. The exchange acknowledges it, assigning its own id, or rejects it. It then receives fills, partial and complete, and the strategy may cancel or replace it, each request pending until confirmed. It ends filled, cancelled, expired or rejected, and its fills update the position and P&L.
What are the tricky cases in order management?
Messages crossing in flight: fills arriving while a cancel is pending, cancel rejects for orders that already filled, replaces that lose queue priority, and uncertainty after a disconnect. The system must treat pending requests pessimistically for risk and reconcile against the exchange and a drop copy.
What is the difference between IOC and FOK?
Immediate or cancel trades whatever quantity is available at once and cancels the rest. Fill or kill trades the full quantity at once or nothing at all.