A high-performance C++ trading simulator implementing a complete order book with matching engine functionality. Synapse provides realistic trading mechanics for backtesting, algorithm development, and educational purposes.
- Order Book Management: Maintains separate bid and ask order books with price-time priority
- Order Matching Engine: Implements realistic order matching logic for both market and limit orders
- Real-time Order Operations: Support for adding, canceling, and modifying orders
- Market & Limit Orders: Full support for both order types with proper execution semantics
- Limit Orders: Orders with specified price that wait in the book for matching
- Market Orders: Orders that execute immediately at best available prices
- Add Orders: Place new buy/sell orders in the book
- Cancel Orders: Remove existing orders from the book
- Modify Orders: Update price and quantity of existing orders
- Order Tracking: Unique order ID system for order lifecycle management
- Price-Time Priority: Orders matched based on price priority, then time priority
- Partial Fills: Support for partial order execution
- Cross-Side Matching: Automatic matching between buy and sell orders
- Trade Execution: Real-time trade reporting with execution details
struct Order {
uint64_t order_id; // Unique identifier
Side side; // BUY or SELL
OrderType type; // MARKET or LIMIT
double price; // Order price
uint64_t quantity; // Order quantity
timestamp; // Order creation time
};
- Bids: Buy orders stored in descending price order (highest first)
- Asks: Sell orders stored in ascending price order (lowest first)
- Order Map: Fast lookup for order cancellation and modification
std::map
with custom comparators for price-ordered booksstd::deque
for maintaining time priority within price levelsstd::unordered_map
for O(1) order lookup by ID
- C++17 compatible compiler (GCC, Clang, MSVC)
- CMake (optional, for future builds)
# Clone the repository
git clone <repository-url>
cd synapse
# Compile with g++
g++ -std=c++17 src/*.cpp -Iinclude -o trading_simulator
# Run the simulator
./trading_simulator
- Market Orders: Execute immediately against best available prices
- Limit Orders: Match against opposing orders at same or better prices
- Price Priority: Better prices get matched first
- Time Priority: Earlier orders at same price get matched first
- Order Addition: O(log n) for limit orders, O(n) for market orders
- Order Cancellation: O(log n) average case
- Order Modification: O(log n) (cancel + add)
- Book Display: O(n) where n is number of price levels
- Efficient use of STL containers
- Automatic cleanup of empty price levels
- Iterator-based order tracking for fast cancellation
Added limit order: ID=1 side=BUY price=100 qty=10
Added limit order: ID=2 side=SELL price=102 qty=15
===== ORDER BOOK =====
--- Asks (Sell) ---
Price Quantity
--------------------------
102 15
--- Bids (Buy) ---
Price Quantity
--------------------------
100 10
=========================
>> Trade executed: BUY 3 | SELL 2 | price 102 | qty 5
- Historical Data Replay: Load and replay historical market data
- Strategy Framework: Plugin system for trading strategies
- Risk Management: Position limits and risk controls
- Market Data Feed: Real-time market data integration
- Performance Analytics: Trade statistics and performance metrics
- Web Interface: Browser-based trading interface
- Database Integration: Order and trade persistence
- Multi-Asset Support: Support for different asset classes
- Multithreading: Concurrent order processing
- Network Interface: TCP/UDP order entry
- Configuration System: JSON/YAML configuration files
- Logging System: Structured logging for debugging
- Unit Tests: Comprehensive test suite
- Benchmarking: Performance measurement tools
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by real-world electronic trading systems
- Built with modern C++ best practices
- Designed for educational and research purposes
Note: This is a simulation environment. Not intended for production trading without proper risk management and regulatory compliance.