A high-performance order matching engine built in Rust that processes buy and sell orders with real-time OHLC (Open, High, Low, Close) tracking.
- Real-time Order Matching: Processes buy and sell orders with price-time priority
- OHLC Tracking: Maintains Open, High, Low, Close price data with timestamps
- Interactive CLI: Command-line interface for entering orders
- Efficient Data Structures: Uses binary heaps and indexed maps for optimal performance
- UUID Order Tracking: Each order is uniquely identified with UUIDs
- Comprehensive Order Book Display: Shows current state of buy and sell orders
The project is structured into several key modules:
engine/order_book.rs: Core order matching logic withOrderBookstructengine/types.rs: Data structures includingOrderandSideenumsengine/ohlc.rs: OHLC price tracking withOhlcTrackerutils/input.rs: CLI input handling for order entry
The OrderBook uses:
- IndexMap: For price-level order queues with insertion order preservation
- BinaryHeap: For efficient price priority management
- VecDeque: For FIFO order processing within price levels
The OrderedF64 wrapper enables floating-point prices to be used as hash keys and in binary heaps by implementing Ord, Hash, and Eq traits.
- Price-Time Priority: Orders are matched based on best price first, then by arrival time
- Partial Fills: Orders can be partially filled across multiple matches
- Quantity Management: Automatic quantity updates and order removal when fully filled
- Rust 1.86.0 or later
- Cargo package manager
[dependencies]
chrono = "0.4.41" # Date/time handling
indexmap = "2.9.0" # Ordered hash maps
uuid = "1.16.0" # UUID generation- Clone the repository:
git clone <repository-url>
cd order-matching-engine- Build the project:
cargo build- Run the application:
cargo runThe engine accepts orders through a simple CLI format:
>> buy 100.50 10
>> sell 101.00 5
>> buy 100.75 15
Format: <side> <price> <quantity>
- Side:
buyorsell - Price: Decimal number (e.g.,
100.50) - Quantity: Integer number of shares/units
Enter orders (buy/sell price qty):
>> buy 100.00 10
Order Book:
SELL:
BUY:
100.00 qty:10 id:a1b2c3d4-e5f6-7890-abcd-ef1234567890
--------------------------
>> sell 99.50 5
Trade executed: 100.00 @ a1b2c3d4-e5f6-7890-abcd-ef1234567890 (qty: 5)
OHLC [2025-07-07T10:30:45.123Z] => O: 100.00, H: 100.00, L: 100.00, C: 100.00
Order Book:
SELL:
BUY:
100.00 qty:5 id:a1b2c3d4-e5f6-7890-abcd-ef1234567890
--------------------------
pub struct Order {
pub id: Uuid, // Unique identifier
pub side: Side, // Buy or Sell
pub price: f64, // Order price
pub qty: u32, // Quantity
pub timestamp: i64, // Creation timestamp
}The OhlcTracker maintains:
- Open: First trade price
- High: Highest trade price
- Low: Lowest trade price
- Close: Most recent trade price
- Timestamp: Last trade timestamp
- Buy Orders: Match against sell orders with price ≤ buy price
- Sell Orders: Match against buy orders with price ≥ sell price
- Price Priority: Best prices are matched first
- Time Priority: Earlier orders at same price are matched first
- Order Insertion: O(log n) where n is number of price levels
- Order Matching: O(log n) for price lookup, O(1) for quantity matching
- Memory Usage: Efficient with minimal allocations during matching
- Scalability: Handles thousands of orders with sub-millisecond latency
cargo testcargo build --releasesrc/
├── main.rs # Application entry point
├── engine/
│ ├── mod.rs # Engine module declarations
│ ├── order_book.rs # Core matching engine
│ ├── types.rs # Data structures
│ └── ohlc.rs # OHLC tracking
└── utils/
├── mod.rs # Utility module declarations
└── input.rs # CLI input handling
- WebSocket API for real-time order streaming
- Persistence layer for order history
- Market data export functionality
- Performance benchmarking suite
- Multi-symbol support
- Advanced order types (stop-loss, iceberg orders)
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.