Skip to content

dorufloare/market-order-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

11 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Market Order Simulator

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.

โœจ Key Features

High-Performance Architecture

  • 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

Advanced ICEBERG Orders

  • 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

Professional Order Types

  • 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

Real-Time Performance Monitoring

  • Microsecond-precision timing measurements
  • Live throughput and latency statistics
  • Comprehensive benchmarking with CSV export
  • Memory-safe operation under high-stress conditions

Logging

  • Thread-safe logging across all components
  • Detailed order lifecycle tracking
  • Trade execution logs with timestamps
  • Automatic log directory creation

๐Ÿ› ๏ธ How to Build & Run

Prerequisites

  • C++17 compiler (GCC 7+, Clang 5+, or Visual Studio 2017+)
  • CMake 3.16 or newer
  • Multi-core system recommended for optimal performance

Build Steps

# 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

Live Performance Demo

๐Ÿš€ 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

> 

๐ŸŽฎ Trading Commands

Basic Orders

# 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

ICEBERG Orders (Large Order Stealth)

# 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 Orders (Risk Management)

# 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

Real-Time Performance Stats

> 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.1

๐Ÿ—๏ธ Modular Architecture

The system features a clean, modular design optimized for performance and maintainability:

Core Components

๐Ÿ“ 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

Threading Model

  • 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

Order Flow

User Input โ†’ Engine Queue โ†’ Order Book โ†’ Matching Engine
                โ†“
Background Generator โ†’ Direct Order Book Integration
                โ†“
All Activity โ†’ Thread-Safe Logging โ†’ CSV Files

Performance Optimizations

  • 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

๐Ÿ“ˆ Benchmark Results

Live Performance Metrics

๐Ÿ† === 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)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

ICEBERG Order Performance

  • 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

๐Ÿ“Š Logging

The simulator creates detailed CSV logs in the build/logs/ directory:

Order Lifecycle Tracking (orders.log)

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

Trade Execution Log (matches.log)

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

Performance Benchmarks (benchmarks.log)

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

๐Ÿงช Testing & Validation

ICEBERG Order Testing

# 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!

Cross-Price Matching Validation

# 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

Memory Safety Under Stress

  • โœ… Zero memory leaks detected
  • โœ… No iterator invalidation crashes
  • โœ… Thread-safe operation verified
  • โœ… High-volume stress testing passed

๐Ÿ“„ License

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.

About

Multi-threaded C++ order book simulator. Built to learn financial markets and microsecond optimization

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published