Order Lifecycle State Machine
Prerequisites: Trade Lifecycle Overview, Order Gateway and Risk Checks
Build the order gateway's record of where every AAPL order stands: a state machine with one state per order. The strategy and the risk checks act on it, because an order that can still trade is exposure and a done order is not.
- Acknowledgment: the exchange's message that it accepted the order.
- Fill: a message that some of the order's shares traded.
- Position: the net AAPL shares held, starting at 0.
What to implement
process_commands(commands)returns the printed lines, in order. Each command is one line and prints exactly one line.- Order ids and share quantities are positive integers. Every quantity, filled total, and position fits in a 32-bit
int. - On stdin, the first line is the number of commands, and each remaining line is one command.
- There are two parts, and Part 2 adds behavior on top of Part 1. One solution must pass all ten tests.
| Command | What it does |
|---|---|
NEW id side qty | The gateway sent an order. side is B (buy) or S (sell) |
ACK id | Acknowledgment |
FILL id qty | Fill of qty more shares |
STATUS id | Print the status line |
OPEN | List the open orders |
POSITION | Print the position |
REJECT id | Part 2: the exchange refused a new order |
CANCEL_REQ id | Part 2: the strategy asks to cancel the unfilled shares |
CANCEL_ACK id, CANCEL_REJECT id | Part 2: the exchange's two answers to a cancel request |
The status line
Most commands print the order's status line:
id STATE filled/qty
STATEis the order's state after the command,filledthe shares filled so far, andqtythe order's quantity.7 PARTIAL 100/300is an order for 300 shares with 100 filled.
Part 1: The happy path
In Part 1 an order passes through four states.
| State | Meaning |
|---|---|
PENDING | Sent to the exchange, with no answer yet. |
ACKED | Acknowledged: the exchange accepted the order, and it is live with nothing filled. |
PARTIAL | Live, with some but not all of its shares filled. |
FILLED | Every share has filled. The order is done. |
The rules
NEWstarts the order inPENDING.ACKmoves aPENDINGorder toACKED.FILLaddsqtyto the filled total. The order becomesFILLEDif no shares are left unfilled andPARTIALotherwise.- A fill moves the position up by
qtyfor a buy and down byqtyfor a sell. NEW,ACK,FILL, andSTATUSprint the order's status line.OPENprintsOPENfollowed by the ids of every order that is not done, in increasing order and separated by spaces, orOPEN NONEwhen no order is open.- In Part 1 the only done state is
FILLED. Every other state,PENDINGincluded, is open. POSITIONprintsPOSITION n, wherenis the current position.
Guarantees in Part 1
- Every command is legal. Each id has been created by a
NEW, and eachACKnames aPENDINGorder. - Each
FILLnames anACKEDorPARTIALorder and never exceeds its unfilled shares.
Tests 1–3 cover this part.
Part 2: Rejects, cancels, and invalid events
Three more states complete the lifecycle:
REJECTED: the exchange refused the order. Done, likeFILLED.CANCEL_PENDING: the strategy asked the exchange to cancel the unfilled shares and is waiting for the answer. Open.CANCELLED: the exchange confirmed the cancel, so the unfilled shares will never trade. Done.
The strategy's CANCEL_REQ goes through the gateway, and this gateway sends a cancel only for an acknowledged order.
| Event | Legal from | New state |
|---|---|---|
ACK id | PENDING | ACKED |
REJECT id | PENDING | REJECTED |
FILL id qty | ACKED, PARTIAL | FILLED if no shares are left unfilled, else PARTIAL |
FILL id qty | CANCEL_PENDING | FILLED if no shares are left unfilled, else CANCEL_PENDING |
CANCEL_REQ id | ACKED, PARTIAL | CANCEL_PENDING |
CANCEL_ACK id | CANCEL_PENDING | CANCELLED |
CANCEL_REJECT id | CANCEL_PENDING | PARTIAL if any shares have filled, else ACKED |
Every legal event prints the order's status line, as in Part 1.
A fill can race a cancel (the second FILL row)
- A cancel request takes time to reach the exchange, and until it arrives the order can keep trading.
- A fill that crosses the request leaves the order in
CANCEL_PENDINGwith a larger filled total. A laterCANCEL_ACKcancels only the shares still unfilled, so the order endsCANCELLEDwith some shares filled. - If the fill completes the order instead, the order is
FILLEDand done. An answer to the cancel that arrives afterwards is an event for a done order.
Invalid events
An event the table does not allow is invalid. It prints one error line and changes nothing: not the order's state, not its filled total, not the position. The checks run in this order:
- A command other than
NEWwhose id noNEWhas created,STATUSincluded, printsERROR id UNKNOWN_ORDER. ANEWwhose id is already taken printsERROR id DUPLICATE_ID, whatever state the existing order is in, because ids are never reused. - An event that is not legal from the order's current state prints
ERROR id EVENT IN STATE. A duplicate acknowledgment printsERROR 7 ACK IN ACKED, and a fill after a confirmed cancel printsERROR 7 FILL IN CANCELLED. - A
FILLin a state that accepts fills, whoseqtyis larger than the order's unfilled shares (its quantity minus its filled total), printsERROR id OVERFILL.
Tests 4–10 cover this part.
The example below also uses Part 2: a cancel request, and a fill that arrives after the cancel.
8 NEW 7 B 300 ACK 7 FILL 7 100 CANCEL_REQ 7 FILL 7 50 CANCEL_ACK 7 FILL 7 20 POSITION
7 PENDING 0/300 7 ACKED 0/300 7 PARTIAL 100/300 7 CANCEL_PENDING 100/300 7 CANCEL_PENDING 150/300 7 CANCELLED 150/300 ERROR 7 FILL IN CANCELLED POSITION 150
The strategy buys 300 AAPL shares. After the acknowledgment and a 100-share fill, it asks to cancel the remaining 200. Before the exchange answers, 50 more shares trade, so the order stays CANCEL_PENDING with 150 filled.
CANCEL_ACK cancels the 150 shares still unfilled, and the order ends CANCELLED 150/300. The fill that arrives after that is invalid: it prints an error and leaves the position at the 150 shares actually bought.