Orderbook Update 1: Core Data Structures
Introduction
When implementing an orderbook system, the foundation lies in choosing the right data structures and abstractions. In this post, I'll walk through the key design decisions in building the core components of our C++ orderbook implementation.
Type Definitions and Performance Considerations
The first critical decision was selecting appropriate types for our core primitives:
using Price = std::int32_t;
using Quantity = std::uint32_t;
using OrderId = std::uint64_t;
These choices reflect careful consideration of both performance and practical requirements. Using fixed-width integer types ensures consistent behavior across platforms while providing sufficient range for practical trading scenarios. The decision to use signed integers for prices allows for negative values in certain market conditions, while quantities and order IDs are naturally unsigned.
Managing Price Levels
Price level management is crucial for orderbook performance. Our implementation uses a two-tiered approach:
struct LevelInfo {
Price price_;
Quantity quantity_;
};
using LevelInfos = std::vector<LevelInfo>;
Order Management
The Order class forms the cornerstone of our implementation:
class Order {
public:
Order(OrderType orderType, OrderId orderId, Side side,
Price price, Quantity quantity)
: orderType_{ orderType }
, orderId_{ orderId }
, side_{ side }
, price_{ price }
, initialQuantity_{ quantity }
, remainingQuantity_{ quantity }
{ }
// ... getters and utility methods ...
};
Performance Implications
Our design choices prioritize performance in several ways:
What's Next?
In the upcoming posts, I’ll dive into the details of:
1. Implementing the core functionality of the Order book.
2. Adding more complex order types and matching algorithms.
3. Optimizing for performance and low latency.
4. Implementing a simple interface for interacting with the Order book.
Thank you for following along on this journey. Stay tuned for more updates on the Order book.