Slick OrderBook 1.1.0
High-Performance C++23 OrderBook Library
Loading...
Searching...
No Matches
slick::orderbook::OrderBookL2 Class Reference

Level 2 OrderBook - aggregated price levels. More...

#include <orderbook_l2.hpp>

Collaboration diagram for slick::orderbook::OrderBookL2:

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
 

Detailed Description

Level 2 OrderBook - aggregated price levels.

Maintains aggregated order book state with price levels and total quantities. Does not track individual orders.

Features:

  • O(log n) updates (binary search in sorted vector)
  • O(1) best bid/ask queries
  • Cache-friendly memory layout
  • Observer pattern for notifications

Thread Safety: Single writer, multiple readers (lock-free reads planned)

Memory Layout: Cache-aligned to 64 bytes for optimal performance

Usage:

OrderBookL2 book(symbol_id);
book.updateLevel(Side::Buy, 10000, 100, timestamp);
auto best_bid = book.getBestBid();
Level 2 OrderBook - aggregated price levels.

Definition at line 41 of file orderbook_l2.hpp.

Constructor & Destructor Documentation

◆ OrderBookL2() [1/3]

slick::orderbook::OrderBookL2::OrderBookL2 ( SymbolId  symbol,
std::size_t  initial_capacity = 32 
)
inlineexplicit

Constructor.

Parameters
symbolSymbol identifier
initial_capacityInitial 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_.

◆ ~OrderBookL2()

virtual slick::orderbook::OrderBookL2::~OrderBookL2 ( )
virtualdefault

Destructor.

◆ OrderBookL2() [2/3]

slick::orderbook::OrderBookL2::OrderBookL2 ( const OrderBookL2 &  )
delete

◆ OrderBookL2() [3/3]

slick::orderbook::OrderBookL2::OrderBookL2 ( OrderBookL2 &&  other)
inlinenoexcept

Definition at line 26 of file orderbook_l2_impl.hpp.

Member Function Documentation

◆ addObserver()

void slick::orderbook::OrderBookL2::addObserver ( std::shared_ptr< IOrderBookObserver >  observer)
inline

Observer management.

Definition at line 138 of file orderbook_l2.hpp.

◆ clear()

void slick::orderbook::OrderBookL2::clear ( Timestamp  timestamp = 0)
inline

Clear all levels (both sides) Refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed)

Parameters
timestampTimestamp 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_.

◆ clearObservers()

void slick::orderbook::OrderBookL2::clearObservers ( )
inlinenoexcept

Definition at line 146 of file orderbook_l2.hpp.

◆ clearSide()

void slick::orderbook::OrderBookL2::clearSide ( Side  side,
Timestamp  timestamp = 0 
)
inline

Clear all levels on one side Refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed)

Parameters
sideBuy or Sell
timestampTimestamp 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.

◆ deleteLevel()

bool slick::orderbook::OrderBookL2::deleteLevel ( Side  side,
Price  price,
Timestamp  timestamp = 0 
)
inline

Delete a price level Does not emit a PriceLevelUpdate, but refreshes the cached top-of-book (and notifies onTopOfBookUpdate if it changed)

Parameters
sideBuy or Sell
pricePrice level to delete
timestampTimestamp for the top-of-book update (0 = unknown)
Returns
true if level was found and deleted

Definition at line 126 of file orderbook_l2_impl.hpp.

References notifyTopOfBookIfChanged(), slick::orderbook::SideCount, sides_, and SLICK_ASSERT.

◆ emitSnapshot()

void slick::orderbook::OrderBookL2::emitSnapshot ( Timestamp  timestamp)
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()

Parameters
timestampSnapshot 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_.

◆ getBestAsk()

const detail::PriceLevelL2 * slick::orderbook::OrderBookL2::getBestAsk ( ) const
inlinenoexcept

Get best ask (lowest sell price)

Returns
Pointer to best ask level, or nullptr if no asks

Definition at line 170 of file orderbook_l2_impl.hpp.

References slick::orderbook::TopOfBook::best_ask, cached_best_ask_, cached_tob_, and tob_seq_.

◆ getBestBid()

const detail::PriceLevelL2 * slick::orderbook::OrderBookL2::getBestBid ( ) const
inlinenoexcept

Get best bid (highest buy price)

Returns
Pointer to best bid level, or nullptr if no bids

Definition at line 148 of file orderbook_l2_impl.hpp.

References slick::orderbook::TopOfBook::best_bid, cached_best_bid_, cached_tob_, and tob_seq_.

◆ getLastSeqNum()

uint64_t slick::orderbook::OrderBookL2::getLastSeqNum ( ) const
inlinenoexcept

Get last processed sequence number.

Returns
Last sequence number (0 if not tracking)

Definition at line 156 of file orderbook_l2.hpp.

◆ getLevel()

const detail::PriceLevelL2 * slick::orderbook::OrderBookL2::getLevel ( Side  side,
Price  price 
) const
inlinenoexcept

Get a specific price level by price.

Parameters
sideBuy or Sell
pricePrice to look up
Returns
Pointer to price level, or nullptr if not found

Definition at line 215 of file orderbook_l2_impl.hpp.

References slick::orderbook::SideCount, and SLICK_ASSERT.

◆ getLevelByIndex()

const detail::PriceLevelL2 * slick::orderbook::OrderBookL2::getLevelByIndex ( Side  side,
uint16_t  index 
) const
inlinenoexcept

Get a price level by index (0 = best)

Parameters
sideBuy or Sell
indexLevel index (0-based, 0 = best bid/ask)
Returns
Pointer to price level, or nullptr if index out of range

