A FIX 4.4 Aligned Order Book Implementation
This document serves as the technical summary and presentation guide for the Matching Engine. It describes the architecture, business logic, and design decisions used to build a low-latency, deterministic trading system.
The goal of this system is to provide a robust, in-memory matching engine that handles the lifecycle of tradeable instruments and orders with high precision.
- Standardization: Fully aligned with FIX 4.4 Protocol specifications for
NewOrderSingleandExecutionReport. - Multi-Symbol Support: Independent order books managed via a centralized
OrderBookRegistry. - Order Capabilities:
- Order Types: Market and Limit orders.
- Time-In-Force (TIF): GTC (Good 'Til Canceled), IOC (Immediate or Cancel), and FOK (Fill or Kill).
- Precision: Eliminates floating-point rounding errors by utilizing scaled-integer pricing (converting doubles to longs).
The engine implements a strict Price-Time Priority (FIFO) matching algorithm.
- Bids (Buy Side): Highest price takes priority. For identical prices, the order that arrived first is executed first.
- Asks (Sell Side): Lowest price takes priority. For identical prices, the order that arrived first is executed first.
-
Execution: When an incoming order's price overlaps with the opposite side (Buy
$\ge$ Ask or Sell$\le$ Bid), aTradeis generated and the quantity is deducted from both parties.
| TIF | Behavior |
|---|---|
| GTC | Rests in the book until fully filled or manually canceled. |
| IOC | Matches immediately against available liquidity; any remaining quantity is canceled. |
| FOK | "All or nothing." If the full quantity cannot be filled immediately, the order is rejected. |
To ensure maintainability and scalability, several industry-standard design patterns were employed:
| Pattern | Component | Justification |
|---|---|---|
| Adapter | FixOrderAdapter |
Decouples raw FIX wire formats from the Domain Model, allowing the engine to remain transport-agnostic. |
| Observer | ExecutionReportPublisher |
Decouples the engine from downstream systems (Risk, OMS, UI). The engine emits events without knowing the consumers. |
| Builder | Order.Builder |
Ensures complex order objects are constructed in a valid state and supports immutable snapshotting for reports. |
| Registry | OrderBookRegistry |
Routes orders to the correct symbol-specific book and manages the lifecycle of multiple instruments. |
| Value Object | Trade, Instrument |
Uses immutability to ensure thread-safety and data integrity across the system. |
Below is the logical flow from the moment a client submits a request to the final execution report.
The system is structured to separate concerns between the "Wire" (FIX), the "Routing" (Registry), and the "Execution" (Order Book).
-
OrderBookRegistry$\rightarrow$ Manages a Map ofBaseOrderBookimplementations. -
BaseOrderBook$\rightarrow$ Interface defining the matching API. -
PriceTimePriorityOrderBook$\rightarrow$ Concrete implementation usingTreeMap(for price levels) andArrayDeque(for time priority). -
Order$\rightarrow$ The central domain entity trackingleavesQty,cumQty, andavgPx. -
PriceUtility$\rightarrow$ Handles the conversion betweendouble(UI) andlong(Engine).
-
Deterministic Execution: The combination of
TreeMapandArrayDequeensures$O(\log N)$ price discovery and$O(1)$ time-priority access. -
Zero Precision Loss: By scaling prices to integers, the system avoids the common
0.1 + 0.2 = 0.30000000000000004error found in financial software. -
Extensibility: Because the engine depends on the
BaseOrderBookinterface, the matching algorithm (e.g., moving from FIFO to Pro-Rata) can be swapped without modifying the rest of the system.
