|
Slick OrderBook 1.1.0
High-Performance C++23 OrderBook Library
|
Level 3 OrderBook - Individual order tracking with price-time priority. More...
#include <orderbook_l3.hpp>
Classes | |
| struct | PriceComparator |
Public Types | |
| using | PriceLevelMap = detail::FlatMap< Price, detail::PriceLevelL3, PriceComparator > |
Public Member Functions | |
| OrderBookL3 (SymbolId symbol, std::size_t interested_num_levels=10, std::size_t initial_order_capacity=1024, std::size_t initial_level_capacity=32) | |
| Constructor. | |
| virtual | ~OrderBookL3 () |
| Destructor. | |
| OrderBookL3 (const OrderBookL3 &)=delete | |
| OrderBookL3 & | operator= (const OrderBookL3 &)=delete |
| OrderBookL3 (OrderBookL3 &&) noexcept=default | |
| OrderBookL3 & | operator= (OrderBookL3 &&) noexcept=default |
| SymbolId | symbol () const noexcept |
| Get symbol ID. | |
| bool | addOrModifyOrder (OrderId order_id, Side side, Price price, Quantity quantity, Timestamp timestamp, uint64_t priority, uint64_t seq_num=0, bool is_last_in_batch=true) |
| Add a new order or modify existing order if OrderId already exists If the order already exists, modifies its price and/or quantity. | |
| bool | addOrder (OrderId order_id, Side side, Price price, Quantity quantity, Timestamp timestamp, uint64_t priority=0, uint64_t seq_num=0, bool is_last_in_batch=true) |
| Add a new order (strict - fails if order already exists) | |
| bool | modifyOrder (OrderId order_id, Price new_price, Quantity new_quantity, Timestamp new_timestamp, uint64_t new_priority=0, uint64_t seq_num=0, bool is_last_in_batch=true) |
| Modify an existing order's price and/or quantity Compares new values with old to determine what changed. | |
| bool | deleteOrder (OrderId order_id, Timestamp timestamp, uint64_t seq_num=0, bool is_last_in_batch=true) |
| Delete an order. | |
| virtual bool | executeOrder (OrderId order_id, Quantity executed_quantity, Timestamp timestamp, uint64_t seq_num=0, bool is_last_in_batch=true) |
| Execute (partially fill) an order. | |
| const detail::Order * | findOrder (OrderId order_id) const noexcept |
| Find order by OrderId. | |
| template<Side SIDE> | |
| const detail::PriceLevelL3 * | getBestLevel () const noexcept |
| Get the best price level for a given side. | |
| const detail::PriceLevelL3 * | getBestBid () const noexcept |
| Get best bid (highest buy price) | |
| const detail::PriceLevelL3 * | getBestAsk () const noexcept |
| Get best ask (lowest sell price) | |
| TopOfBook | getTopOfBook () const noexcept |
| Get top-of-book snapshot (aggregated L2 view) | |
| const PriceLevelMap & | getLevelsL3 (Side side) const noexcept |
| Get L3 price levels for a side (zero-copy access to full order data) Returns reference to flat_map, iterate with range-for. | |
| std::vector< detail::PriceLevelL2 > | getLevelsL2 (Side side, std::size_t depth=0) const |
| Get aggregated L2 price levels for a side Converts L3 data to L2 by aggregating quantities at each price. | |
| std::pair< const detail::PriceLevelL3 *, uint16_t > | getLevel (Side side, Price price) const noexcept |
| Get L3 price level by price Access the orders at this price via level->orders (IntrusiveList) | |
| std::pair< detail::PriceLevelL3 *, uint16_t > | getLevel (Side side, Price price) noexcept |
| const detail::PriceLevelL3 * | getLevelByIndex (Side side, uint16_t index) const noexcept |
| Get L3 price level by index (0 = best) | |
| std::size_t | levelCount (Side side) const noexcept |
| Get number of price levels on a side. | |
| std::size_t | orderCount () const noexcept |
| Get total number of orders. | |
| std::size_t | orderCount (Side side) const noexcept |
| Get number of orders on a side. | |
| bool | isEmpty (Side side) const noexcept |
| Check if a side is empty. | |
| bool | isEmpty () const noexcept |
| Check if book is empty (both sides) | |
| void | clearSide (Side side, Timestamp timestamp=0) |
| Clear all orders on one side Does not emit order/level updates, but refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed) | |
| void | clear (Timestamp timestamp=0) |
| Clear all orders (both sides) Refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed) | |
| void | addObserver (std::shared_ptr< IOrderBookObserver > observer) |
| Observer management. | |
| bool | removeObserver (const std::shared_ptr< IOrderBookObserver > &observer) |
| void | clearObservers () noexcept |
| std::size_t | observerCount () const noexcept |
| uint64_t | getLastSeqNum () const noexcept |
| Get last processed sequence number. | |
| void | emitSnapshot (Timestamp timestamp) |
| Emit complete orderbook snapshot to observers (L3 MBO) Useful for replaying full book state to a new observer Calls onSnapshotBegin(), followed by onOrderUpdate() for each order, then onSnapshotEnd() | |
Protected Member Functions | |
| std::tuple< detail::PriceLevelL3 *, uint16_t, bool > | getOrCreateLevel (Side side, Price price) |
| Get or create price level, returns pointer, index and if new level created. | |
| bool | removeLevelIfEmpty (Side side, Price price) noexcept |
| Remove price level if empty. | |
| void | releaseSide (Side side) noexcept |
| Free all orders and levels on one side without any notification. | |
| void | notifyOrderUpdate (const detail::Order *order, Quantity old_quantity, Price old_price, Timestamp timestamp, uint16_t level_index, uint8_t change_flags, uint64_t seq_num) const |
| Notify observers of order update with level index and change flags. | |
| void | notifyOrderDelete (const detail::Order *order, Timestamp timestamp, uint16_t level_index, uint8_t change_flags, uint64_t seq_num) const |
| Notify observers of order delete with level index. | |
| uint16_t | calculateLevelIndex (Side side, Price price) const noexcept |
| Calculate price level index for a given side and price. | |
| void | notifyTrade (OrderId passive_order_id, OrderId aggressive_order_id, Side aggressor_side, Price price, Quantity quantity, Timestamp timestamp) const |
| Notify observers of trade. | |
| void | notifyPriceLevelUpdate (Timestamp timestamp, Side side, Price price, Quantity total_quantity, size_t order_count, uint16_t level_index, uint8_t change_flags, uint64_t seq_num) const |
| Notify observers of price level update (L2 aggregated view) | |
| void | notifyTopOfBookIfChanged (Timestamp timestamp) |
| Notify observers of top-of-book update if best changed Updates cached_tob_ after notification. | |
Protected Attributes | |
| SymbolId | symbol_ |
| std::array< PriceLevelMap, SideCount > | levels_ |
| detail::OrderMap | order_map_ |
| detail::ObjectPool< detail::Order > | order_pool_ |
| ObserverManager | observers_ |
| TopOfBook | cached_tob_ |
| uint64_t | last_seq_num_ |
| uint16_t | change_starting_index_ = INVALID_INDEX |
| std::size_t | interested_num_levels_ |
Level 3 OrderBook - Individual order tracking with price-time priority.
Maintains full order-by-order state with individual order visibility. Provides automatic aggregated L2 view.
Features:
Thread Safety: Single writer, multiple readers (lock-free reads planned)
Memory Layout: Cache-aligned to 64 bytes for optimal performance
Usage:
Definition at line 62 of file orderbook_l3.hpp.
| using slick::orderbook::OrderBookL3::PriceLevelMap = detail::FlatMap<Price, detail::PriceLevelL3, PriceComparator> |
Definition at line 81 of file orderbook_l3.hpp.
|
inlineexplicit |
Constructor.
| symbol | Symbol identifier |
| interested_num_levels | The top N levels to track for observer notifications (0 = all levels) |
| initial_order_capacity | Initial capacity for order pool |
| initial_level_capacity | Initial capacity for price levels per side |
Definition at line 14 of file orderbook_l3_impl.hpp.
References slick::orderbook::Buy, cached_tob_, levels_, slick::orderbook::Sell, slick::orderbook::TopOfBook::symbol, and symbol_.
|
inlinevirtual |
Destructor.
Definition at line 37 of file orderbook_l3_impl.hpp.
References slick::orderbook::Buy, releaseSide(), and slick::orderbook::Sell.
|
delete |
|
defaultnoexcept |
|
inline |
Observer management.
Definition at line 266 of file orderbook_l3.hpp.
|
inline |
Add a new order (strict - fails if order already exists)
| order_id | Unique order identifier |
| side | Buy or Sell |
| price | Order price |
| quantity | Order quantity |
| timestamp | Order timestamp |
| priority | Priority for sorting (defaults to timestamp) |
| seq_num | Exchange sequence number (0 = no tracking, default) Out-of-order updates (seq_num < last_seq_num) are rejected |
| is_last_in_batch | Set to true if this is the last update in an external batch (enables batching of TopOfBook updates) |
Definition at line 124 of file orderbook_l3_impl.hpp.
References addOrModifyOrder(), slick::orderbook::detail::OrderMap::contains(), last_seq_num_, and order_map_.
|
inline |
Add a new order or modify existing order if OrderId already exists If the order already exists, modifies its price and/or quantity.
| order_id | Unique order identifier |
| side | Buy or Sell (must match existing order's side if it exists) |
| price | Order price |
| quantity | Order quantity |
| timestamp | Order timestamp |
| priority | Priority for sorting (used only when adding new order) |
| seq_num | Exchange sequence number (0 = no tracking, default) Out-of-order updates (seq_num < last_seq_num) are rejected |
| is_last_in_batch | Set to true if this is the last update in an external batch (enables batching of TopOfBook updates) |
Definition at line 43 of file orderbook_l3_impl.hpp.
References change_starting_index_, deleteOrder(), slick::orderbook::detail::OrderMap::find(), getOrCreateLevel(), slick::orderbook::detail::OrderMap::insert(), slick::orderbook::INVALID_INDEX, last_seq_num_, slick::orderbook::LastInBatch, modifyOrder(), notifyOrderUpdate(), notifyPriceLevelUpdate(), notifyTopOfBookIfChanged(), order_map_, order_pool_, slick::orderbook::detail::Order::price, slick::orderbook::PriceChanged, slick::orderbook::detail::Order::priority, slick::orderbook::detail::Order::quantity, slick::orderbook::QuantityChanged, slick::orderbook::detail::Order::side, slick::orderbook::SideCount, SLICK_ASSERT, and SLICK_UNLIKELY.
Referenced by addOrder().
|
inlineprotectednoexcept |
Calculate price level index for a given side and price.
Definition at line 579 of file orderbook_l3_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inline |
Clear all orders (both sides) Refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed)
| timestamp | Timestamp for the top-of-book update (0 = unknown) |
Definition at line 523 of file orderbook_l3_impl.hpp.
References slick::orderbook::Buy, notifyTopOfBookIfChanged(), releaseSide(), and slick::orderbook::Sell.
|
inlinenoexcept |
Definition at line 274 of file orderbook_l3.hpp.
Clear all orders on one side Does not emit order/level updates, but refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed)
| side | Buy or Sell |
| timestamp | Timestamp for the top-of-book update (0 = unknown) |
Definition at line 516 of file orderbook_l3_impl.hpp.
References notifyTopOfBookIfChanged(), releaseSide(), slick::orderbook::SideCount, and SLICK_ASSERT.
|
inline |
Delete an order.
| order_id | Order identifier |
| timestamp | Timestamp of the deletion (required) |
| seq_num | Exchange sequence number (0 = no tracking, default) Out-of-order updates (seq_num < last_seq_num) are rejected |
| is_last_in_batch | Set to true if this is the last update in an external batch (enables batching of TopOfBook updates) |
Definition at line 292 of file orderbook_l3_impl.hpp.
References change_starting_index_, slick::orderbook::detail::OrderMap::erase(), slick::orderbook::detail::OrderMap::find(), getLevel(), slick::orderbook::INVALID_INDEX, last_seq_num_, slick::orderbook::LastInBatch, notifyOrderDelete(), notifyPriceLevelUpdate(), notifyTopOfBookIfChanged(), order_map_, order_pool_, slick::orderbook::detail::Order::price, slick::orderbook::PriceChanged, slick::orderbook::QuantityChanged, removeLevelIfEmpty(), slick::orderbook::detail::Order::side, and slick::orderbook::detail::Order::timestamp.
Referenced by addOrModifyOrder(), executeOrder(), and modifyOrder().
|
inline |
Emit complete orderbook snapshot to observers (L3 MBO) Useful for replaying full book state to a new observer Calls onSnapshotBegin(), followed by onOrderUpdate() for each order, then onSnapshotEnd()
| timestamp | Snapshot timestamp |
Definition at line 722 of file orderbook_l3_impl.hpp.
References slick::orderbook::Buy, last_seq_num_, levels_, slick::orderbook::ObserverManager::notifyOrderUpdate(), slick::orderbook::ObserverManager::notifySnapshotBegin(), slick::orderbook::ObserverManager::notifySnapshotEnd(), observers_, slick::orderbook::OrderUpdate::order_id, slick::orderbook::PriceChanged, slick::orderbook::QuantityChanged, slick::orderbook::Sell, and symbol_.
|
inlinevirtual |
Execute (partially fill) an order.
| order_id | Order identifier |
| executed_quantity | Quantity executed |
| timestamp | Timestamp of the execution (required) |
| seq_num | Exchange sequence number (0 = no tracking, default) Out-of-order updates (seq_num < last_seq_num) are rejected |
| is_last_in_batch | Set to true if this is the last update in an external batch (enables batching of TopOfBook updates) |
Definition at line 366 of file orderbook_l3_impl.hpp.
References deleteOrder(), slick::orderbook::detail::OrderMap::find(), last_seq_num_, modifyOrder(), order_map_, slick::orderbook::detail::Order::price, slick::orderbook::detail::Order::priority, slick::orderbook::detail::Order::quantity, SLICK_ASSERT, and SLICK_UNLIKELY.
|
inlinenoexcept |
Find order by OrderId.
| order_id | Order identifier |
Definition at line 399 of file orderbook_l3_impl.hpp.
|
inlinenoexcept |
Get best ask (lowest sell price)
Definition at line 413 of file orderbook_l3_impl.hpp.
References levels_, and slick::orderbook::Sell.
Referenced by notifyTopOfBookIfChanged().
|
inlinenoexcept |
Get best bid (highest buy price)
Definition at line 403 of file orderbook_l3_impl.hpp.
References slick::orderbook::Buy, and levels_.
Referenced by notifyTopOfBookIfChanged().
|
noexcept |
Get the best price level for a given side.
| SIDE |
|
inlinenoexcept |
Get last processed sequence number.
Definition at line 284 of file orderbook_l3.hpp.
|
inlinenoexcept |
Get L3 price level by price Access the orders at this price via level->orders (IntrusiveList)
| side | Buy or Sell |
| price | Price level |
Definition at line 460 of file orderbook_l3_impl.hpp.
References slick::orderbook::INVALID_INDEX, slick::orderbook::SideCount, and SLICK_ASSERT.
Referenced by deleteOrder(), and modifyOrder().
|
inlinenoexcept |
Definition at line 471 of file orderbook_l3_impl.hpp.
References slick::orderbook::INVALID_INDEX, slick::orderbook::SideCount, and SLICK_ASSERT.
|
inlinenoexcept |
Get L3 price level by index (0 = best)
| side | Buy or Sell |
| index | Level index (0-based, 0 = best bid/ask) |
Definition at line 482 of file orderbook_l3_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inline |
Get aggregated L2 price levels for a side Converts L3 data to L2 by aggregating quantities at each price.
| side | Buy or Sell |
| depth | Maximum number of levels (0 = all) |
Definition at line 426 of file orderbook_l3_impl.hpp.
References slick::orderbook::Buy, levels_, slick::orderbook::SideCount, and SLICK_ASSERT.
|
inlinenoexcept |
Get L3 price levels for a side (zero-copy access to full order data) Returns reference to flat_map, iterate with range-for.
| side | Buy or Sell |
Definition at line 203 of file orderbook_l3.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inlineprotected |
Get or create price level, returns pointer, index and if new level created.
Definition at line 548 of file orderbook_l3_impl.hpp.
References levels_, slick::orderbook::SideCount, and SLICK_ASSERT.
Referenced by addOrModifyOrder(), and modifyOrder().
|
inlinenoexcept |
Get top-of-book snapshot (aggregated L2 view)
Definition at line 422 of file orderbook_l3_impl.hpp.
References cached_tob_.
|
inlinenoexcept |
Check if book is empty (both sides)
Definition at line 512 of file orderbook_l3_impl.hpp.
References slick::orderbook::Buy, levels_, and slick::orderbook::Sell.
|
inlinenoexcept |
Check if a side is empty.
| side | Buy or Sell |
Definition at line 507 of file orderbook_l3_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inlinenoexcept |
Get number of price levels on a side.
| side | Buy or Sell |
Definition at line 493 of file orderbook_l3_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inline |
Modify an existing order's price and/or quantity Compares new values with old to determine what changed.
| order_id | Order identifier |
| new_price | New price |
| new_quantity | New quantity (0 = delete) |
| new_timestamp | Timestamp of the modification (required) |
| new_priority | New priority (default: 0 = use new_timestamp as priority for FIFO ordering) Use non-zero to explicitly set queue position priority |
| seq_num | Exchange sequence number (0 = no tracking, default) Out-of-order updates (seq_num < last_seq_num) are rejected |
| is_last_in_batch | Set to true if this is the last update in an external batch (enables batching of TopOfBook updates) |
Definition at line 144 of file orderbook_l3_impl.hpp.
References change_starting_index_, deleteOrder(), slick::orderbook::detail::OrderMap::find(), getLevel(), getOrCreateLevel(), slick::orderbook::INVALID_INDEX, last_seq_num_, slick::orderbook::LastInBatch, notifyOrderUpdate(), notifyPriceLevelUpdate(), notifyTopOfBookIfChanged(), order_map_, slick::orderbook::detail::Order::price, slick::orderbook::PriceChanged, slick::orderbook::detail::Order::priority, slick::orderbook::detail::Order::quantity, slick::orderbook::QuantityChanged, removeLevelIfEmpty(), slick::orderbook::detail::Order::side, SLICK_UNLIKELY, and slick::orderbook::detail::Order::timestamp.
Referenced by addOrModifyOrder(), and executeOrder().
|
inlineprotected |
Notify observers of order delete with level index.
Definition at line 620 of file orderbook_l3_impl.hpp.
References slick::orderbook::ObserverManager::notifyOrderUpdate(), observers_, slick::orderbook::detail::Order::order_id, slick::orderbook::detail::Order::price, slick::orderbook::detail::Order::priority, slick::orderbook::detail::Order::quantity, slick::orderbook::detail::Order::side, and symbol_.
Referenced by deleteOrder().
|
inlineprotected |
Notify observers of order update with level index and change flags.
Definition at line 595 of file orderbook_l3_impl.hpp.
References slick::orderbook::ObserverManager::notifyOrderUpdate(), observers_, slick::orderbook::detail::Order::order_id, slick::orderbook::detail::Order::price, slick::orderbook::detail::Order::priority, slick::orderbook::detail::Order::quantity, slick::orderbook::detail::Order::side, and symbol_.
Referenced by addOrModifyOrder(), and modifyOrder().
|
inlineprotected |
Notify observers of price level update (L2 aggregated view)
Definition at line 662 of file orderbook_l3_impl.hpp.
References interested_num_levels_, slick::orderbook::ObserverManager::notifyPriceLevelUpdate(), observers_, and symbol_.
Referenced by addOrModifyOrder(), deleteOrder(), and modifyOrder().
|
inlineprotected |
Notify observers of top-of-book update if best changed Updates cached_tob_ after notification.
| timestamp | Update timestamp |
Definition at line 689 of file orderbook_l3_impl.hpp.
References slick::orderbook::TopOfBook::ask_quantity, slick::orderbook::TopOfBook::best_ask, slick::orderbook::TopOfBook::best_bid, slick::orderbook::TopOfBook::bid_quantity, cached_tob_, slick::orderbook::TopOfBook::change_flags, getBestAsk(), getBestBid(), slick::orderbook::ObserverManager::notifyTopOfBookUpdate(), observers_, slick::orderbook::PriceChanged, slick::orderbook::QuantityChanged, and slick::orderbook::TopOfBook::timestamp.
Referenced by addOrModifyOrder(), clear(), clearSide(), deleteOrder(), and modifyOrder().
|
inlineprotected |
Notify observers of trade.
Definition at line 643 of file orderbook_l3_impl.hpp.
References slick::orderbook::ObserverManager::notifyTrade(), observers_, and symbol_.
|
inlinenoexcept |
Definition at line 278 of file orderbook_l3.hpp.
|
delete |
|
defaultnoexcept |
|
inlinenoexcept |
Get total number of orders.
Definition at line 237 of file orderbook_l3.hpp.
|
inlinenoexcept |
Get number of orders on a side.
| side | Buy or Sell |
Definition at line 498 of file orderbook_l3_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inlineprotectednoexcept |
Free all orders and levels on one side without any notification.
Definition at line 529 of file orderbook_l3_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
Referenced by ~OrderBookL3(), clear(), and clearSide().
|
inlineprotectednoexcept |
Remove price level if empty.
Definition at line 566 of file orderbook_l3_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
Referenced by deleteOrder(), and modifyOrder().
|
inline |
Definition at line 270 of file orderbook_l3.hpp.
|
inlinenoexcept |
Get symbol ID.
Definition at line 103 of file orderbook_l3.hpp.
|
protected |
Definition at line 332 of file orderbook_l3.hpp.
Referenced by OrderBookL3(), getTopOfBook(), and notifyTopOfBookIfChanged().
|
protected |
Definition at line 334 of file orderbook_l3.hpp.
Referenced by addOrModifyOrder(), deleteOrder(), and modifyOrder().
|
protected |
Definition at line 335 of file orderbook_l3.hpp.
Referenced by notifyPriceLevelUpdate().
|
protected |
Definition at line 333 of file orderbook_l3.hpp.
Referenced by addOrder(), addOrModifyOrder(), deleteOrder(), emitSnapshot(), executeOrder(), and modifyOrder().
|
protected |
Definition at line 328 of file orderbook_l3.hpp.
Referenced by OrderBookL3(), emitSnapshot(), getBestAsk(), getBestBid(), getLevelsL2(), getOrCreateLevel(), and isEmpty().
|
protected |
Definition at line 331 of file orderbook_l3.hpp.
Referenced by emitSnapshot(), notifyOrderDelete(), notifyOrderUpdate(), notifyPriceLevelUpdate(), notifyTopOfBookIfChanged(), and notifyTrade().
|
protected |
Definition at line 329 of file orderbook_l3.hpp.
Referenced by addOrder(), addOrModifyOrder(), deleteOrder(), executeOrder(), and modifyOrder().
|
protected |
Definition at line 330 of file orderbook_l3.hpp.
Referenced by addOrModifyOrder(), and deleteOrder().
|
protected |
Definition at line 327 of file orderbook_l3.hpp.
Referenced by OrderBookL3(), emitSnapshot(), notifyOrderDelete(), notifyOrderUpdate(), notifyPriceLevelUpdate(), and notifyTrade().