Matching Engine

StockMarketABM.MatchingEngine consumes incoming OrderSpecs, matches them against existing book liquidity, emits market events, and enforces time-in-force policies. The matching flow proceeds as follows:

  1. match_order! validates the request (e.g. FOK pre-check via available_qty), emits an initial OrderAccepted event, and consumes liquidity by repeatedly calling consume! from the order book. Each fill produces a TradeExecuted event containing maker/taker metadata and the residual quantities.
  2. Remaining quantity is handed off to apply_tif!, which either posts the residual (OrderAccepted with resting=true for GTC/GTT) or cancels it with the appropriate reason (:unfilled_market, :ioc_lapse, :fok_not_filled). Market orders never rest, regardless of TIF.
  3. cancel_order! and clear_book! wrap the corresponding order-book operations, emitting OrderCanceled events so downstream components can update state consistently.

Together these functions provide a deterministic event log and keep the order book as the source of truth for resting liquidity. See docs/src/lob/orderbook.md for details on price-level management.

StockMarketABM.MatchingEngine.apply_tif!Method
apply_tif!(book, spec, residual_qty; now)

Handle residual quantity after matching according to the order's TIF policy. For example IOC orders cancel leftovers immediately, whereas GTC keeps them resting in the book. Returns the generated MarketEvents.

source
StockMarketABM.MatchingEngine.match_order!Method
match_order!(book, spec; now)

Attempt to execute OrderSpec against the provided LimitOrderBook. Returns a vector of MarketEvents describing acceptances, trades and any residual cancels. Time-in-force behaviour is delegated to apply_tif!.

source