A high-performance, multi-threaded order book simulator built in C++17. Designed to simulate real financial market trading with advanced order types, real-time performance monitoring, and microsecond-level precision. Features comprehensive ICEBERG order support and professional-grade STOP order functionality.
- Multi-threaded engine with automatic CPU core detection
- Unified order book design for optimal cross-price matching
- Background trading simulation generating realistic market activity
- Performance: 15.4K+ matches/sec, 4.2M+ volume/sec, 14.4K+ orders/sec
- Hide large orders by showing only small portions at a time
- Automatic refill mechanism when visible portions are executed
- Prevents market impact from large institutional orders
- Supports both BUY and SELL ICEBERG orders with configurable display quantities
- LIMIT orders: Execute at specified price or better
- MARKET orders: Execute immediately at best available price
- STOP_LIMIT orders: Trigger at specified price, then execute as LIMIT
- STOP_MARKET orders: Trigger at specified price, then execute as MARKET
- ICEBERG orders: Large orders with hidden quantities
- Price collar validation prevents unrealistic STOP order executions
- Microsecond-precision timing measurements
- Live throughput and latency statistics
- Comprehensive benchmarking with CSV export
- Memory-safe operation under high-stress conditions
- Thread-safe logging across all components
- Detailed order lifecycle tracking
- Trade execution logs with timestamps
- Automatic log directory creation
- C++17 compiler (GCC 7+, Clang 5+, or Visual Studio 2017+)
- CMake 3.16 or newer
- Multi-core system recommended for optimal performance
# Clone the repository
git clone https://github.com/dorufloare/market-order-simulator.git
cd market-order-simulator
# Build the project
mkdir build && cd build
cmake ..
make -j$(nproc)
# Run the simulator
./OrderBookSimulator๐ Starting Market Order Simulator with Performance Benchmarking...
Hardware threads detected: 8
Using 8 worker threads
Starting high-volume background trading simulation...
Background generator started
=== Market Order Simulator ===
Commands:
- <BUY/SELL> <LIMIT/MARKET> <price> <quantity> : Place an order
- <BUY/SELL> <STOP_LIMIT/STOP_MARKET> <trigger_price> <limit_price> <quantity> : Place a STOP order
- <BUY/SELL> <ICEBERG> <price> <total_quantity> <display_quantity> : Place an ICEBERG order
- price : Show current last traded price
- help : Show detailed help
- stats : Show performance statistics
- quit : Exit the simulator
Examples:
BUY LIMIT 100.5 10 - Buy at $100.50 or better
SELL MARKET 0 5 - Sell immediately at market price
SELL STOP_LIMIT 95.0 94.5 10 - If price drops to $95, sell at $94.50
BUY ICEBERG 100.0 1000 100 - Buy 1000 shares, showing only 100 at a time
BUY STOP_MARKET 105.0 0 5 - If price rises to $105, buy at market
>
# Buy 100 shares at $99.50 or better
> BUY LIMIT 99.50 100
โ Order submitted: BUY LIMIT 100 @ $99.50
# Sell 50 shares at current market price
> SELL MARKET 0 50
โ Order submitted: SELL MARKET 50 @ Market ($98.75)
[MATCH] You sold 50 units @ $98.75# Place large ICEBERG order showing only small portions
> BUY ICEBERG 100.0 2000 300
โ ICEBERG Order submitted: BUY ICEBERG - Total: 2000 - Display: 300 @ $100.00
๐ง Your ICEBERG order is hiding 1700.00 shares behind the scenes...
[ICEBERG] Your ICEBERG BUY order placed. Showing 300 of 2000 shares @ $100.0
# When visible portion executes, more shares automatically appear
[MATCH] You bought 300 units @ $100.0
[ICEBERG REFILL] 300.0 more shares now visible @ $100.0 (remaining: 1700.0)
[ICEBERG REFILL] 300.0 more shares now visible @ $100.0 (remaining: 1400.0)
[ICEBERG COMPLETE] Your ICEBERG order fully executed!# STOP-LOSS: Sell if price drops to $95
> SELL STOP_LIMIT 95.0 94.5 100
โ STOP Order submitted: SELL STOP_LIMIT - Trigger: $95.00 โ Limit: $94.50 - Qty: 100.00
Your STOP order is now monitoring price movements...
[STOP] Your STOP-LIMIT SELL order placed. Will trigger when price <= $95
# BREAKOUT: Buy if price rises above $105
> BUY STOP_MARKET 105.0 0 50
โ STOP Order submitted: BUY STOP_MARKET - Trigger: $105.00 - Qty: 50.00
[STOP TRIGGERED] Your STOP BUY triggered at $105.50 -> executing MARKET order
[MATCH] You bought 50 units @ $105.50> stats
๐ === REAL-TIME PERFORMANCE STATS ===
Program Runtime: 75.58s
๐ Counters:
Volume Traded: 317222450 (4197191.2/sec)
Orders Matched: 1165898 (15426.1/sec)
Background Orders Generated: 1090003 (14421.9/sec)
Iceberg Orders Refilled: 212889 (2816.7/sec)
Stop Orders Triggered: 281108 (3719.4/sec)
Orders Resting: 617053 (8164.3/sec)
Stop Orders Placed: 362859 (4801.0/sec)
User Orders Submitted: 5 (0.1/sec)
โฑ๏ธ Timing Statistics:
Operation Count Avg(ms) Min(ms) Max(ms) Throughput(ops/sec)
-----------------------------------------------------------------------------------------------
Background Order Generation 1090003 0.069 0.004 24.842 14439.0
OrderBook Match 1090008 0.069 0.003 36.279 14463.5
Stop Trigger Check 388570 0.165 0.000 24.765 6043.7
Order Processing 5 21.126 9.110 36.279 47.3
Order Submission 5 0.008 0.003 0.020 120241.4
Order Queue Wait 6 12596.924 5394.187 23218.830 0.1The system features a clean, modular design optimized for performance and maintainability:
๐ src/
โโโ order_book.cpp # Core matching engine (~100 lines, focused)
โโโ order_book_base.cpp # Utility functions (price tracking)
โโโ stop_orders.cpp # STOP order logic & triggering
โโโ iceberg_orders.cpp # ICEBERG order management & refills
โโโ engine.cpp # Trading engine coordination
โโโ thread_pool.cpp # Multi-threading support
โโโ background_generator.cpp # Market simulation
โโโ main.cpp # Application entry point
- Main Thread: User interface and command processing
- Engine Thread: Order queue management and dispatching
- Worker Threads: Parallel order processing (auto-detects CPU cores)
- Background Generator: Realistic market activity simulation
User Input โ Engine Queue โ Order Book โ Matching Engine
โ
Background Generator โ Direct Order Book Integration
โ
All Activity โ Thread-Safe Logging โ CSV Files
- Unified Order Book: Single data structure for optimal cross-price matching
- Memory Safety: Fixed iterator invalidation issues in ICEBERG orders
- Lock Contention: Minimized through careful mutex design
- Cache Efficiency: Price-time priority queues for fast matching
๐ === PRODUCTION PERFORMANCE (75+ second sustained test) ===
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Metric โ Value โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Volume Throughput โ 4,197,191+ shares/sec โ
โ Order Matching Engine โ 15,426+ matches/sec โ
โ Background Generation โ 14,439+ orders/sec โ
โ ICEBERG Refill Rate โ 2,816+ refills/sec โ
โ STOP Order Triggers โ 3,719+ triggers/sec โ
โ STOP Trigger Checks โ 6,043+ ops/sec โ
โ Order Processing Latency โ 0.069ms average โ
โ Order Submission Speed โ 120,241+ orders/sec โ
โ Total Volume Processed โ 317M+ shares (75 seconds) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Refill Latency: Sub-millisecond automatic refills
- Memory Efficiency: Zero memory leaks under stress testing
- Cross-Price Matching: Full price improvement execution
- Large Order Support: Tested with 5000+ share ICEBERG orders
The simulator creates detailed CSV logs in the build/logs/ directory:
Timestamp,OrderID,UserID,Type,Side,Price,Quantity,TriggerPrice,Status
2025-07-21 12:37:15.708,10000,8438,LIMIT,SELL,119.21,6.13,0.00,SUBMITTED
2025-07-21 12:37:15.710,10000,8438,LIMIT,SELL,119.21,6.13,0.00,RESTING
2025-07-21 12:37:15.762,10001,2967,STOP_LIMIT,BUY,106.08,1.73,105.00,SUBMITTED
2025-07-21 12:37:15.763,10001,2967,STOP_LIMIT,BUY,106.08,1.73,105.00,STOP_PLACED
2025-07-21 12:37:16.125,10002,0,ICEBERG,BUY,100.00,2000,0.00,SUBMITTED
Timestamp,IncomingOrderID,RestingOrderID,MatchPrice,MatchQuantity,IncomingSide,RestingSide
2025-07-21 12:37:16.125,10002,10000,119.21,300.00,BUY,SELL
2025-07-21 12:37:16.847,10008,10003,106.08,1.73,SELL,BUY
2025-07-21 12:37:17.234,10009,10002,100.00,300.00,SELL,BUY
Operation,AvgTime(ms),MinTime(ms),MaxTime(ms),Count,Throughput(ops/sec)
OrderBook_Match,0.330,0.217,0.666,6,3030.3
Order_Processing,0.263,0.218,0.300,5,3801.9
Stop_Trigger_Check,0.000,0.000,0.000,3,7194244.6
Background_Order_Generation,0.734,0.734,0.734,1,1362.6
Iceberg_Order_Refill,0.125,0.100,0.150,5,8000.0
# Test large ICEBERG order with multiple refills
> BUY ICEBERG 100 2000 300
[ICEBERG] Showing 300 of 2000 shares @ $100.0
[ICEBERG REFILL] 300.0 more shares now visible (remaining: 1700.0)
[ICEBERG REFILL] 300.0 more shares now visible (remaining: 1400.0)
[ICEBERG COMPLETE] Your ICEBERG order fully executed!# Verify price improvement
> SELL LIMIT 95 1000 # Place sell at $95
> BUY ICEBERG 100 5000 500 # Buy at $100 - should match at $95
[MATCH] You bought 500 units @ $95.00 # โ
Price improvement working- โ Zero memory leaks detected
- โ No iterator invalidation crashes
- โ Thread-safe operation verified
- โ High-volume stress testing passed
This project is open source and available under the MIT License.
Performance Note: All benchmarks measured on 8-core system. Results may vary based on hardware configuration.