All skills
joellewis avatar

/order-lifecycle

@cf3e6ba
by joellewisjoellewis/finance_skills200 stars
37

Guide the design and implementation of order lifecycle management in trading systems. Owns FIX application-layer message flows (NewOrderSingle, ExecutionReport, cancel/replace) and order state. Use when building an order state machine for an OMS or EMS, handling cancel/replace race conditions, defining pre-submission validation rules (buying power, position limits, restricted lists), selecting order types and time-in-force instructions, designing multi-leg or OCO or bracket orders, building CAT-compliant audit trails, troubleshooting order rejections or unexpected state transitions, hardening an OMS against edge cases, or implementing order persistence and recovery for failover. Also covers execution-report handling, ClOrdID chaining, and partial fill aggregation. For FIX session management (logon, sequence gaps, disconnects) see exchange-connectivity.

Use this Skill: https://skilld.dev/gh/joellewis/finance_skills/order-lifecycle

This session only. Nothing lands on disk.

referencesexamples.md

≈3.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Order Lifecycle — Worked Examples

Example 1: Designing an Order State Machine for a Broker-Dealer's OMS

Scenario: A mid-size broker-dealer is building a new order management system to replace a legacy platform. The legacy system had a flat order status field with values like "OPEN," "DONE," and "ERROR" — insufficient for proper lifecycle tracking. The new OMS must implement a rigorous state machine that handles all order types, supports FIX connectivity to multiple execution venues, and satisfies CAT reporting requirements.

Design approach:

The engineering team starts by defining the state enumeration. Drawing from FIX OrdStatus values and operational requirements, they establish 13 states: New, PendingNew, Accepted, PartiallyFilled, Filled, PendingCancel, Canceled, PendingReplace, Replaced, Rejected, Expired, Suspended, and DoneForDay. Each state is categorized as terminal (Filled, Canceled, Replaced, Rejected, Expired) or non-terminal (all others).

The transition table is implemented as an explicit allowlist. Rather than permitting any transition not explicitly forbidden (a dangerous pattern that allows invalid states through omissions), the system defines every permitted transition as a pair (from_state, to_state) with an associated trigger event (typically a FIX ExecType or an internal event). Any transition not in the allowlist is rejected and logged as an error. The transition table contains approximately 25 to 30 valid transitions.

For state persistence, the team selects a write-ahead log (WAL) pattern. Before processing any inbound message (FIX ExecutionReport, cancel acknowledgment, etc.), the system writes the pending state transition to a durable log. If the system crashes mid-transition, the recovery process replays the WAL from the last checkpoint, applying each transition idempotently. Idempotency is achieved by assigning a unique event identifier (based on the FIX message sequence number and session identifier) to each transition and checking for duplicates during replay.

The state machine handles the cancel-vs-fill race condition explicitly. When an order is in PendingCancel and a fill ExecutionReport arrives, the system processes the fill first (transitioning to PartiallyFilled or Filled), then evaluates whether the cancel request is still relevant. If the order is now Filled, the cancel is abandoned and the CancelReject is expected. If the order is PartiallyFilled, the cancel may still succeed for the remaining quantity. The system never drops a fill message — fills are processed with highest priority regardless of pending cancel/replace state.

For CAT compliance, every state transition generates an audit event record containing: the order identifier (ClOrdID and OrderID), the previous state, the new state, the trigger event (FIX message type and key fields), the timestamp (microsecond precision, synchronized per FINRA Rule 4590), and the system component that processed the transition. These events are written to an append-only audit log and are the source data for CAT reporting.

Analysis:

The explicit-allowlist approach for state transitions is preferred over a denylist because it fails safely — a missing transition results in a rejected event (which is logged and investigated) rather than a silently accepted invalid transition. The WAL pattern ensures no state changes are lost during crashes, and idempotent replay handles the case where a message was partially processed before the crash. The cancel-vs-fill race handling prioritizes fill processing because fills represent irrevocable financial events — a fill that is dropped or delayed can cause position discrepancies, P&L errors, and regulatory issues.

Example 2: Implementing Cancel/Replace Workflows with Race Condition Handling

Scenario: A proprietary trading desk is experiencing issues with its cancel/replace workflow. Traders frequently amend limit order prices as the market moves, but the current system occasionally produces inconsistent states: orders that appear canceled but have unrecognized fills, or replace requests that reference stale ClOrdIDs and are rejected by the venue. The desk needs a redesigned cancel/replace implementation that correctly handles all race conditions.

Design approach:

The root cause analysis reveals three problems. First, the system is not maintaining the ClOrdID chain correctly — when a replace request is submitted, the system updates the order's ClOrdID immediately rather than waiting for the venue's confirmation. If the replace is rejected, the order's ClOrdID no longer matches what the venue has on record, and subsequent requests fail. Second, the system permits concurrent cancel/replace requests — a trader can submit a price amendment while a previous amendment is still pending, creating ambiguity at the venue. Third, fill messages arriving during a Pending Replace state are being deferred rather than processed immediately, causing position tracking to lag.

The redesign addresses each problem:

For ClOrdID management, the system maintains two identifiers per order: the "active ClOrdID" (the ClOrdID currently acknowledged by the venue) and the "pending ClOrdID" (the ClOrdID of an outstanding cancel/replace request, if any). The active ClOrdID is updated only when the venue confirms the replace (ExecutionReport with ExecType=Replaced). If the replace is rejected (OrderCancelReject), the pending ClOrdID is discarded and the active ClOrdID remains unchanged. All new requests to the venue reference the active ClOrdID as the OrigClOrdID.

