Class OrderBook
- All Implemented Interfaces:
IOrderBook
The order book stores orders separately for bids and asks using price-sorted maps.
- Bids are sorted in descending price order (best bid = highest).
- Asks are sorted in ascending price order (best ask = lowest).
- Orders at the same price level follow FIFO ordering.
The internal data structures are never exposed directly. Consumers interact
with the order book through an immutable OrderBook.OrderBookSnapshot.
Mutation operations are intended to be used only by the matching engine, while read-only data is provided for the UI and statistics layer.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordImmutable snapshot of the entire order book state.static final recordAggregated representation of a single price level. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a LIMIT order to the appropriate side of the order book.Returns the best ask order currently resting in the book.Returns the current best ask price.Returns the best bid order currently resting in the book.Returns the current best bid price.Computes the mid price of the order book.Builds an immutable snapshot of the current order book state.Computes the bid-ask spread.booleanhasAsks()Checks whether the order book contains any ask orders.booleanhasBids()Checks whether the order book contains any bid orders.intReturns the total number of resting orders in the book.voidRemoves the best ask order from the order book.voidRemoves the best bid order from the order book.voidremoveOrder(Order order) Removes a specific order from the book.
-
Constructor Details
-
OrderBook
public OrderBook()
-
-
Method Details
-
hasBids
public boolean hasBids()Checks whether the order book contains any bid orders.- Specified by:
hasBidsin interfaceIOrderBook- Returns:
trueif at least one bid order exists, otherwisefalse
-
hasAsks
public boolean hasAsks()Checks whether the order book contains any ask orders.- Specified by:
hasAsksin interfaceIOrderBook- Returns:
trueif at least one ask order exists, otherwisefalse
-
getBestBidPrice
Returns the current best bid price.- Returns:
- an
OptionalDoublecontaining the highest bid price, or an empty optional if no bids exist
-
getBestAskPrice
Returns the current best ask price.- Returns:
- an
OptionalDoublecontaining the lowest ask price, or an empty optional if no asks exist
-
getBestBidOrder
-
getBestAskOrder
-
getMidPrice
Computes the mid price of the order book.The mid price is defined as:
(bestBid + bestAsk) / 2
- Returns:
- an
OptionalDoublecontaining the mid price, or empty if either side of the book is empty
-
getSpread
Computes the bid-ask spread.The spread is defined as:
bestAsk − bestBid
- Returns:
- an
OptionalDoublecontaining the spread, or empty if either side of the book is empty
-
addOrder
Adds a LIMIT order to the appropriate side of the order book.If the price level does not yet exist, a new FIFO queue is created automatically.
- Parameters:
order- the limit order to add- Throws:
IllegalArgumentException- if the order type is not LIMIT
-
removeOrder
Removes a specific order from the book.If the removal leaves a price level empty, that level is removed from the map.
- Parameters:
order- the order to remove
-
removeBestBidOrder
public void removeBestBidOrder()Removes the best bid order from the order book.If the price level becomes empty after removal, the entire level is deleted.
-
removeBestAskOrder
public void removeBestAskOrder()Removes the best ask order from the order book.If the price level becomes empty after removal, the entire level is deleted.
-
getSnapshot
Builds an immutable snapshot of the current order book state.The returned lists are unmodifiable using
List.copyOf(Collection).- Specified by:
getSnapshotin interfaceIOrderBook- Returns:
- a snapshot containing aggregated bid/ask levels and key metrics
-
remainingOrderCount
public int remainingOrderCount()Returns the total number of resting orders in the book.This counts orders across both bid and ask sides.
- Specified by:
remainingOrderCountin interfaceIOrderBook- Returns:
- total number of remaining orders
-