A high-performance C++ trading engine that simulates algorithmic trading strategies using historical market data. The engine processes orders efficiently, maintains realistic market behavior, and reports comprehensive performance metrics.
- High-Performance Order Book: Cache-friendly limit order book with time and price priority
- Strategy Framework: Pluggable architecture for multiple trading strategies
- Market Data Replay: Historical tick and bar data processing
- Performance Metrics: Comprehensive PnL, drawdown, and risk analysis
- Low-Latency Design: Optimized for speed with minimal memory allocations
- SMA Crossover Strategy: Simple Moving Average crossover signals
- VWAP Execution Strategy: Volume-Weighted Average Price execution
- Microsecond-level timing and benchmarking
- Cache-friendly data structures (std::vector preferred over std::list)
- Efficient order matching algorithm
- Real-time performance monitoring (ticks/sec, orders/sec)
- Modern C++ compiler (C++17 or newer)
- CMake 3.16 or higher
# Clone or download the project
mkdir build && cd build
cmake ..
make
# Run the backtester
./backtester
# Run tests
./testsMiniBacktester/
├── src/ # Source files
│ ├── main.cpp # Main application
│ ├── Engine.cpp # Trading engine implementation
│ ├── OrderBook.cpp # Limit order book
│ ├── Strategy.cpp # Base strategy class
│ ├── SMA.cpp # SMA crossover strategy
│ ├── VWAP.cpp # VWAP execution strategy
│ ├── CSVReader.cpp # Market data reader
│ └── PerformanceMetrics.cpp
├── include/ # Header files
├── data/ # Historical market data
│ └── sample_data.csv # Sample tick data
├── tests/ # Unit tests
└── CMakeLists.txt # Build configuration
# Run with default sample data
./backtester
# Run with custom data file
./backtester path/to/your/data.csvThe engine expects CSV files with the following format:
timestamp,symbol,price,volume
1640995200000000,AAPL,150.00,500
1640995200001000,AAPL,150.05,300Where:
timestamp: Microseconds since epochsymbol: Trading symbolprice: Price per sharevolume: Number of shares
Starting Mini Backtester & Trading Engine
========================================
Loading historical data from: data/sample_data.csv
Loaded 10000 ticks
Processing market data...
[SMA_AAPL_10_30] Bullish crossover detected. Buying 100 shares at 150.25
[VWAP_AAPL_500] Starting VWAP execution at 150.30
Backtest completed in 45 ms
Processing rate: 222,222 ticks/second
=== Trading Engine Summary ===
Total Trades: 15
Total PnL: $245.50
Active Positions: 1
Active Strategies: 2
Processing Rate: 222,222 ticks/second
=== Performance Metrics ===
Total PnL: $245.50
Max Drawdown: $12.30
Win Rate: 73.33%
Sharpe Ratio: 1.45
- Price Levels: Sorted vectors for cache efficiency
- Time Priority: FIFO within price levels
- Fast Matching: O(1) best bid/ask access
- Memory Efficient: Minimal dynamic allocations
class Strategy {
public:
virtual void onTick(const Tick& tick) = 0;
virtual void onTrade(const Trade& trade) = 0;
virtual void onOrderUpdate(const Order& order) = 0;
};- Cache-friendly data structures
- Pre-allocated vectors where possible
- Minimal string operations in hot paths
- Efficient order lookup with hash maps
- RAII for automatic resource management
Typical performance on modern hardware:
- Order Processing: 500,000+ orders/second
- Tick Processing: 200,000+ ticks/second
- Memory Usage: <100MB for 1M orders
- Latency: <1μs per order match
- Position tracking per symbol
- Realized and unrealized PnL calculation
- Maximum drawdown monitoring
- Configurable position limits
- Trade export to CSV
- Equity curve generation
- Performance metrics calculation
- Real-time statistics
- Plugin architecture for new strategies
- Configurable order types
- Multi-symbol support
- Custom performance metrics
Run the comprehensive test suite:
./testsTests cover:
- Order book functionality
- Strategy execution
- Performance metrics
- Edge cases and error handling
- Performance benchmarks
- Multi-threading support
- Real-time market data feeds
- Advanced order types (stop-loss, iceberg)
- Machine learning strategy integration
- Web-based dashboard
- Database persistence
- Risk management modules
This project demonstrates core concepts for algorithmic trading systems and is designed for educational and portfolio purposes. It showcases:
- Modern C++ best practices
- High-performance computing techniques
- Financial market microstructure
- Quantitative trading concepts
- Software architecture patterns
Perfect for demonstrating skills to quantitative trading firms, prop trading companies, and high-frequency trading organizations.
This project is provided as-is for educational and demonstration purposes.