A C++ implementation of a high-frequency trading simulator that models order book mechanics, order matching, and trade execution with optional latency simulation.
This simulator provides a realistic environment for testing trading strategies and understanding market microstructure. It implements:
- Order Book Management: Maintains separate bid and ask books with price-time priority
- Order Matching Engine: Matches orders according to standard exchange rules
- Trade Execution: Generates and logs completed trades
- Latency Simulation: Optional delay simulation for realistic HFT scenarios
- Market Data: Real-time best bid/ask prices and sizes
- Order Submission: User/agent submits order to exchange
- Event Queue: Order enters event queue (if latency enabled)
- Exchange Processing: Exchange processes order from queue
- Order Book Matching: OrderBook attempts to match order against opposite side
- Trade Generation: Matched orders generate trades
- Book Update: Remaining order quantity added to book
- C++17 or later
- CMake (optional, for build system)
cd Code-Projects/HFT
g++ -std=c++17 -O2 -o hft_simulator src/*.cpp./hft_simulator#include "ExchangeSimulator.hpp"
int main() {
ExchangeSimulator exchange;
// Submit limit orders to build the book
exchange.submitOrder(100.50, 100, Side::BUY, OrderType::LIMIT, 1);
exchange.submitOrder(101.00, 100, Side::SELL, OrderType::LIMIT, 2);
// Submit a market order that will match
exchange.submitOrder(0.0, 50, Side::BUY, OrderType::MARKET, 3);
// Print results
exchange.printOrderBook();
exchange.printTradeLog();
exchange.printStatistics();
return 0;
}ExchangeSimulator exchange(true); // Enable latency
// Set custom latency generator (1-10ms delay)
exchange.setLatencyGenerator([]() {
return std::rand() % 9000 + 1000; // 1-10ms in microseconds
});
// Submit orders (they go to queue)
exchange.submitOrder(100.50, 100, Side::BUY, OrderType::LIMIT, 1);
exchange.submitOrder(101.00, 100, Side::SELL, OrderType::LIMIT, 2);
// Process the queue
exchange.processEventQueue();- Limit Orders: Orders with specific price limits
- Market Orders: Orders that execute at best available price
- Price-Time Priority: Orders matched by price first, then time
- Partial Fills: Orders can be partially filled
- Market Orders: Execute immediately against best available price
- Best Bid/Ask: Current best bid and ask prices
- Market Depth: Size at best bid/ask levels
- Order Book Display: Full order book visualization
- Trade Log: Complete history of all executed trades
- Performance Metrics: Orders processed, trades executed, total volume
- Real-time Statistics: Live market statistics
The simulator includes comprehensive tests:
- Basic Test: Simple order submission and matching
- Stress Test: 1000 random orders to test performance
- Latency Test: Order processing with simulated delays
Run tests with:
./hft_simulator- Order Processing: ~1-10 microseconds per order (depending on book depth)
- Memory Usage: Efficient data structures with minimal overhead
- Scalability: Designed to handle high-frequency order flows
- Multiple instruments/symbols
- Advanced order types (stop-loss, iceberg, etc.)
- Market maker algorithms
- Risk management systems
- Network latency simulation
- Historical data replay
- WebSocket API for real-time data
- Configuration file support
- Fork the repository
- Create a feature branch
- Implement your changes
- Add tests for new functionality
- Submit a pull request
This project is open source and available under the MIT License.