Definition at line 220 of file orderbook_l2_impl.hpp.

References slick::orderbook::SideCount, and SLICK_ASSERT.

◆ getLevels()

std::vector< detail::PriceLevelL2 > slick::orderbook::OrderBookL2::getLevels ( Side  side,
std::size_t  depth = 0 
) const
inline

Get price levels for a side.

Parameters
sideBuy or Sell
depthMaximum number of levels (0 = all)
Returns
Vector of price levels

Definition at line 210 of file orderbook_l2_impl.hpp.

References slick::orderbook::SideCount, sides_, and SLICK_ASSERT.

◆ getTopOfBook()

TopOfBook slick::orderbook::OrderBookL2::getTopOfBook ( ) const
inlinenoexcept

Get top-of-book snapshot.

Returns
TopOfBook structure with best bid/ask

Definition at line 192 of file orderbook_l2_impl.hpp.

References cached_tob_, and tob_seq_.

◆ isEmpty() [1/2]

bool slick::orderbook::OrderBookL2::isEmpty ( ) const
inlinenoexcept

Check if book is empty (both sides)

Returns
true if both sides are empty

Definition at line 239 of file orderbook_l2_impl.hpp.

References slick::orderbook::Buy, slick::orderbook::Sell, and sides_.

◆ isEmpty() [2/2]

bool slick::orderbook::OrderBookL2::isEmpty ( Side  side) const
inlinenoexcept

Check if a side is empty.

Parameters
sideBuy or Sell
Returns
true if side has no levels

Definition at line 234 of file orderbook_l2_impl.hpp.

References slick::orderbook::SideCount, and SLICK_ASSERT.

◆ levelCount()

std::size_t slick::orderbook::OrderBookL2::levelCount ( Side  side) const
inlinenoexcept

Get number of levels on a side.

Parameters
sideBuy or Sell
Returns
Number of levels

Definition at line 229 of file orderbook_l2_impl.hpp.

References slick::orderbook::SideCount, and SLICK_ASSERT.

◆ notifyTopOfBookIfChanged()

◆ observerCount()

std::size_t slick::orderbook::OrderBookL2::observerCount ( ) const
inlinenoexcept

Definition at line 150 of file orderbook_l2.hpp.

◆ operator=() [1/2]

OrderBookL2 & slick::orderbook::OrderBookL2::operator= ( const OrderBookL2 &  )
delete

◆ operator=() [2/2]

OrderBookL2 & slick::orderbook::OrderBookL2::operator= ( OrderBookL2 &&  other)
inlinenoexcept

Definition at line 38 of file orderbook_l2_impl.hpp.

References symbol_.

◆ removeObserver()

bool slick::orderbook::OrderBookL2::removeObserver ( const std::shared_ptr< IOrderBookObserver > &  observer)
inline

Definition at line 142 of file orderbook_l2.hpp.

◆ symbol()

SymbolId slick::orderbook::OrderBookL2::symbol ( ) const
inlinenoexcept

Get symbol ID.

Definition at line 58 of file orderbook_l2.hpp.

◆ updateLevel()

void slick::orderbook::OrderBookL2::updateLevel ( Side  side,
Price  price,
Quantity  quantity,
Timestamp  timestamp,
uint64_t  seq_num = 0,
bool  is_last_in_batch = true 
)
inline

Add or update a price level If quantity is 0, the level is deleted.

Parameters
sideBuy or Sell
pricePrice level
quantityTotal quantity at this level (0 = delete)
timestampUpdate timestamp
seq_numExchange sequence number (0 = no tracking, default) Out-of-order updates (seq_num <= last_seq_num) are rejected
is_last_in_batchSet 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_.

Member Data Documentation

◆ cached_best_ask_

detail::PriceLevelL2 slick::orderbook::OrderBookL2::cached_best_ask_
protected

Definition at line 177 of file orderbook_l2.hpp.

Referenced by getBestAsk(), and notifyTopOfBookIfChanged().

◆ cached_best_bid_

detail::PriceLevelL2 slick::orderbook::OrderBookL2::cached_best_bid_
protected

Definition at line 176 of file orderbook_l2.hpp.

Referenced by getBestBid(), and notifyTopOfBookIfChanged().

◆ cached_tob_

TopOfBook slick::orderbook::OrderBookL2::cached_tob_
protected

◆ change_starting_index_

uint16_t slick::orderbook::OrderBookL2::change_starting_index_ = INVALID_INDEX
protected

Definition at line 180 of file orderbook_l2.hpp.

Referenced by updateLevel().

◆ last_seq_num_

uint64_t slick::orderbook::OrderBookL2::last_seq_num_
protected

Definition at line 179 of file orderbook_l2.hpp.

Referenced by emitSnapshot(), and updateLevel().

◆ observers_

ObserverManager slick::orderbook::OrderBookL2::observers_
protected

Definition at line 174 of file orderbook_l2.hpp.

Referenced by emitSnapshot(), notifyTopOfBookIfChanged(), and updateLevel().

◆ sides_

std::array<detail::LevelContainer, SideCount> slick::orderbook::OrderBookL2::sides_
protected

◆ symbol_

SymbolId slick::orderbook::OrderBookL2::symbol_
protected

Definition at line 172 of file orderbook_l2.hpp.

Referenced by OrderBookL2(), emitSnapshot(), operator=(), and updateLevel().

◆ tob_seq_

std::atomic<uint64_t> slick::orderbook::OrderBookL2::tob_seq_
protected

Definition at line 178 of file orderbook_l2.hpp.

Referenced by getBestAsk(), getBestBid(), getTopOfBook(), and notifyTopOfBookIfChanged().


The documentation for this class was generated from the following files: