|
Slick OrderBook 1.1.0
High-Performance C++23 OrderBook Library
|
Level 2 OrderBook - aggregated price levels. More...
#include <orderbook_l2.hpp>
Public Member Functions | |
| OrderBookL2 (SymbolId symbol, std::size_t initial_capacity=32) | |
| Constructor. | |
| virtual | ~OrderBookL2 ()=default |
| Destructor. | |
| OrderBookL2 (const OrderBookL2 &)=delete | |
| OrderBookL2 & | operator= (const OrderBookL2 &)=delete |
| OrderBookL2 (OrderBookL2 &&) noexcept | |
| OrderBookL2 & | operator= (OrderBookL2 &&) noexcept |
| SymbolId | symbol () const noexcept |
| Get symbol ID. | |
| void | updateLevel (Side side, Price price, Quantity quantity, Timestamp timestamp, uint64_t seq_num=0, bool is_last_in_batch=true) |
| Add or update a price level If quantity is 0, the level is deleted. | |
| bool | deleteLevel (Side side, Price price, Timestamp timestamp=0) |
| Delete a price level Does not emit a PriceLevelUpdate, but refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed) | |
| void | clearSide (Side side, Timestamp timestamp=0) |
| Clear all levels on one side Refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed) | |
| void | clear (Timestamp timestamp=0) |
| Clear all levels (both sides) Refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed) | |
| const detail::PriceLevelL2 * | getBestBid () const noexcept |
| Get best bid (highest buy price) | |
| const detail::PriceLevelL2 * | getBestAsk () const noexcept |
| Get best ask (lowest sell price) | |
| TopOfBook | getTopOfBook () const noexcept |
| Get top-of-book snapshot. | |
| std::vector< detail::PriceLevelL2 > | getLevels (Side side, std::size_t depth=0) const |
| Get price levels for a side. | |
| const detail::PriceLevelL2 * | getLevel (Side side, Price price) const noexcept |
| Get a specific price level by price. | |
| const detail::PriceLevelL2 * | getLevelByIndex (Side side, uint16_t index) const noexcept |
| Get a price level by index (0 = best) | |
| std::size_t | levelCount (Side side) const noexcept |
| Get number of levels 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 | 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 Useful for replaying full book state to a new observer Calls onSnapshotBegin(), followed by onPriceLevelUpdate() for each level, then onSnapshotEnd() | |
Protected Member Functions | |
| 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< detail::LevelContainer, SideCount > | sides_ |
| ObserverManager | observers_ |
| TopOfBook | cached_tob_ |
| detail::PriceLevelL2 | cached_best_bid_ |
| detail::PriceLevelL2 | cached_best_ask_ |
| std::atomic< uint64_t > | tob_seq_ |
| uint64_t | last_seq_num_ |
| uint16_t | change_starting_index_ = INVALID_INDEX |
Level 2 OrderBook - aggregated price levels.
Maintains aggregated order book state with price levels and total quantities. Does not track individual orders.
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 41 of file orderbook_l2.hpp.
|
inlineexplicit |
Constructor.
| symbol | Symbol identifier |
| initial_capacity | Initial capacity for price levels per side |
Definition at line 12 of file orderbook_l2_impl.hpp.
References slick::orderbook::Buy, cached_tob_, slick::orderbook::Sell, sides_, slick::orderbook::TopOfBook::symbol, and symbol_.
|
virtualdefault |
Destructor.
|
delete |
|
inlinenoexcept |
Definition at line 26 of file orderbook_l2_impl.hpp.
|
inline |
Observer management.
Definition at line 138 of file orderbook_l2.hpp.
|
inline |
Clear all levels (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 142 of file orderbook_l2_impl.hpp.
References slick::orderbook::Buy, notifyTopOfBookIfChanged(), slick::orderbook::Sell, and sides_.
|
inlinenoexcept |
Definition at line 146 of file orderbook_l2.hpp.
Clear all levels on one side 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 136 of file orderbook_l2_impl.hpp.
References notifyTopOfBookIfChanged(), slick::orderbook::SideCount, sides_, and SLICK_ASSERT.
|
inline |
Delete a price level Does not emit a PriceLevelUpdate, but refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed)
| side | Buy or Sell |
| price | Price level to delete |
| timestamp | Timestamp for the top-of-book update (0 = unknown) |
Definition at line 126 of file orderbook_l2_impl.hpp.
References notifyTopOfBookIfChanged(), slick::orderbook::SideCount, sides_, and SLICK_ASSERT.
|
inline |
Emit complete orderbook snapshot to observers Useful for replaying full book state to a new observer Calls onSnapshotBegin(), followed by onPriceLevelUpdate() for each level, then onSnapshotEnd()
| timestamp | Snapshot timestamp |
Definition at line 294 of file orderbook_l2_impl.hpp.
References slick::orderbook::Buy, last_seq_num_, slick::orderbook::ObserverManager::notifyPriceLevelUpdate(), slick::orderbook::ObserverManager::notifySnapshotBegin(), slick::orderbook::ObserverManager::notifySnapshotEnd(), observers_, slick::orderbook::PriceLevelUpdate::price, slick::orderbook::PriceChanged, slick::orderbook::QuantityChanged, slick::orderbook::Sell, sides_, and symbol_.
|
inlinenoexcept |
Get best ask (lowest sell price)
Definition at line 170 of file orderbook_l2_impl.hpp.
References slick::orderbook::TopOfBook::best_ask, cached_best_ask_, cached_tob_, and tob_seq_.
|
inlinenoexcept |
Get best bid (highest buy price)
Definition at line 148 of file orderbook_l2_impl.hpp.
References slick::orderbook::TopOfBook::best_bid, cached_best_bid_, cached_tob_, and tob_seq_.
|
inlinenoexcept |
Get last processed sequence number.
Definition at line 156 of file orderbook_l2.hpp.
|
inlinenoexcept |
Get a specific price level by price.
| side | Buy or Sell |
| price | Price to look up |
Definition at line 215 of file orderbook_l2_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inlinenoexcept |
Get a price level by index (0 = best)
| side | Buy or Sell |
| index | Level index (0-based, 0 = best bid/ask) |
Definition at line 220 of file orderbook_l2_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inline |
Get price levels for a side.
| side | Buy or Sell |
| depth | Maximum number of levels (0 = all) |
Definition at line 210 of file orderbook_l2_impl.hpp.
References slick::orderbook::SideCount, sides_, and SLICK_ASSERT.
|
inlinenoexcept |
Get top-of-book snapshot.
Definition at line 192 of file orderbook_l2_impl.hpp.
References cached_tob_, and tob_seq_.
|
inlinenoexcept |
Check if book is empty (both sides)
Definition at line 239 of file orderbook_l2_impl.hpp.
References slick::orderbook::Buy, slick::orderbook::Sell, and sides_.
|
inlinenoexcept |
Check if a side is empty.
| side | Buy or Sell |
Definition at line 234 of file orderbook_l2_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inlinenoexcept |
Get number of levels on a side.
| side | Buy or Sell |
Definition at line 229 of file orderbook_l2_impl.hpp.
References slick::orderbook::SideCount, and SLICK_ASSERT.
|
inlineprotected |
Notify observers of top-of-book update if best changed Updates cached_tob_ after notification.
| timestamp | Update timestamp |
Definition at line 243 of file orderbook_l2_impl.hpp.
References slick::orderbook::TopOfBook::ask_quantity, slick::orderbook::TopOfBook::best_ask, slick::orderbook::TopOfBook::best_bid, slick::orderbook::TopOfBook::bid_quantity, slick::orderbook::Buy, cached_best_ask_, cached_best_bid_, cached_tob_, slick::orderbook::TopOfBook::change_flags, slick::orderbook::ObserverManager::notifyTopOfBookUpdate(), observers_, slick::orderbook::PriceChanged, slick::orderbook::QuantityChanged, slick::orderbook::Sell, sides_, slick::orderbook::TopOfBook::timestamp, and tob_seq_.
Referenced by clear(), clearSide(), deleteLevel(), and updateLevel().
|
inlinenoexcept |
Definition at line 150 of file orderbook_l2.hpp.
|
delete |
|
inlinenoexcept |
Definition at line 38 of file orderbook_l2_impl.hpp.
References symbol_.
|
inline |
Definition at line 142 of file orderbook_l2.hpp.
|
inlinenoexcept |
Get symbol ID.
Definition at line 58 of file orderbook_l2.hpp.
|
inline |
Add or update a price level If quantity is 0, the level is deleted.
| side | Buy or Sell |
| price | Price level |
| quantity | Total quantity at this level (0 = delete) |
| timestamp | Update 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 52 of file orderbook_l2_impl.hpp.
References change_starting_index_, slick::orderbook::INVALID_INDEX, last_seq_num_, slick::orderbook::LastInBatch, slick::orderbook::ObserverManager::notifyPriceLevelUpdate(), notifyTopOfBookIfChanged(), observers_, slick::orderbook::PriceChanged, slick::orderbook::QuantityChanged, slick::orderbook::SideCount, sides_, SLICK_ASSERT, and symbol_.
|
protected |
Definition at line 177 of file orderbook_l2.hpp.
Referenced by getBestAsk(), and notifyTopOfBookIfChanged().
|
protected |
Definition at line 176 of file orderbook_l2.hpp.
Referenced by getBestBid(), and notifyTopOfBookIfChanged().
|
protected |
Definition at line 175 of file orderbook_l2.hpp.
Referenced by OrderBookL2(), getBestAsk(), getBestBid(), getTopOfBook(), and notifyTopOfBookIfChanged().
|
protected |
Definition at line 180 of file orderbook_l2.hpp.
Referenced by updateLevel().
|
protected |
Definition at line 179 of file orderbook_l2.hpp.
Referenced by emitSnapshot(), and updateLevel().
|
protected |
Definition at line 174 of file orderbook_l2.hpp.
Referenced by emitSnapshot(), notifyTopOfBookIfChanged(), and updateLevel().
|
protected |
Definition at line 173 of file orderbook_l2.hpp.
Referenced by OrderBookL2(), clear(), clearSide(), deleteLevel(), emitSnapshot(), getLevels(), isEmpty(), notifyTopOfBookIfChanged(), and updateLevel().
|
protected |
Definition at line 172 of file orderbook_l2.hpp.
Referenced by OrderBookL2(), emitSnapshot(), operator=(), and updateLevel().
|
protected |
Definition at line 178 of file orderbook_l2.hpp.
Referenced by getBestAsk(), getBestBid(), getTopOfBook(), and notifyTopOfBookIfChanged().