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:
match_order!validates the request (e.g. FOK pre-check viaavailable_qty), emits an initialOrderAcceptedevent, and consumes liquidity by repeatedly callingconsume!from the order book. Each fill produces aTradeExecutedevent containing maker/taker metadata and the residual quantities.- Remaining quantity is handed off to
apply_tif!, which either posts the residual (OrderAcceptedwithresting=truefor GTC/GTT) or cancels it with the appropriate reason (:unfilled_market,:ioc_lapse,:fok_not_filled). Market orders never rest, regardless of TIF. cancel_order!andclear_book!wrap the corresponding order-book operations, emittingOrderCanceledevents 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._price_limit — Method_price_limit(side, price)Convert an optional limit price into an executable bound. Market orders map to typemax/typemin depending on side so comparisons work uniformly.
StockMarketABM.MatchingEngine._resolve_instrument — Method_resolve_instrument(spec, default) -> InstrumentReturn the instrument embedded in spec. The default argument is unused but retained for API compatibility with callers that still provide it.
StockMarketABM.MatchingEngine.apply_tif! — Methodapply_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.
StockMarketABM.MatchingEngine.build_accept_event! — Methodbuild_accept_event!(events, spec; resting)Helper constructing an OrderAccepted event and pushing it to events.
StockMarketABM.MatchingEngine.build_cancel_event! — Methodbuild_cancel_event!(events, spec, reason, ts)Helper to emit OrderCanceled events.
StockMarketABM.MatchingEngine.build_trade_event! — Methodbuild_trade_event!(events, price, qty, maker, taker, ts)Construct a TradeExecuted event and append it to events.
StockMarketABM.MatchingEngine.cancel_order! — Methodcancel_order!(book, spec) -> Vector{MarketEvent}Cancel a resting order described by CancelSpec. Returns the generated events (e.g. OrderCanceled).
StockMarketABM.MatchingEngine.clear_book! — Methodclear_book!(book; now) -> Vector{MarketEvent}Cancel all resting orders in the book and return the resulting events. Useful for session resets or halts.
StockMarketABM.MatchingEngine.match_order! — Methodmatch_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!.