Lux Docs
Cex

Matching Engine

Central Limit Order Book with price-time priority

The Lux CEX matching engine is a Central Limit Order Book (CLOB) with price-time priority. Internally it uses int64 price and quantity representations for cache-line-aligned, allocation-free matching.

Price-Time Priority

Orders are matched by:

  1. Best price -- highest bid / lowest ask
  2. Time priority -- FIFO at each price level (earliest order fills first)

Order Types

TypeDescriptionSupported TIF
limitExecute at specified price or betterGTC, IOC, FOK, DAY
marketImmediate execution at best available priceIOC, FOK
stopTriggered market order when stop price is reachedGTC, IOC, FOK, DAY
stop_limitTriggered limit order when stop price is reachedGTC, IOC, FOK, DAY

Time-in-Force

TIFNameBehavior
gtcGood-Til-CancelledRemains on book until filled or cancelled
iocImmediate-Or-CancelFill what is available immediately, cancel remainder
fokFill-Or-KillFill entire quantity immediately or reject entirely
dayDayExpires at end of trading session

Order Lifecycle

StatusDescription
newOrder created, not yet validated
pending_approvalAwaiting compliance gate
openOn the order book, available for matching
partial_fillPartially executed
filledFully executed
cancelledCancelled by user or system
rejectedFailed compliance check or matching
expiredTIF expired (DAY orders)
suspendedHalted by surveillance

Execution Path

Order submitted
     |
     v
Market validation (exists, tradable, not halted)
     |
     v
Pre-trade compliance (8 sequential checks)
     |
     v
Internal CLOB match (price-time priority)
     |
     v  (unfilled remainder)
Broker SOR fallback (luxfi/broker)
     |
     v
Execution reports generated
     |
     v
Post-trade hooks (reporting + surveillance)

The MatchEngine attempts to fill against the internal order book first. Any unfilled quantity is routed to the luxfi/broker Smart Order Router, which finds the best external venue (Alpaca, IBKR, Coinbase, etc.) based on price and availability.

Circuit Breakers

The engine supports market halts triggered by:

  • Admin action -- POST /v1/admin/markets/{symbol}/halt with a reason
  • Surveillance alerts -- price spike or manipulation detection
  • System errors -- automated halt on anomalous conditions

When a market is halted:

  1. Market.Status is set to "halted" and Market.Tradable to false
  2. All new orders for that symbol are rejected with "market {symbol} is halted"
  3. Existing open orders remain on the book (not cancelled)
  4. Admin resumes with POST /v1/admin/markets/{symbol}/resume

Orderbook Queries

All protocols can query the current orderbook state:

ProtocolMethod
RESTGET /v1/markets/{symbol}/book?depth=20
WebSocketget_orderbook message or orderbook subscription
JSON-RPCcex.getOrderBook
FIXMarketDataRequest (V) / MarketDataSnapshot (W)
ZAPGetBestBid (0x04), GetBestAsk (0x05), GetBook (0x06)

The MatchEngine.GetSnapshot() returns an OrderBookSnapshot with bid and ask levels, each containing price, quantity, and order count.

On this page