diff --git a/.gitignore b/.gitignore index 876583c..3ae8c65 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,11 @@ orimakefile include/PreprocessOld.hpp src/PreprocessOld.cpp .cache/ -.vscode/ \ No newline at end of file +.vscode/ + +# Build artifacts +obj/ +bin/ +*.o +tests/helper.o +tests/testHealth \ No newline at end of file diff --git a/README.md b/README.md index e69de29..5337e6b 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,730 @@ +# Xchange - High-Performance Order Matching Engine + +A sophisticated C++ implementation of a financial exchange system featuring real-time order matching, comprehensive order type support, and intelligent order preprocessing. Built with modern C++23 features for optimal performance and type safety. + +## ๐ŸŽฏ Project Overview + +**Xchange** is a complete order matching engine that simulates real-world financial exchange operations. It handles multiple participants trading various symbols with support for 10 different order types, time-based order activation, and priority-based matching algorithms. + +### Key Features +- **Multi-Symbol Trading**: Support for trading multiple financial instruments simultaneously +- **10 Order Types**: Comprehensive support for various order types including Market, Limit, GoodTillCancel, GoodTillDate, GoodForDay, GoodAfterTime, MarketOnOpen, MarketOnClose, ImmediateOrCancel, FillOrKill, and AllOrNone +- **Smart Order Preprocessing**: Intelligent order queue management with configurable thresholds +- **Priority-Based Matching**: Price-time priority for fair and efficient order execution +- **Participant Management**: Track multiple participants with portfolios and trade history +- **Time Zone Support**: Configurable time zones with trading hours validation +- **Holiday Calendar**: Market holiday awareness for realistic trading simulations + +--- + +## ๐Ÿ—๏ธ Architecture + +### System Components + +The system is organized into a layered architecture with clear separation of concerns: + +``` +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ Xchange โ”‚ +โ”‚ (Central Exchange - Singleton Pattern) โ”‚ +โ”‚ โ€ข Participant Management โ”‚ +โ”‚ โ€ข Symbol Registration โ”‚ +โ”‚ โ€ข Order Routing โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ โ”‚ + โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” + โ”‚ Participant โ”‚ โ”‚ SymbolInfo โ”‚ + โ”‚ โ€ข Portfolio โ”‚ โ”‚ โ€ข OrderBook โ”‚ + โ”‚ โ€ข Orders โ”‚ โ”‚ โ€ข PreProcessors โ”‚ + โ”‚ โ€ข Trade History โ”‚ โ”‚ (Buy & Sell) โ”‚ + โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ + โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” + โ”‚ โ”‚ + โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” + โ”‚ PreProcessor โ”‚ โ”‚ OrderBook โ”‚ + โ”‚ (Per Side/Symbol) โ”‚ โ”‚ (Per Symbol) โ”‚ + โ”‚ โ€ข Order Queue โ”‚โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ€ข Bid Levels โ”‚ + โ”‚ โ€ข Time Validation โ”‚ โ”‚ โ€ข Ask Levels โ”‚ + โ”‚ โ€ข Type Ranking โ”‚ โ”‚ โ€ข Matching โ”‚ + โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ โ”‚ + โ”‚ โ”‚ + โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” + โ”‚ Order โ”‚ โ”‚ Level โ”‚ + โ”‚ โ€ข Order Details โ”‚ โ”‚ โ€ข Price Level โ”‚ + โ”‚ โ€ข Time Attributes โ”‚ โ”‚ โ€ข Order Queue โ”‚ + โ”‚ โ€ข Status โ”‚ โ”‚ โ€ข Quantity โ”‚ + โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ + โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” + โ”‚ Trade โ”‚ + โ”‚ โ€ข Matched โ”‚ + โ”‚ Orders โ”‚ + โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +``` + +--- + +## ๐Ÿ“ฆ Component Details + +### 1. **Xchange** (Central Exchange) +**File**: `include/Xchange.hpp`, `src/Xchange.cpp` + +The main orchestrator implementing the **Singleton Pattern** to ensure a single exchange instance. + +**Responsibilities**: +- Manage participants (add, remove, lookup by government ID) +- Register and retire tradable symbols +- Route orders to appropriate order books and preprocessors +- Validate trading hours and market status +- Provide system-wide configuration (order thresholds, pending duration) + +**Key Features**: +- Singleton pattern with custom initialization parameters +- Maps government IDs to unique participant IDs +- Manages `SymbolInfo` objects for each tradable symbol +- Configurable pending order thresholds and durations +- Time zone aware with trading hours validation + +### 2. **SymbolInfo** (Symbol Container) +**File**: `include/SymbolInfo.hpp`, `src/SymbolInfo.cpp` + +A lightweight struct that groups all components needed for trading a specific symbol. + +**Components**: +- One `OrderBook` per symbol +- Two `PreProcessor` instances (one for bids, one for asks) +- Symbol identifier + +**Purpose**: Encapsulates symbol-specific trading infrastructure for clean organization. + +### 3. **OrderBook** (Matching Engine) +**File**: `include/OrderBook.hpp`, `src/OrderBook.cpp` + +The core matching engine that maintains price levels and executes trades. + +**Data Structure**: +- **Bid Levels**: `std::map>` (highest price first) +- **Ask Levels**: `std::map>` (lowest price first) +- **Trade History**: Vector of executed trades + +**Operations**: +- `AddOrder()`: Insert order at appropriate price level +- `CancelOrder()`: Remove order from order book +- `ModifyOrder()`: Update existing order +- `MatchPotentialOrders()`: Execute price-time priority matching + +**Matching Algorithm**: +1. Check if bid price โ‰ฅ ask price (crossing condition) +2. Match orders at the crossing price +3. Fill orders based on time priority (FIFO at each price level) +4. Generate `Trade` objects for matched orders +5. Update quantities and remove fully filled orders + +### 4. **PreProcessor** (Order Queue Manager) +**File**: `include/Preprocess.hpp`, `src/PreProcess.cpp` + +Intelligent order queue that validates and schedules orders before they enter the OrderBook. + +**Key Responsibilities**: +- Buffer orders and validate activation/deactivation times +- Rank orders by type priority (Market > ImmediateOrCancel > GoodTillCancel, etc.) +- Enforce pending order thresholds to prevent queue overflow +- Check trading hours and holiday calendar +- Flush qualified orders to OrderBook based on time and count triggers + +**Order Type Ranking** (Higher rank = Higher priority): +``` +Market (highest priority) +FillOrKill +ImmediateOrCancel +MarketOnClose +MarketOnOpen +GoodAfterTime +GoodForDay +GoodTillDate +GoodTillCancel +AllOrNone (lowest priority) +``` + +**Configuration**: +- `MAX_PENDING_ORDERS_THRESHOLD`: Trigger flush when queue exceeds this count +- `MAX_PENDING_DURATION`: Flush interval in milliseconds +- Holiday calendar with Indian market holidays for 2025 +- Trading hours validation (configurable per symbol) + +### 5. **Level** (Price Level) +**File**: `include/Level.hpp`, `src/Level.cpp` + +Represents a single price point in the order book with multiple orders at that price. + +**Data Structure**: +- `m_orderList`: Linked list of orders (maintains time priority) +- `m_info`: Hash map for O(1) order lookup +- `m_quantity`: Aggregate quantity at this level + +**Operations**: +- `AddOrder()`: Append to end of order list (time priority) +- `CancelOrder()`: Remove specific order +- `ModifyOrder()`: Update order details +- `removeMatchedOrder()`: Clean up after trade execution + +**Benefits**: +- Fast aggregation of quantities at each price +- Efficient order lookup with O(1) complexity +- Maintains strict time priority within price level + +### 6. **Order** (Order Object) +**File**: `include/Order.hpp`, `src/Order.cpp` + +Core data structure representing a single order. + +**Attributes**: +- Symbol, side (Buy/Sell), price, quantity +- Order type (Market, Limit, GoodTillDate, etc.) +- Participant ID +- Timestamps: creation, activation, deactivation +- Order status (NotProcessed, Processing, Fulfilled, Cancelled) +- Unique Order ID (encoded with timestamp, price, and side) + +**Key Methods**: +- `encodeOrderID()`: Generate unique 64-bit order ID +- `FillPartially()`: Update quantity on partial fill +- `convertDateTimeToTimeStamp()`: Parse time strings +- `returnReadableTime()`: Format timestamps for display + +### 7. **Participant** (Trader) +**File**: `include/Participant.hpp`, `src/Participant.cpp` + +Represents a market participant with portfolio and order history. + +**Tracks**: +- Portfolio: Map of symbols to net positions (amounts) +- Order composition: All orders placed by participant +- Trade history: Record of all executed trades +- Order status: Pending, processing, fulfilled, cancelled counts + +**Capabilities**: +- Record new orders (non-cancel and cancel) +- Update portfolio on trade execution +- Calculate portfolio valuation +- Query order and trade statistics + +### 8. **Trade** (Matched Order Pair) +**File**: `include/Trade.hpp` + +Immutable record of a successful trade between two orders. + +**Contains**: +- Matched bid order (buyer) +- Matched ask order (seller) +- Symbol +- Match timestamp + +**Purpose**: Provides audit trail and trade history for participants and the exchange. + +--- + +## ๐Ÿ”— Component Interactions + +### Order Flow (Complete Lifecycle) + +``` +1. Participant Submits Order + โ†“ +2. Xchange.placeOrder() + โ€ข Validates participant exists + โ€ข Routes to appropriate SymbolInfo + โ†“ +3. PreProcessor.InsertIntoPreprocessing() + โ€ข Validates time attributes + โ€ข Checks market hours & holidays + โ€ข Adds to type-ranked queue + โ†“ +4. PreProcessor.TryFlush() [Triggered by time/count] + โ€ข Sorts orders by type priority + โ€ข Validates activation times + โ€ข Flushes qualified orders + โ†“ +5. OrderBook.AddOrder() + โ€ข Finds/creates price level + โ€ข Adds order to level + โ†“ +6. OrderBook.MatchPotentialOrders() + โ€ข Checks for crossing orders + โ€ข Executes trades (price-time priority) + โ€ข Generates Trade objects + โ†“ +7. Participant.recordTrades() + โ€ข Updates portfolio + โ€ข Records trade history + โ€ข Updates order status +``` + +### Data Flow Example + +``` +Participant A: Buy 100 AAPL @ $150 +Participant B: Sell 50 AAPL @ $149 + +Flow: +1. Both orders enter respective PreProcessors (Buy/Sell) +2. PreProcessor flushes orders to OrderBook +3. OrderBook detects crossing ($150 bid โ‰ฅ $149 ask) +4. Match 50 shares at $149 (ask price) +5. Create Trade object +6. Participant A: +50 AAPL, -$7,450 +7. Participant B: -50 AAPL, +$7,450 +8. Participant A's order: 50 shares remaining (partial fill) +9. Participant B's order: fully filled, removed from book +``` + +--- + +## ๐ŸŽจ Key C++ Features Utilized + +### Modern C++ Features +1. **`#pragma once`**: Modern header guard replacing traditional `#ifndef` guards +2. **`std::chrono`**: Comprehensive time handling with `system_clock::time_point` (C++11) +3. **`constexpr`**: Compile-time holiday array in PreProcessor (C++11) +4. **Range-based loops**: Used throughout for container iteration (C++11) +5. **C++23 Standard**: Project is compiled with `-std=c++23` flag for latest language features + +### Smart Pointers & Memory Management +```cpp +// Shared ownership across multiple preprocessors +std::shared_ptr m_orderbookPtr; + +// Unique ownership in Xchange singleton +static std::unique_ptr m_instance; + +// Type aliases for clarity +using OrderPointer = std::shared_ptr; +using LevelPointer = std::shared_ptr; +using PreProcessorPointer = std::shared_ptr; +``` + +**Benefits**: +- Automatic memory management (no manual `delete`) +- Shared ownership for OrderBook across preprocessors +- Exception-safe resource management + +### STL Containers (Strategic Choices) + +#### 1. `std::map` (OrderBook Levels) +```cpp +std::map> m_bids; // Max-heap behavior +std::map> m_asks; // Min-heap behavior +``` +**Reason**: Maintains sorted price levels for efficient matching + +#### 2. `std::unordered_map` (Fast Lookups) +```cpp +std::unordered_map m_orderComposition; +std::unordered_map m_participants; +``` +**Reason**: O(1) lookup for orders and participants by ID + +#### 3. `std::set` (Ordered Collections) +```cpp +std::set m_laterProcessOrders; +``` +**Reason**: Automatic sorting with custom comparator, no duplicates + +#### 4. `std::list` (Order Queue at Level) +```cpp +OrderList m_orderList; // typedef std::list +``` +**Reason**: Efficient insertion/deletion while maintaining time priority + +#### 5. `std::vector` (Trade History) +```cpp +std::vector m_trades; +``` +**Reason**: Fast sequential access for trade records + +### Enumerations (Type Safety) +```cpp +namespace Side { + enum Side { Buy, Sell }; +} + +namespace OrderType { + enum OrderType { + AllOrNone, GoodTillCancel, GoodTillDate, + GoodForDay, GoodAfterTime, MarketOnOpen, + MarketOnClose, ImmediateOrCancel, + FillOrKill, Market + }; +} + +namespace OrderStatus { + enum OrderStatus { + NotProcessed, Processing, Fulfilled, Cancelled + }; +} +``` +**Benefits**: Type-safe constants with namespace isolation + +### Templates & Type Aliases +```cpp +// Type aliases for readability +using Price = std::int32_t; +using Quantity = std::uint64_t; +using Symbol = std::string; +using TimeStamp = std::chrono::system_clock::time_point; +using OrderID = std::uint64_t; +using ParticipantID = std::string; +using Amount = double; +using Portfolio = std::unordered_map; + +// Template comparators in std::map +std::map> bids; // Descending +std::map> asks; // Ascending +``` + +### Design Patterns + +#### 1. **Singleton Pattern** (Xchange) +```cpp +class Xchange { +private: + static std::unique_ptr m_instance; + Xchange(...); // Private constructor + +public: + Xchange(const Xchange&) = delete; // Delete copy + Xchange& operator=(const Xchange&) = delete; // Delete assignment + + static Xchange& getInstance(...); + static void destroyInstance(); +}; +``` +**Purpose**: Ensure single exchange instance across the system + +#### 2. **RAII** (Resource Acquisition Is Initialization) +- Smart pointers automatically manage Order, Level, OrderBook lifetimes +- No manual memory management required +- Exception-safe cleanup + +#### 3. **Struct for Data Aggregation** (SymbolInfo) +```cpp +struct SymbolInfo { + Symbol m_symbol; + OrderBookPointer m_orderbook; + PreProcessorPointer m_bidprepro; + PreProcessorPointer m_askprepro; +}; +``` +**Purpose**: Lightweight container for related components + +### Operator Overloading +```cpp +bool Order::operator<(const Order& other) const { + return m_timestamp < other.m_timestamp; // Time priority +} + +bool Order::operator==(const Order& other) const { + return m_orderID == other.m_orderID; +} +``` +**Purpose**: Enable natural comparison syntax and STL container compatibility + +### Static Members & Methods +```cpp +class OrderBook { +public: + static Price decodePriceFromOrderID(const OrderID orderID); + static Side::Side decodeSideFromOrderID(const OrderID orderID); +}; + +class PreProcessor { +private: + static std::unordered_map m_typeRank; + static constexpr std::array, 15> m_holidays = {...}; +}; +``` +**Purpose**: Shared state/logic across instances, utility functions + +### Optional Type (Safety) +```cpp +std::optional AddOrder(Order& order); +std::optional placeOrder(...); +std::optional getOrderInfo(const OrderID& orderID); +``` +**Benefits**: +- Explicit handling of "no value" cases +- Avoid null pointer errors +- Clear API contracts + +### Const Correctness +```cpp +Price getPrice() const { return m_price; } +Symbol getSymbol() const { return m_symbol; } +const OrderTraded& getMatchedBid() const { return m_bidMatch; } +``` +**Benefits**: +- Compile-time guarantees that methods don't modify state +- Enables optimization +- Prevents accidental mutations + +### Inline Functions & Getters +```cpp +Symbol getSymbol() const { return m_symbol; } +Price getPrice() const { return m_price; } +``` +**Benefits**: Compiler can inline for zero-cost abstraction + +--- + +## ๐Ÿ“ Project Structure + +``` +xchange/ +โ”œโ”€โ”€ include/ # Public header files +โ”‚ โ”œโ”€โ”€ Xchange.hpp # Central exchange +โ”‚ โ”œโ”€โ”€ OrderBook.hpp # Matching engine +โ”‚ โ”œโ”€โ”€ Preprocess.hpp # Order preprocessing +โ”‚ โ”œโ”€โ”€ Participant.hpp # Trader representation +โ”‚ โ”œโ”€โ”€ Level.hpp # Price level +โ”‚ โ”œโ”€โ”€ Order.hpp # Order object +โ”‚ โ”œโ”€โ”€ Trade.hpp # Trade record +โ”‚ โ”œโ”€โ”€ OrderTraded.hpp # Matched order details +โ”‚ โ””โ”€โ”€ SymbolInfo.hpp # Symbol container +โ”œโ”€โ”€ src/ # Implementation files +โ”‚ โ”œโ”€โ”€ Xchange.cpp +โ”‚ โ”œโ”€โ”€ OrderBook.cpp +โ”‚ โ”œโ”€โ”€ PreProcess.cpp +โ”‚ โ”œโ”€โ”€ Participant.cpp +โ”‚ โ”œโ”€โ”€ Level.cpp +โ”‚ โ”œโ”€โ”€ Order.cpp +โ”‚ โ””โ”€โ”€ SymbolInfo.cpp +โ”œโ”€โ”€ utils/ # Utilities and type definitions +โ”‚ โ”œโ”€โ”€ enums/ # Enumerations +โ”‚ โ”‚ โ”œโ”€โ”€ Side.hpp # Buy/Sell +โ”‚ โ”‚ โ”œโ”€โ”€ OrderTypes.hpp # 10 order types +โ”‚ โ”‚ โ”œโ”€โ”€ OrderStatus.hpp # Order lifecycle states +โ”‚ โ”‚ โ””โ”€โ”€ Actions.hpp # Add/Modify/Cancel +โ”‚ โ”œโ”€โ”€ alias/ # Type aliases +โ”‚ โ”‚ โ”œโ”€โ”€ Fundamental.hpp # Basic types (Price, Quantity, etc.) +โ”‚ โ”‚ โ”œโ”€โ”€ OrderRel.hpp # Order-related pointers +โ”‚ โ”‚ โ”œโ”€โ”€ LevelRel.hpp # Level-related pointers +โ”‚ โ”‚ โ””โ”€โ”€ ... +โ”‚ โ”œโ”€โ”€ helpers/ # Helper functions +โ”‚ โ”‚ โ”œโ”€โ”€ HelperFunctions.hpp +โ”‚ โ”‚ โ””โ”€โ”€ HelperFunctions.cpp +โ”‚ โ””โ”€โ”€ Constants.hpp # System constants +โ”œโ”€โ”€ tests/ # Test files (GTest framework) +โ”‚ โ”œโ”€โ”€ core-xchange.tests.cpp # Xchange tests +โ”‚ โ”œโ”€โ”€ core-part.tests.cpp # Participant tests +โ”‚ โ”œโ”€โ”€ core-pre.tests.cpp # PreProcessor tests +โ”‚ โ”œโ”€โ”€ actual/ +โ”‚ โ”‚ โ”œโ”€โ”€ core-ob.tests.cpp # OrderBook tests +โ”‚ โ”‚ โ””โ”€โ”€ core-lev.tests.cpp # Level tests +โ”‚ โ”œโ”€โ”€ cases/ # Test case data files +โ”‚ โ””โ”€โ”€ testHandler.cpp # Test utilities +โ”œโ”€โ”€ makefile # Build configuration +โ”œโ”€โ”€ .gitignore +โ””โ”€โ”€ README.md # This file +``` + +--- + +## ๐Ÿ› ๏ธ Building and Testing + +### Prerequisites +- **Compiler**: g++ with C++23 support (GCC 12+ or Clang 15+ recommended) +- **Build System**: GNU Make +- **Testing**: Google Test (GTest) framework + +### Build Commands + +```bash +# Clean build artifacts +make clean + +# Build object files +make getObjectFiles + +# Build and run (uses tests/core-pre.cpp as entry point) +make fresh + +# Run existing executable +make run +``` + +### Testing Commands + +```bash +# Run test health check +make testHealth + +# Build all test binaries +make testFiles + +# Run all tests +make test + +# Run all tests and clean up +make testAll + +# Clean test artifacts +make cleanTests +``` + +### Build Configuration + +The project uses a sophisticated Makefile with: +- **Compiler**: `g++` +- **C++ Standard**: `-std=c++23` +- **Flags**: `-Wall -Wextra -pedantic-errors` (strict warnings) +- **Debug**: `-ggdb -O0` (debug symbols, no optimization) +- **Include Path**: `-I.` (root directory) + +**Directory Structure**: +- `src/` โ†’ `obj/*.o` (compiled objects) +- `tests/*.tests.cpp` โ†’ `tests/bin/*` (test executables) + +--- + +## ๐Ÿงช Test Coverage + +The project includes comprehensive tests organized by component: + +### Test Files +1. **core-xchange.tests.cpp**: Exchange operations (participants, symbols, order routing) +2. **core-part.tests.cpp**: Participant portfolio and order tracking +3. **core-pre.tests.cpp**: PreProcessor queue management and order validation +4. **core-ob.tests.cpp**: OrderBook matching logic +5. **core-lev.tests.cpp**: Level operations + +### Test Cases +The `tests/cases/` directory contains structured test data for: +- **PreProcessor**: Order insertion, removal, modification, queue flushing +- **OrderBook**: Matching scenarios (no match, partial match, complete match, time priority) + +--- + +## ๐Ÿš€ Usage Example + +```cpp +// Initialize exchange with thresholds +Xchange& xchange = Xchange::getInstance( + 50, // Pending order threshold + 5000, // Pending duration (ms) + "America/New_York" // Time zone +); + +// Add participants +ParticipantID trader1 = xchange.addParticipant("GOV_ID_12345"); +ParticipantID trader2 = xchange.addParticipant("GOV_ID_67890"); + +// Enable symbol trading +xchange.tradeNewSymbol("AAPL"); + +// Place buy order +auto orderID = xchange.placeOrder( + trader1, // Participant + Actions::Add, // Action + std::nullopt, // Order ID (null for new) + "AAPL", // Symbol + Side::Buy, // Side + OrderType::GoodTillCancel, // Order type + 15000, // Price ($150.00) + 100, // Quantity + std::nullopt, // Activation time + std::nullopt // Deactivation time +); + +// Place sell order +xchange.placeOrder( + trader2, + Actions::Add, + std::nullopt, + "AAPL", + Side::Sell, + OrderType::Market, + 14900, // Price ($149.00) + 50, // Quantity + std::nullopt, + std::nullopt +); + +// Check trades +auto trades = xchange.getTradesExecuted("AAPL"); +for (const auto& trade : trades) { + std::cout << "Trade executed: " << trade.getSymbol() << std::endl; +} + +// Cleanup +Xchange::destroyInstance(); +``` + +--- + +## ๐ŸŽฏ Key Highlights + +### Performance Optimizations +1. **O(1) Order Lookup**: Hash maps for instant order retrieval +2. **Sorted Price Levels**: `std::map` with custom comparators for efficient matching +3. **Time Priority**: Linked lists maintain FIFO order at each price level +4. **Smart Pointer Sharing**: Avoid deep copies with shared ownership + +### Scalability Features +1. **Multi-Symbol Support**: Independent order books per symbol +2. **Configurable Thresholds**: Adjust queue sizes and flush intervals +3. **Extensible Order Types**: Easy to add new order type behaviors +4. **Modular Architecture**: Components can be tested and optimized independently + +### Real-World Considerations +1. **Time Zone Awareness**: Convert between local and GMT times +2. **Trading Hours Validation**: Prevent orders outside market hours +3. **Holiday Calendar**: Skip non-trading days +4. **Order Status Tracking**: Monitor order lifecycle from submission to fulfillment + +--- + +## ๐Ÿ“š Learning Resources + +### C++ Features Used +- **Modern C++**: [C++23 Standard](https://en.cppreference.com/w/cpp/23) +- **Smart Pointers**: [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr), [std::unique_ptr](https://en.cppreference.com/w/cpp/memory/unique_ptr) +- **STL Containers**: [std::map](https://en.cppreference.com/w/cpp/container/map), [std::unordered_map](https://en.cppreference.com/w/cpp/container/unordered_map), [std::list](https://en.cppreference.com/w/cpp/container/list) +- **Chrono Library**: [std::chrono](https://en.cppreference.com/w/cpp/chrono) +- **Optional**: [std::optional](https://en.cppreference.com/w/cpp/utility/optional) + +### Design Patterns +- **Singleton Pattern**: Ensures single instance +- **RAII**: Resource management through object lifetime +- **Type Aliases**: Improve code readability + +--- + +## ๐Ÿค Contributing + +This project demonstrates: +- Clean architecture with separation of concerns +- Extensive use of modern C++ features +- Comprehensive test coverage +- Real-world financial system modeling + +--- + +## ๐Ÿ“„ License + +*Add your license information here* + +--- + +## ๐Ÿ‘ค Author + +**Ash4dev** - [GitHub Profile](https://github.com/Ash4dev) + +--- + +## ๐Ÿ™ Acknowledgments + +- Financial order book algorithms and market microstructure concepts +- Modern C++ community for best practices and patterns +- Google Test framework for robust testing infrastructure diff --git a/tests/sample/TestHealth.txt b/tests/sample/TestHealth.txt index 617350c..e69de29 100644 --- a/tests/sample/TestHealth.txt +++ b/tests/sample/TestHealth.txt @@ -1,132 +0,0 @@ -Update { - updateCount: 1 - action: 0 - symbol: SOY - orderType: 9 - side: 0 - price: 12 - quantity: 23 - participantID: 12_NF23 -} -activate: 2025-07-01 19:12:27 -activate: 2100-01-01 00:00:00 -Update { - updateCount: 2 - action: 0 - symbol: APL - orderType: 2 - side: 1 - price: 23 - quantity: 12 - participantID: 193_2H6G -} -activate: 2025-07-01 19:12:27 -activate: 2025-07-03 14:13:24 -Update { - updateCount: 1 - action: 2 - symbol: (null) - orderType: (null) - side: (null) - price: (null) - quantity: (null) - participantID: (null) -} -Update { - updateCount: 3 - action: 0 - symbol: TSL - orderType: 4 - side: 0 - price: 45 - quantity: 124 - participantID: 1_JHFG -} -activate: 2025-07-01 09:09:09 -activate: 2100-01-01 00:00:00 -Update { - updateCount: 2 - action: 1 - symbol: (null) - orderType: 9 - side: (null) - price: 67 - quantity: (null) - participantID: (null) -} -activate: 2025-07-01 19:12:27 -activate: 2100-01-01 00:00:00 -PreProcessorResult { - symbol: TSL - side: 0 - action: 0 - price: 45 - quantity: 124 - participantID: 1_LOL1 -} -PreProcessorResult { - symbol: LOL - side: 1 - action: 0 - price: 2343 - quantity: 1234 - participantID: 2_LOL2 -} -PreProcessorResult { - symbol: APL - side: 1 - action: 0 - price: 451 - quantity: 1341 - participantID: 3_LOL3 -} -OrderBookResult { - side: 0 - symbol: AMZ - price: 34 - quantity: 123 - orderListSize: 2 -} -OrderBookResult { - side: 1 - symbol: AMZ - price: 33 - quantity: 1234 - orderListSize: 6 -} -OrderBookResult { - side: 0 - symbol: TSL - price: 576 - quantity: 23 - orderListSize: 765 -} -OrderBookResult { - side: 0 - symbol: TSL - price: 1324 - quantity: 234 - orderListSize: 6 -} -OrderBookResult { - side: 1 - symbol: ZXY - price: 12 - quantity: 12 - orderListSize: 12 -} -5 -TradeResult { - symbol: APL - price: 69 - quantity: 12 - buyerID: 1_JGJI - sellerID: 12_JUHM -} -TradeResult { - symbol: TJP - price: 12 - quantity: 234 - buyerID: 24_UKBF - sellerID: 45_KOBV -}