A high-performance, thread-safe limit order book implementation with matching engine.
# Clone the repository
git clone https://github.com/g3dots/Orderbook
cd Orderbook
# Build
cargo build --release
# Run
cargo run --release
# Run the example
cargo run
# Run with debug logging
RUST_LOG=debug cargo run
# Run release build (optimized)
cargo run --release# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific test
cargo test test_matching- Full Limit Order Book: Buy and sell sides with price-time priority
- Order Types:
- Good Till Cancel (GTC): Remains active until filled or cancelled
- Fill and Kill (FAK): Executes immediately for available quantity, cancels remainder
- Fill or Kill (FOK): Executes entirely or cancels completely
- Good For Day (GFD): Auto-cancels at market close (4 PM)
- Market Orders: Converts to limit orders at best opposite price
- Order Operations: Add, cancel, modify orders
- Automatic Matching Engine: Real-time order matching with trade generation
- Level Data Tracking: Maintains aggregate quantity per price level
use orderbook::*;
#[tokio::main]
async fn main() -> Result<()> {
// Create orderbook
let orderbook = Orderbook::new();
// Add buy order
let buy_order = Order::new(
OrderType::GoodTillCancel,
1, // Order ID
Side::Buy,
100, // Price
50, // Quantity
);
orderbook.add_order(buy_order)?;
// Add sell order (will match and generate trades)
let sell_order = Order::new(
OrderType::GoodTillCancel,
2,
Side::Sell,
100,
30,
);
let trades = orderbook.add_order(sell_order)?;
// Check orderbook state
let snapshot = orderbook.get_order_infos();
println!("Bids: {:?}", snapshot.bids);
println!("Asks: {:?}", snapshot.asks);
Ok(())
}orderbook/
├── src/
│ ├── main.rs # Example usage
│ ├── lib.rs # Library root
│ ├── orderbook.rs # Core orderbook logic
│ ├── order.rs # Order types and structures
│ ├── trade.rs # Trade structures
│ ├── types.rs # Common types
│ ├── error.rs # Error handling
│ └── tests.rs # Unit tests
├── Cargo.toml # Dependencies
└── README.md # This file
tokio- Async runtimedashmap- Concurrent hashmapparking_lot- Efficient synchronizationchrono- Time handlingtracing- Logging
# Run with debug logging
RUST_LOG=debug cargo run
# Format code
cargo fmt
# Run linter
cargo clippy
# Generate documentation
cargo doc --openMIT