Implementation:Avhz RustQuant LimitOrderBook
| Knowledge Sources | |
|---|---|
| Domains | Order_Management, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for a high-performance limit order book implementation provided by the RustQuant library.
Description
The Book struct implements a limit order book using BTreeMap-based price levels and a HashMap for O(1) order lookups. The buy and sell sides are maintained as separate BTreeMap<u64, Limit> trees, allowing efficient price-level traversal in sorted order. An order_map (HashMap<u64, Order>) provides direct access to any order by its unique ID. The struct supports three core operations: add_order() inserts a new order at a specified limit price, returning an ExistingIdError if the order ID already exists; cancel_order() removes an order by ID, returning a NonExistingIdError if not found, and cleans up empty price levels; execute_market_order() fills a market order against the best available prices, iterating through price levels from the best bid or ask, returning a tuple of (success_flag, Vec<(price, shares_executed)>) indicating whether the full order was filled and the execution details at each price level. Two custom error types (ExistingIdError and NonExistingIdError) provide clear error reporting. The struct implements Default via new().
Usage
Use this when you need a self-contained limit order book for backtesting trading strategies, simulating market microstructure, or building trading system prototypes that require order matching at discrete price levels.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_trading/src/limit_order_book.rs
- Lines: 1-214
Signature
pub struct Book {
buy_limits: BTreeMap<u64, Limit>,
sell_limits: BTreeMap<u64, Limit>,
order_map: HashMap<u64, Order>,
}
impl Book {
pub fn new() -> Self;
pub fn add_order(
&mut self,
order_id: u64,
is_buy: bool,
shares: u64,
limit_value: u64,
timestamp: u64,
) -> Result<(), ExistingIdError>;
pub fn cancel_order(&mut self, order_id: u64) -> Result<(), NonExistingIdError>;
pub fn execute_market_order(&mut self, shares: u64, is_buy: bool) -> (bool, Vec<(u64, u64)>);
}
Import
use RustQuant::trading::limit_order_book::Book;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| order_id | u64 | Yes | Unique identifier for the order |
| is_buy | bool | Yes | True for buy orders, false for sell orders |
| shares | u64 | Yes | Number of shares in the order |
| limit_value | u64 | Yes | Price of shares (typically $ x 1000, e.g., $4.50 -> 4500) |
| timestamp | u64 | Yes | Timestamp of the order |
Outputs
| Name | Type | Description |
|---|---|---|
| Result<(), ExistingIdError> | Result | add_order returns Ok(()) or error if ID exists |
| Result<(), NonExistingIdError> | Result | cancel_order returns Ok(()) or error if ID not found |
| (bool, Vec<(u64, u64)>) | Tuple | execute_market_order returns (fully_filled, [(price, shares_executed)]) |
Usage Examples
use RustQuant::trading::limit_order_book::Book;
let mut book = Book::new();
// Add buy and sell orders
book.add_order(1, true, 100, 45000, 1000).unwrap(); // Buy 100 @ $45.00
book.add_order(2, false, 50, 46000, 1001).unwrap(); // Sell 50 @ $46.00
book.add_order(3, false, 75, 47000, 1002).unwrap(); // Sell 75 @ $47.00
// Execute a market buy order for 80 shares
let (filled, executions) = book.execute_market_order(80, true);
// executions contains [(46000, 50), (47000, 30)] - filled across two price levels
// Cancel an order
book.cancel_order(1).unwrap();