A simplified single-symbol limit order book exchange simulator implemented in C++17. Features price-time priority matching, order cancellation, and event-driven architecture.
Core Functionality:
- Limit order book with FIFO queues per price level
- Price-time priority order matching
- Trade execution with automatic order matching
- Partial fills support
- Order cancellation with FIFO preservation
- Top-of-book and full book queries
Architecture:
- Event-driven design (Observer pattern)
- Clean separation of concerns
- Batch file processing
- Interactive and file input modes
Testing:
- Comprehensive unit test coverage
- Golden test framework for regression testing
- Automated test file generation
# Build the project
mkdir build && cmake -S . -B build && cmake --build build
# Run interactively
./build/exchange_simulator
# Process a file
./build/exchange_simulator input.txt
# Run tests
./build/test_parser
./build/test_order_book
./build/test_matching_basic
./build/test_matching_cancel
./build/test_golden tests/data/tight_spread.txt tests/data/tight_spread_expected.txt
./build/test_performance tests/data/benchmark_100k.txtRequirements:
- CMake ≥ 3.16
- C++17 compatible compiler (GCC, Clang, MSVC)
Build Commands:
mkdir build
cmake -S . -B build
cmake --build buildRead commands from stdin:
./build/exchange_simulator
# Then type commands:
N 1 B 100 10
N 2 S 105 5
P
XProcess commands from a file:
./build/exchange_simulator input.txt| Command | Format | Description |
|---|---|---|
| N | N <order_id> <side> <price> <qty> |
New limit order |
| C | C <order_id> |
Cancel order |
| P | P |
Print top of book (best bid/ask) |
| B | B |
Print full book (all price levels) |
| X | X |
Exit |
Examples:
N 1 B 100 10- Buy order: ID=1, price=100, qty=10N 2 S 105 5- Sell order: ID=2, price=105, qty=5C 1- Cancel order ID 1P- Show best bid and ask
Validation:
- Order IDs must be positive integers
- Prices must be positive integers
- Quantities must be positive integers
- Side must be exactly "B" (buy) or "S" (sell)
The simulator outputs events as they occur:
| Event | Format | Description |
|---|---|---|
| ACK | ACK <order_id> |
Order accepted |
| TRD | TRD <buy_id> <sell_id> <price> <qty> |
Trade executed |
| REJ | REJ <order_id> <reason> |
Order rejected (BAD/DUP/UNK) |
| CXL | CXL <order_id> |
Order cancelled |
| TOB | TOB BID <price> <qty> |
Top of book bid |
| TOB | TOB ASK <price> <qty> |
Top of book ask |
| BOOK | BOOK BID <price> <qty> |
Full book bid level |
| BOOK | BOOK ASK <price> <qty> |
Full book ask level |
Example Output:
ACK 1
ACK 2
TRD 1 2 100 5
TOB BID 100 5
TOB ASK 105 5
Run all unit tests:
./build/test_parser
./build/test_order_book
./build/test_matching_basic
./build/test_matching_cancelGolden tests compare actual output against expected reference files.
Generate Test Files:
# Generate random test input
python3 scripts/generate_test.py -n 1000 -o test_input.txt --seed 42
# Generate expected output (golden reference)
./build/exchange_simulator test_input.txt > test_expected.txtRun Golden Tests:
./build/test_golden <input_file> <expected_file>Pre-generated Test Files:
./build/test_golden tests/data/tight_spread.txt tests/data/tight_spread_expected.txt
./build/test_golden tests/data/benchmark_100k.txt tests/data/benchmark_100k_expected.txtThe performance profiler measures pure logic latency and throughput using RAII ScopedTimer pattern. Timing excludes parsing and I/O overhead to measure only the core matching engine logic.
Run Performance Analysis:
./build/test_performance <input_file>Example:
./build/test_performance tests/data/benchmark_100k.txtMeasurement Approach:
- Start Timer: Immediately before calling
matching_engine.process_new_order()orcancel_order() - Stop Timer: Immediately after the engine returns (using RAII destructor)
- Excludes: Parsing, I/O, and other overhead that wouldn't exist in hardware/FPGA implementations
Metrics Reported:
- Total Operations: Number of operations processed (separate counts for orders and cancels)
- Mean Latency: Average time per operation (microseconds)
- P99 Latency: 99th percentile latency (microseconds)
- Throughput: Operations processed per second
Example Output:
=== Order Performance Statistics ===
Total Operations: 69841
Mean Latency: 1.60935 us
P99 Latency: 4.292 us
Throughput: 327136 Orders/sec
=== Cancel Performance Statistics ===
Total Operations: 20100
Mean Latency: 0.344536 us
P99 Latency: 1 us
Throughput: 94148.6 Cancels/sec
Completed Milestones:
- Milestone 1: Command parsing and validation
- Milestone 2: Limit order book with FIFO queues per price level
- Milestone 3: Matching engine with price-time priority matching
- Milestone 4: Order cancellation + Event system (Observer pattern)
- Milestone 5: File batch processing and golden tests
- Milestone 6: Performance analysis (latency and throughput metrics)
Project Structure:
exchange_simulator/
├── src/ # Source code
│ ├── parser.* # Command parsing
│ ├── order_book.* # Order book implementation
│ ├── matching_engine.* # Matching logic
│ ├── performance_profiler.* # Performance instrumentation
│ └── main.cpp # Application entry point
├── tests/ # Test files
│ ├── test_*.cpp # Unit tests
│ └── data/ # Test data files
├── scripts/ # Utility scripts
│ └── generate_test.py # Test file generator
└── build/ # Build output (generated)
Technology:
- C++17 standard
- CMake build system
- Observer pattern for event handling
- STL containers (map, list, unordered_map)