For concurrency control, the system enforces a strict one-pending-request rule. While a cancel or replace request is outstanding (order is in PendingCancel or PendingReplace), new cancel/replace requests from the trader are queued internally. When the pending request is resolved (confirmed or rejected), the system dequeues the next request (if any) and submits it. If the queued request conflicts with the resolution (e.g., the trader queued a price change to $50.10 but the order filled while the previous request was pending), the queued request is discarded and the trader is notified.

For fill processing during pending states, the system processes fill ExecutionReports immediately regardless of pending cancel/replace status. If the order is in PendingReplace and a fill arrives, the fill is applied (cumulative quantity and average price are updated, the order may transition to PartiallyFilled or Filled). If the fill completes the order (Filled), the pending replace is moot. If the fill is partial, the pending replace may still succeed, but the OMS recalculates whether the replace request's quantity is still valid (the new quantity must be greater than or equal to the cumulative filled quantity; otherwise the venue will reject the replace).

Analysis:

The two-identifier pattern (active ClOrdID and pending ClOrdID) eliminates the stale-reference problem because the system always knows which ClOrdID the venue considers current. The one-pending-request rule eliminates venue-side ambiguity and simplifies the OMS state machine. The immediate fill processing during pending states ensures that position tracking is always current, even when cancel/replace messages are in flight. Together, these patterns handle the fundamental race condition of cancel/replace workflows: the unavoidable latency window between sending a request and receiving the venue's response, during which fills and other events may occur.

Example 3: Building FIX Connectivity to an Execution Venue

Scenario: A buy-side firm is establishing FIX connectivity to a new electronic communication network (ECN) to access additional liquidity for its equity trading strategies. The firm's existing OMS supports FIX 4.2 connections to two other venues. The new ECN supports FIX 4.4 and has specific requirements for message formatting, session management, and order handling.

Design approach:

The implementation proceeds through four phases: session certification, application message mapping, exception handling, and production cutover.

Session certification: The ECN provides a certification (test) environment with a FIX acceptor endpoint. The firm's FIX engine (the initiator) must establish a session by negotiating protocol version, sender and target CompIDs, heartbeat interval, and sequence number handling. The certification process validates that the FIX engine correctly handles: logon and logout sequences, heartbeat exchange (including detection of missed heartbeats and test request/heartbeat recovery), sequence number synchronization (including gap detection and resend requests), and message-level rejection (MsgType=3, Reject) for malformed messages. Session certification typically takes one to two weeks and requires multiple rounds of testing. Common session-level issues include: incorrect CompID configuration, heartbeat interval mismatch (the ECN expects 30 seconds; the firm's engine is configured for 60), and sequence number reset policy disagreements (some venues require a daily sequence number reset at a specific time; others maintain continuous sequence numbers).

Application message mapping: FIX 4.4 introduces fields and message structures not present in FIX 4.2. The firm must map its internal order representation to the ECN's specific FIX 4.4 requirements. Key mapping considerations include: the ECN may require specific values in Tag 1 (Account) that differ from the firm's internal account identifiers; the ECN may support order types or time-in-force values that the firm's other venues do not (or may not support order types that the firm uses elsewhere); the ECN may use custom tags (user-defined tags in the 5000+ range) for venue-specific features such as order routing preferences or self-trade prevention instructions; execution report processing must handle the ECN's specific usage of ExecType and OrdStatus, which may differ subtly from other venues (for example, some venues use ExecType=Trade for fills while others use ExecType=Fill, which was introduced in FIX 4.4 as a clearer alternative to the overloaded ExecType=Trade value from FIX 4.2).

Exception handling: The connection must handle operational exceptions gracefully. Network disconnections require automatic reconnection with exponential backoff. During disconnection, the OMS must track which orders are "in flight" at the ECN — orders that were submitted but whose status is unknown due to the disconnection. Upon reconnection, after sequence number synchronization and gap fill processing, the OMS sends OrderStatusRequest messages for all in-flight orders to reconcile OMS state with venue state. Venue-side order cancellation (the ECN cancels orders unilaterally during a system event or end-of-day) must be detected and processed — the OMS cannot assume that an order remains active at the venue just because no cancel confirmation was received. Drop copy connections (a secondary FIX session that receives copies of all ExecutionReports) provide redundancy: if the primary session drops a message, the drop copy catches it.

Production cutover: Before going live, the firm conducts a parallel run: orders are submitted to the new ECN while the same orders are priced (but not executed) against the existing venues to compare execution quality. The cutover plan includes: a rollback procedure (ability to stop routing to the new ECN and revert to existing venues within minutes), monitoring dashboards that track rejection rates, fill rates, and latency in real time during the first days of live trading, and an escalation path to the ECN's market operations desk for production support.

Analysis:

FIX connectivity projects are deceptively complex. The protocol standard is well-defined, but each venue interprets and extends it differently. The certification process is essential for discovering venue-specific behaviors before they cause production incidents. The most common production issues with new FIX connections are: sequence number desynchronization after an unclean disconnect (requiring manual intervention to agree on a reset point), message format differences that pass certification but cause sporadic rejections under production load (e.g., a field that the ECN expects only for certain order types), and latency spikes during high-volume periods that trigger heartbeat timeouts and session disconnects. Robust monitoring and automated reconnection logic are as important as correct message formatting.

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    No security issues were detected. The skill consists entirely of documentation and architectural guidance for trading systems, with no executable code or risky operations.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    1/1 file flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at cf3e6ba. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 3 months ago

README badge

README badge for joellewis/finance_skills/order-lifecycle