Slick OrderBook 1.1.0
High-Performance C++23 OrderBook Library
Loading...
Searching...
No Matches
orderbook_l2.hpp
Go to the documentation of this file.
1// Copyright 2026 Slick Quant
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
12#include <memory>
13#include <vector>
14#include <array>
15#include <atomic>
16
18
42public:
46 explicit OrderBookL2(SymbolId symbol, std::size_t initial_capacity = 32);
47
49 virtual ~OrderBookL2() = default;
50
51 // Non-copyable, movable (manual implementation due to std::atomic member)
52 OrderBookL2(const OrderBookL2&) = delete;
54 OrderBookL2(OrderBookL2&&) noexcept;
55 OrderBookL2& operator=(OrderBookL2&&) noexcept;
56
58 [[nodiscard]] SymbolId symbol() const noexcept { return symbol_; }
59
70 void updateLevel(Side side, Price price, Quantity quantity, Timestamp timestamp,
71 uint64_t seq_num = 0, bool is_last_in_batch = true);
72
80 bool deleteLevel(Side side, Price price, Timestamp timestamp = 0);
81
86 void clearSide(Side side, Timestamp timestamp = 0);
87
91 void clear(Timestamp timestamp = 0);
92
95 [[nodiscard]] const detail::PriceLevelL2* getBestBid() const noexcept;
96
99 [[nodiscard]] const detail::PriceLevelL2* getBestAsk() const noexcept;
100
103 [[nodiscard]] TopOfBook getTopOfBook() const noexcept;
104
109 [[nodiscard]] std::vector<detail::PriceLevelL2> getLevels(Side side, std::size_t depth = 0) const;
110
115 [[nodiscard]] const detail::PriceLevelL2* getLevel(Side side, Price price) const noexcept;
116
121 [[nodiscard]] const detail::PriceLevelL2* getLevelByIndex(Side side, uint16_t index) const noexcept;
122
126 [[nodiscard]] std::size_t levelCount(Side side) const noexcept;
127
131 [[nodiscard]] bool isEmpty(Side side) const noexcept;
132
135 [[nodiscard]] bool isEmpty() const noexcept;
136
138 void addObserver(std::shared_ptr<IOrderBookObserver> observer) {
139 observers_.addObserver(std::move(observer));
140 }
141
142 bool removeObserver(const std::shared_ptr<IOrderBookObserver>& observer) {
143 return observers_.removeObserver(observer);
144 }
145
146 void clearObservers() noexcept {
147 observers_.clearObservers();
148 }
149
150 [[nodiscard]] std::size_t observerCount() const noexcept {
151 return observers_.observerCount();
152 }
153
156 [[nodiscard]] uint64_t getLastSeqNum() const noexcept {
157 return last_seq_num_;
158 }
159
164 void emitSnapshot(Timestamp timestamp);
165
166protected:
170 void notifyTopOfBookIfChanged(Timestamp timestamp);
171
172 SymbolId symbol_; // Symbol identifier
173 std::array<detail::LevelContainer, SideCount> sides_; // Bid and ask sides (indexed by Side enum)
174 ObserverManager observers_; // Observer notifications
175 TopOfBook cached_tob_; // Cached top-of-book for efficient change detection
176 detail::PriceLevelL2 cached_best_bid_; // Cached best bid (for thread-safe access)
177 detail::PriceLevelL2 cached_best_ask_; // Cached best ask (for thread-safe access)
178 std::atomic<uint64_t> tob_seq_; // Sequence lock for cached_tob_ and best bid/ask (odd = writing, even = readable)
179 uint64_t last_seq_num_; // Last processed sequence number (0 = not tracking)
180 uint16_t change_starting_index_ = INVALID_INDEX; // The lowerest level had changed in a batch. The value reset to INVALID_INDEX after TopOfBook change notified
181};
183
185
186// Include implementation for header-only mode
187#ifdef SLICK_ORDERBOOK_HEADER_ONLY
189#endif
190
Base interface for orderbook observers Observers receive callbacks for orderbook events.
Definition observer.hpp:20
Observer manager for notifying multiple observers Uses type erasure to support both concept-based and...
Definition observer.hpp:52
Level 2 OrderBook - aggregated price levels.
detail::PriceLevelL2 cached_best_ask_
std::atomic< uint64_t > tob_seq_
virtual ~OrderBookL2()=default
Destructor.
std::size_t observerCount() const noexcept
OrderBookL2(const OrderBookL2 &)=delete
OrderBookL2 & operator=(const OrderBookL2 &)=delete
uint64_t getLastSeqNum() const noexcept
Get last processed sequence number.
detail::PriceLevelL2 cached_best_bid_
bool removeObserver(const std::shared_ptr< IOrderBookObserver > &observer)
std::array< detail::LevelContainer, SideCount > sides_
#define SLICK_NAMESPACE_END
Definition config.hpp:127
#define SLICK_DLL_INTERFACE_WARNINGS_PUSH
Definition config.hpp:47
#define SLICK_CACHE_ALIGNED
Definition config.hpp:123
#define SLICK_NAMESPACE_BEGIN
Definition config.hpp:126
#define SLICK_API
Definition config.hpp:13
#define SLICK_DLL_INTERFACE_WARNINGS_POP
Definition config.hpp:48
int64_t Quantity
Quantity type - volume or size.
Definition types.hpp:21
int64_t Price
Price type - fixed-point representation Example: For 4 decimal places, price = actual_price * 10000 T...
Definition types.hpp:18
Side
Order side - using enum for easy array indexing.
Definition types.hpp:35
constexpr uint16_t INVALID_INDEX
Definition types.hpp:32
uint16_t SymbolId
Symbol identifier - can be hash or sequential ID.
Definition types.hpp:27
uint64_t Timestamp
Timestamp in nanoseconds since epoch.
Definition types.hpp:30
Top-of-book snapshot - best bid and ask.
Definition events.hpp:142
Price level for Level 2 orderbook (aggregated) Stores total quantity at a specific price point.