Class OrderBook

java.lang.Object
com.quantfinlib.orderbook.OrderBook

public final class OrderBook extends Object
Price-time-priority limit order book with continuous matching. Models order placement, cancellation and execution so microstructure effects — spread dynamics, queue priority, book sweeps — can be studied directly.

Which lane this is in: this is the research-grade venue model — used by the microstructure fuzz tests, queue analytics and simulations — NOT part of the measured ultra-low-latency path. The library's sub-microsecond numbers are participant-side (tick → strategy/quoter → risk gate → order ring → venue adapter); in that architecture matching happens at the exchange, not in this process, so no order on the hot path ever touches this class. Internally it deliberately favors clarity over allocation discipline (TreeMap<Double,…> boxing, per-order objects, iterators) — adequate for simulation, and exactly what a venue-grade core must NOT do. The venue-grade sibling is HftOrderBook (dense integer-tick ladder, pooled intrusive nodes, primitive id map, zero allocation) — and its correctness is defined BY this class: a model-based equivalence test drives both books with identical random operation streams and demands identical state.

Keeps message counters (orders, cancels, trades) for order-to-trade ratio and surveillance analytics. Single-threaded by design: drive it from one strategy/simulation thread (or the HFT bus consumer thread).

  • Constructor Details

    • OrderBook

      public OrderBook(String symbol)
  • Method Details

    • symbol

      public String symbol()
    • addTradeListener

      public void addTradeListener(OrderBook.TradeListener listener)
    • submitLimit

      public long submitLimit(Side side, double price, long quantity, long timestampNanos)
      Submits a limit order: matches any crossing liquidity, then rests the remainder. Returns the order id (usable for cancel(long) and qtyAhead(long) while any quantity rests).
    • submitMarket

      public long submitMarket(Side side, long quantity, long timestampNanos)
      Submits a market order; any unfilled remainder is discarded. Returns filled quantity.
    • cancel

      public boolean cancel(long orderId)
      Cancels a resting order. Returns false if unknown or fully filled.
    • bestBid

      public double bestBid()
    • bestAsk

      public double bestAsk()
    • bestBidSize

      public long bestBidSize()
    • bestAskSize

      public long bestAskSize()
    • mid

      public double mid()
    • spread

      public double spread()
    • levels

      public List<double[]> levels(Side side, int maxLevels)
      Price/quantity pairs for the given side's best maxLevels (side = the resting liquidity side: BUY returns bids).
    • depth

      public long depth(Side side, int maxLevels)
      Total resting quantity on a side across its best maxLevels.
    • qtyAhead

      public long qtyAhead(long orderId)
      Quantity queued ahead of the given resting order at its price level.
    • order

      public LimitOrder order(long orderId)
    • orderCount

      public long orderCount()
    • cancelCount

      public long cancelCount()
    • tradeCount

      public long tradeCount()
    • orderToTradeRatio

      public double orderToTradeRatio()
      Order-to-trade ratio: messages (orders + cancels) per trade.