Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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.
CommandWhat it does
NEW id side qtyThe gateway sent an order. side is B (buy) or S (sell)
ACK idAcknowledgment
FILL id qtyFill of qty more shares
STATUS idPrint the status line
OPENList the open orders
POSITIONPrint the position
REJECT idPart 2: the exchange refused a new order
CANCEL_REQ idPart 2: the strategy asks to cancel the unfilled shares
CANCEL_ACK id, CANCEL_REJECT idPart 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
  • STATE is the order's state after the command, filled the shares filled so far, and qty the order's quantity.
  • 7 PARTIAL 100/300 is an order for 300 shares with 100 filled.

Part 1: The happy path

In Part 1 an order passes through four states.

StateMeaning
PENDINGSent to the exchange, with no answer yet.
ACKEDAcknowledged: the exchange accepted the order, and it is live with nothing filled.
PARTIALLive, with some but not all of its shares filled.
FILLEDEvery share has filled. The order is done.

The rules

  1. NEW starts the order in PENDING.
  2. ACK moves a PENDING order to ACKED.
  3. FILL adds qty to the filled total. The order becomes FILLED if no shares are left unfilled and PARTIAL otherwise.
  4. A fill moves the position up by qty for a buy and down by qty for a sell.
  5. NEW, ACK, FILL, and STATUS print the order's status line.
  6. OPEN prints OPEN followed by the ids of every order that is not done, in increasing order and separated by spaces, or OPEN NONE when no order is open.
  7. In Part 1 the only done state is FILLED. Every other state, PENDING included, is open.
  8. POSITION prints POSITION n, where n is the current position.

Guarantees in Part 1

  • Every command is legal. Each id has been created by a NEW, and each ACK names a PENDING order.
  • Each FILL names an ACKED or PARTIAL order 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, like FILLED.
  • 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.

EventLegal fromNew state
ACK idPENDINGACKED
REJECT idPENDINGREJECTED
FILL id qtyACKED, PARTIALFILLED if no shares are left unfilled, else PARTIAL
FILL id qtyCANCEL_PENDINGFILLED if no shares are left unfilled, else CANCEL_PENDING
CANCEL_REQ idACKED, PARTIALCANCEL_PENDING
CANCEL_ACK idCANCEL_PENDINGCANCELLED
CANCEL_REJECT idCANCEL_PENDINGPARTIAL 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_PENDING with a larger filled total. A later CANCEL_ACK cancels only the shares still unfilled, so the order ends CANCELLED with some shares filled.
  • If the fill completes the order instead, the order is FILLED and 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:

  1. A command other than NEW whose id no NEW has created, STATUS included, prints ERROR id UNKNOWN_ORDER. A NEW whose id is already taken prints ERROR id DUPLICATE_ID, whatever state the existing order is in, because ids are never reused.
  2. An event that is not legal from the order's current state prints ERROR id EVENT IN STATE. A duplicate acknowledgment prints ERROR 7 ACK IN ACKED, and a fill after a confirmed cancel prints ERROR 7 FILL IN CANCELLED.
  3. A FILL in a state that accepts fills, whose qty is larger than the order's unfilled shares (its quantity minus its filled total), prints ERROR 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.

Example
Input
8
NEW 7 B 300
ACK 7
FILL 7 100
CANCEL_REQ 7
FILL 7 50
CANCEL_ACK 7
FILL 7 20
POSITION
Output
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
Explanation

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.