Skip to main content

Module orderbook

Module orderbook 

Source
Expand description

Pure orderbook data structure and matching engine.

This module provides OrderBook, a price-time priority limit order book for a single instrument. It supports:

  • Adding and cancelling orders with full metadata
  • Incremental matching (one maker at a time) with self-trade prevention
  • L2 (price-level) snapshot and incremental diff computation
  • Snapshot/restore for WAL-based persistence

§Design

The orderbook is a pure data structure with no async runtime, no channels, and no side effects. All operations are synchronous and deterministic.

Event emission is handled by the caller. The orderbook computes L2 diffs and returns fill results, but never sends events itself. The runtime layer in the hypercall crate is responsible for routing these to WebSocket clients, persistence, and downstream caches.

§Price-level storage

Bids are stored in a BTreeMap keyed by negated price for descending order. Asks are stored with natural (ascending) price keys. Within each price level, orders are stored in a VecDeque for FIFO (time priority) matching.

Structs§

L2UpdateSet
Set of L2 incremental updates computed by OrderBook::compute_l2_updates.
Order
An order resting on a price level.
OrderBook
A pure, synchronous limit order book for a single instrument.
OrderMeta 🔒
Metadata stored alongside the price-level Order.
OrderRecord
Full order record returned by OrderBook::get_all_orders and accepted by OrderBook::restore_from_orders.
PriceLevel 🔒

Enums§

MatchResult
Result of attempting to match a single maker order.
OrderBookEvent
Events produced by orderbook operations.