pub struct OrderBook { /* private fields */ }Expand description
A limit order book implementation with support for market orders, limit orders, cancellation, modification and real-time depth.
Use crate::OrderBookBuilder to create an instance with optional features
like journaling or snapshot restoration.
Implementations§
Source§impl OrderBook
impl OrderBook
Sourcepub fn new(symbol: &str, opts: OrderBookOptions) -> Self
pub fn new(symbol: &str, opts: OrderBookOptions) -> Self
Creates a new OrderBook instance with the given symbol and options.
Prefer using crate::OrderBookBuilder for clarity and flexibility.
§Parameters
symbol: Market symbol (e.g.,"BTCUSD")opts: Configuration options (e.g., journaling, snapshot)
§Example
use rust_order_book::{OrderBook, OrderBookOptions};
let ob = OrderBook::new("BTCUSD", OrderBookOptions::default());Sourcepub fn market(
&mut self,
options: MarketOrderOptions,
) -> Result<ExecutionReport, OrderBookError>
pub fn market( &mut self, options: MarketOrderOptions, ) -> Result<ExecutionReport, OrderBookError>
Executes a market order against the order book.
The order will immediately match with the best available opposite orders until the quantity is filled or the book is exhausted.
§Parameters
options: AMarketOrderOptionsstruct specifying the side and size.
§Returns
An ExecutionReport with fill information and remaining quantity, if any.
§Errors
Returns Err if the input is invalid (e.g., size is zero).
Sourcepub fn limit(
&mut self,
options: LimitOrderOptions,
) -> Result<ExecutionReport, OrderBookError>
pub fn limit( &mut self, options: LimitOrderOptions, ) -> Result<ExecutionReport, OrderBookError>
Submits a new limit order to the order book.
The order will be matched partially or fully if opposing liquidity exists, otherwise it will rest in the book until matched or canceled.
§Parameters
options: ALimitOrderOptionswith side, price, size, time-in-force and post_only.
§Returns
An ExecutionReport with match information and resting status.
§Errors
Returns Err if the input is invalid.
Sourcepub fn cancel(&mut self, id: u64) -> Result<ExecutionReport, OrderBookError>
pub fn cancel(&mut self, id: u64) -> Result<ExecutionReport, OrderBookError>
Cancels an existing order by ID.
§Parameters
id: UUID of the order to cancel
§Returns
An ExecutionReport with order info if successfully canceled.
§Errors
Returns Err if the order is not found.
Sourcepub fn modify(
&mut self,
id: u64,
price: Option<u64>,
quantity: Option<u64>,
) -> Result<ExecutionReport, OrderBookError>
pub fn modify( &mut self, id: u64, price: Option<u64>, quantity: Option<u64>, ) -> Result<ExecutionReport, OrderBookError>
Modifies an existing order by cancelling it and submitting a new one.
This function cancels the existing order with the given ID and replaces it with a new one that has the updated price and/or quantity. The new order will receive a new unique ID and will be placed at the end of the queue, losing its original time priority.
§Parameters
id: UUID of the existing order to modifyprice: Optional new pricequantity: Optional new quantity
§Returns
An ExecutionReport describing the new order created.
§Errors
Returns Err if the order is not found or if the modification parameters are invalid.
§Note
This is a full replacement: time-priority is reset and the order ID changes.
Sourcepub fn get_orders_at_price(&self, price: u64, side: Side) -> Vec<LimitOrder>
pub fn get_orders_at_price(&self, price: u64, side: Side) -> Vec<LimitOrder>
Get all orders at a specific price level
pub fn get_order(&self, id: u64) -> Result<LimitOrder, OrderBookError>
Sourcepub fn snapshot(&self) -> Snapshot
pub fn snapshot(&self) -> Snapshot
Creates a complete snapshot of the current order book state.
The snapshot includes all internal data necessary to fully restore the order book:
orders: a mapping ofOrderIdtoLimitOrderbidsandasks: BTreeMaps representing the price levels and associated order IDslast_op: the ID of the last operation performednext_order_id: the next available order IDts: a timestamp representing when the snapshot was taken
This function does not fail and can be called at any time.
It returns a Snapshot struct, which can later be used with OrderBook::restore_snapshot
to recreate the order book state exactly as it was at the moment of the snapshot.
Sourcepub fn restore_snapshot(&mut self, snapshot: Snapshot)
pub fn restore_snapshot(&mut self, snapshot: Snapshot)
Sourcepub fn replay_logs(
&mut self,
logs: Vec<JournalLog>,
) -> Result<(), OrderBookError>
pub fn replay_logs( &mut self, logs: Vec<JournalLog>, ) -> Result<(), OrderBookError>
Replays a sequence of journal logs to reconstruct the order book state.
Each log entry represents a previously executed operation, such as a market order, limit order, cancel, or modify. This function applies each operation in order.
§Parameters
logs: A vector ofJournalLogentries to be applied. Logs must be in chronological order to correctly reconstruct the state.
§Returns
Returns Ok(()) if all operations are successfully applied.
Returns Err(OrderBookError) if any operation fails; the replay stops at the first error.