Engineering · TRUEFIX LABS

Order State Machines in Real Trading Systems

Order lifecycle design across internal, broker, and exchange states, including partial fills, cancellation races, reconnects, and unknown outcomes.

Short answer. An order state machine must distinguish internal intent from broker and exchange observations. The useful states include Created, Submitted, Accepted, PartiallyFilled, Filled, Cancelled, Rejected, Expired, PendingCancel, PendingReplace, and Unknown. Unknown is a durable recovery state, not an error message to discard.

State categories

Internal states describe what the system requested; provider states describe what a venue reports; derived states describe what can be safely shown to a user. Store the raw observation alongside the normalized state.

Race conditions

A fill can arrive while cancellation is pending. A replace can be accepted after a stale cancel response. Duplicate events can be replayed after reconnect. Transition validation and event IDs prevent a late message from moving an order backwards.

Unknown recovery

When the result of submission is uncertain, keep the order in Unknown and reconcile it using the original client order ID, provider order ID, account, and time window. Do not create a second intent until the original is resolved or explicitly abandoned.

Testing

Replay event sequences with duplicate messages, missing sequence numbers, out-of-order fills, disconnects, and provider snapshots. State machine tests should assert both final state and the audit trail.

Sources

Last verified: 2026-09-28. Provider capabilities can change by region, account, product, environment, and entitlement; verify the linked official documentation before production use.

Last updated: 2026-09-28