Skip to content

A high-performance limit order book with matching engine written in Rust.

Notifications You must be signed in to change notification settings

g3dots/Orderbook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Orderbook

A high-performance, thread-safe limit order book implementation with matching engine.

Quick Start

# Clone the repository
git clone https://github.com/g3dots/Orderbook
cd Orderbook

# Build
cargo build --release

# Run
cargo run --release

Running

Basic Usage

# Run the example
cargo run

# Run with debug logging
RUST_LOG=debug cargo run

# Run release build (optimized)
cargo run --release

Testing

# Run all tests
cargo test

# Run tests with output
cargo test -- --nocapture

# Run specific test
cargo test test_matching

Features

Core Functionality

  • 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

Usage Example

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(())
}

Project Structure

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

Dependencies

  • tokio - Async runtime
  • dashmap - Concurrent hashmap
  • parking_lot - Efficient synchronization
  • chrono - Time handling
  • tracing - Logging

Development

# Run with debug logging
RUST_LOG=debug cargo run

# Format code
cargo fmt

# Run linter
cargo clippy

# Generate documentation
cargo doc --open

License

MIT

About

A high-performance limit order book with matching engine written in Rust.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages