OnixS C++ B3 Binary UMDF Market Data Handler 1.10.0
Users' manual and API documentation
Loading...
Searching...
No Matches
OrderBook.h
Go to the documentation of this file.
1/*
2* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3*
4* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5* and international copyright treaties.
6*
7* Access to and use of the software is governed by the terms of the applicable ONIXS Software
8* Services Agreement (the Agreement) and Customer end user license agreements granting
9* a non-assignable, non-transferable and non-exclusive license to use the software
10* for it's own data processing purposes under the terms defined in the Agreement.
11*
12* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13* of this source code or associated reference material to any other location for further reproduction
14* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15*
16* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17* the terms of the Agreement is a violation of copyright law.
18*/
19#pragma once
20
24
25#ifdef ONIXS_B3_UMDF_MD_CXX20
26# include <compare>
27# include <ranges>
28#endif
29
49{
51 size_t orderBooksAmount = 500;
52
55 size_t chunksAmount = 1000;
56
60 size_t chunkLength = 512 * 1024;
61
65 bool preferHugePage = false;
66
69 {
78
80
82 double arg;
83 };
84
86};
87
89class ONIXS_B3_UMDF_MD_API OrderBookPool
90{
91public:
94
97
98 struct Statistic
99 {
102 };
103
105 const Statistic& statistic() const noexcept
106 {
107 return statistic_;
108 }
109
110private:
111 OrderBookPool(const OrderBookPool&) = delete;
112 OrderBookPool& operator=(const OrderBookPool&) = delete;
113
114 friend struct OrderBookPoolHelper;
115 Statistic statistic_;
116 void* const impl_;
117
118 // for testing only
121};
122
125
128
131
134
136struct Order
137{
140
142 std::string toString() const;
143
145 ONIXS_B3_UMDF_MD_API
146 void toString(std::string&) const;
147
149 OrderId orderId() const noexcept { return orderId_; };
150
152 Price price() const noexcept { return price_; };
153
155 Quantity quantity() const noexcept { return quantity_; };
156
158 bool marketOrder() const { return price_.isNull(); }
159
160private:
161 OrderId orderId_;
162 Price price_;
163 Quantity quantity_;
164};
165
167bool operator==(const Order& l, const Order& r) noexcept;
168
170ONIXS_B3_UMDF_MD_API
171std::ostream& operator<<(std::ostream& stream, const Order& value);
172
174namespace Implementation
175{
176 class InstrumentRepository;
177 class OrderStorage;
178 struct alignas(1) BookOrderId { char _[3]; };
180};
181
184{
185public:
186 using iterator_concept = std::random_access_iterator_tag;
187 using iterator_category = std::random_access_iterator_tag;
189 using pointer = const Order*;
190 using reference = const Order&;
191 using difference_type = ptrdiff_t;
192
194 OrdersIterator() noexcept = default;
195
197 OrdersIterator(const OrdersIterator& r) noexcept = default;
198
200 OrdersIterator& operator=(const OrdersIterator&) noexcept = default;
201
203 void swap(OrdersIterator& other) noexcept
204 {
205 std::swap(storage_, other.storage_);
206 std::swap(pos_, other.pos_);
207 }
208
210 bool valid() const noexcept
211 {
212 return storage_ != nullptr;
213 }
214
216 reference operator*() const noexcept
217 {
218 assert(valid());
219
220 return *dereference();
221 }
222
224 pointer operator->() const noexcept
225 {
226 assert(valid());
227
228 return dereference();
229 }
230
232 reference operator[] (difference_type n) const noexcept
233 {
234 assert(valid());
235
236 return *dereference(n);
237 }
238
241 {
242 assert(valid());
243
244 OrdersIterator tmp(*this);
245 ++(*this);
246 return tmp;
247 }
248
251 {
252 assert(valid());
253
254 ++pos_;
255 return *this;
256 }
257
260 {
261 assert(valid());
262
263 return {storage_, pos_ + v};
264 }
265
268 {
269 assert(valid());
270
271 pos_ += v;
272 return *this;
273 }
274
277 {
278 assert(valid());
279
280 OrdersIterator tmp(*this);
281 --(*this);
282 return tmp;
283 }
284
287 {
288 assert(valid());
289
290 --pos_;
291 return *this;
292 }
293
296 {
297 assert(valid());
298
299 return {storage_, pos_ - v};
300 }
301
304 {
305 assert(valid());
306
307 pos_ -= v;
308 return *this;
309 }
310
313 {
314 assert(it.valid());
315
316 it += v;
317 return it;
318 }
319
321 friend bool operator==(const OrdersIterator& l, const OrdersIterator& r) noexcept
322 {
323 assert(r.valid());
324 assert(l.valid());
325 assert(l.storage_ == r.storage_);
326
327 return l.pos_ == r.pos_;
328 }
329
331 friend bool operator!=(const OrdersIterator& l, const OrdersIterator& r) noexcept
332 {
333 assert(r.valid());
334 assert(l.valid());
335 assert(l.storage_ == r.storage_);
336
337 return l.pos_ != r.pos_;
338 }
339
340#ifdef ONIXS_B3_UMDF_MD_CXX20
342 friend std::strong_ordering operator<=>(const OrdersIterator& l, const OrdersIterator& r) noexcept
343 {
344 assert(r.valid());
345 assert(l.valid());
346 assert(l.storage_ == r.storage_);
347
348 if (l.pos_ < r.pos_)
349 return std::strong_ordering::less;
350
351 if (r.pos_ < l.pos_)
352 return std::strong_ordering::greater;
353
354 return std::strong_ordering::equal;
355 }
356#endif
357
360 {
361 assert(r.valid());
362 assert(l.valid());
363 assert(l.storage_ == r.storage_);
364
365 return l.pos_ - r.pos_;
366 }
367
368private:
369 friend class ConstOrdersRange;
370 OrdersIterator(const Implementation::OrderStorage* storage, Implementation::OrdersIterator pos) noexcept
371 : storage_(storage)
372 , pos_(pos)
373 {
374 }
375
376 ONIXS_B3_UMDF_MD_API pointer dereference(difference_type n = 0) const noexcept;
377
378private:
379 const Implementation::OrderStorage* storage_{};
380 Implementation::OrdersIterator pos_{};
381};
382
386 : public std::ranges::view_interface<ConstOrdersRange>
387#endif
388{
389public:
391 OrdersIterator begin() const noexcept
392 {
393 assert(valid());
394 return cbegin();
395 }
396
398 OrdersIterator end() const noexcept
399 {
400 assert(valid());
401 return cend();
402 }
403
405 OrdersIterator cbegin() const noexcept
406 {
407 assert(valid());
408 return {storage_, begin_};
409 }
410
412 OrdersIterator cend() const noexcept
413 {
414 assert(valid());
415 return {storage_, end_};
416 }
417
419 bool empty() const noexcept
420 {
421 assert(valid());
422 return begin_ == end_;
423 }
424
426 size_t size() const noexcept
427 {
428 assert(valid());
429 return static_cast<size_t>(std::distance(begin_, end_));
430 }
431
433 const Order& operator[](size_t idx) const noexcept
434 {
435 assert(valid());
436 assert(idx < size());
437 return begin().operator[](idx);
438 }
439
441 ConstOrdersRange() noexcept = default;
442
444 bool valid() const noexcept
445 {
446 return storage_ != nullptr;
447 }
448
449private:
450 friend class OrderBook;
451 ConstOrdersRange(Implementation::OrdersIterator begin, Implementation::OrdersIterator end, const Implementation::OrderStorage* storage) noexcept
452 : begin_(begin)
453 , end_(end)
454 , storage_(storage)
455 {}
456
457private:
459 Implementation::OrdersIterator end_{};
460 const Implementation::OrderStorage* storage_{};
461};
462
466class ONIXS_B3_UMDF_MD_API OrderBook
467{
468public:
470 bool empty() const noexcept;
471
473 ConstOrdersRange asks() const noexcept;
474
476 ConstOrdersRange bids() const noexcept;
477
479 std::string toString() const;
480
482 void toString(std::string&) const;
483
485 std::string toShortString() const;
486
488 void toShortString(std::string&) const;
489
491 std::string toFormattedString() const;
492
494 void toFormattedString (std::string&) const;
495
497 OrderBook(const OrderBook&) = delete;
498 OrderBook(OrderBook&&) = delete;
499 OrderBook& operator=(const OrderBook&) = delete;
500
501protected:
502 OrderBook(size_t depth, const Implementation::InstrumentRepository* instrumentRepository) noexcept;
504
505 const size_t depth_;
506 const Implementation::InstrumentRepository* const instrumentRepository_;
507
508private:
509 const Implementation::OrderStorage* getStorage() const noexcept;
510 InstrumentId instrumentId() const noexcept;
511};
512
514ONIXS_B3_UMDF_MD_API
515void validate(const OrderBook&);
516
518 : orderId_(orderId)
519 , price_(price)
520 , quantity_(quantity)
521{
522}
523
524inline bool operator==(const Order& l, const Order& r) noexcept
525{
526 return (l.orderId() == r.orderId()) && (l.price().mantissa() == r.price().mantissa()) && (l.quantity() == r.quantity());
527}
528
529inline std::string Order::toString() const
530{
531 std::string str;
532 toString(str);
533 return str;
534}
535
538{
539 typedef UInt8 Base;
540
542 enum Enum
543 {
545 New = 0,
546
549
552
554 Clean = 3,
555 };
556};
557
560{
561public:
565 : side_(side)
566 , action_(action)
567 , previous_(previous)
568 , current_(current)
569 {
570 }
571
573 Messaging::Side::Enum side() const noexcept { return side_; }
574
576 BookAtomicChangeAction::Enum action() const noexcept { return action_; }
577
579 const Order* previous() const noexcept { return previous_; }
580
582 const Order* current() const noexcept { return current_; }
583
584private:
587 const Order* previous_;
588 const Order* current_;
589};
590
592
593#ifdef ONIXS_B3_UMDF_MD_CXX20
594namespace std::ranges { template <> inline constexpr bool enable_borrowed_range<OnixS::B3::MarketData::UMDF::ConstOrdersRange> = true; }
595#endif
#define ONIXS_B3_UMDF_MD_NAMESPACE_END
Definition ABI.h:168
#define ONIXS_B3_UMDF_MD_NAMESPACE_BEGIN
Definition ABI.h:163
#define ONIXS_B3_UMDF_MD_CXX20
const Order * previous() const noexcept
Definition OrderBook.h:579
BookAtomicChangeAction::Enum action() const noexcept
Definition OrderBook.h:576
Messaging::Side::Enum side() const noexcept
Definition OrderBook.h:573
constexpr BookAtomicChange(Messaging::Side::Enum side, BookAtomicChangeAction::Enum action, const Order *previous, const Order *current) noexcept
Definition OrderBook.h:563
const Order * current() const noexcept
Definition OrderBook.h:582
size_t size() const noexcept
Returns the number of orders in the range.
Definition OrderBook.h:426
bool empty() const noexcept
Indicates whether the range contains no orders.
Definition OrderBook.h:419
OrdersIterator cend() const noexcept
Returns a const iterator to one past the last order.
Definition OrderBook.h:412
const Order & operator[](size_t idx) const noexcept
Returns the order at the given zero-based index.
Definition OrderBook.h:433
OrdersIterator end() const noexcept
Returns an iterator to one past the last order.
Definition OrderBook.h:398
bool valid() const noexcept
Indicates whether the range is bound to storage.
Definition OrderBook.h:444
ConstOrdersRange() noexcept=default
Creates an empty, invalid range.
OrdersIterator begin() const noexcept
Returns an iterator to the first order.
Definition OrderBook.h:391
OrdersIterator cbegin() const noexcept
Returns a const iterator to the first order.
Definition OrderBook.h:405
A shared memory pool for building order books.
Definition OrderBook.h:90
const Statistic & statistic() const noexcept
Definition OrderBook.h:105
OrderBookPool(const OrderBookPoolSettings &)
std::string toString() const
String presentation of the book.
std::string toFormattedString() const
Returns formatted presentation of the book.
bool empty() const noexcept
Indicates whether book has no bids & asks.
ConstOrdersRange asks() const noexcept
Returns a set of descending ask prices for the given security.
const Implementation::InstrumentRepository *const instrumentRepository_
Definition OrderBook.h:506
OrderBook(const OrderBook &)=delete
ConstOrdersRange bids() const noexcept
Returns a set of ascending bid prices for the given security.
std::string toShortString() const
Returns brief book info.
STL-like iterator over the OrderBook levels.
Definition OrderBook.h:184
friend bool operator==(const OrdersIterator &l, const OrdersIterator &r) noexcept
Compares two iterators for equality.
Definition OrderBook.h:321
friend bool operator!=(const OrdersIterator &l, const OrdersIterator &r) noexcept
Compares two iterators for inequality.
Definition OrderBook.h:331
OrdersIterator operator++(int) noexcept
Returns a copy of the iterator before incrementing it.
Definition OrderBook.h:240
OrdersIterator & operator+=(difference_type v) noexcept
Advances the iterator by the given offset.
Definition OrderBook.h:267
OrdersIterator operator--(int) noexcept
Returns a copy of the iterator before decrementing it.
Definition OrderBook.h:276
void swap(OrdersIterator &other) noexcept
Exchanges the iterator state with another one.
Definition OrderBook.h:203
friend OrdersIterator operator+(difference_type v, OrdersIterator it) noexcept
Returns an iterator advanced by the given offset.
Definition OrderBook.h:312
OrdersIterator & operator--() noexcept
Moves the iterator to the previous order.
Definition OrderBook.h:286
reference operator*() const noexcept
Returns the current element.
Definition OrderBook.h:216
OrdersIterator operator-(difference_type v) const noexcept
Returns an iterator moved backward by the given offset.
Definition OrderBook.h:295
std::random_access_iterator_tag iterator_category
Definition OrderBook.h:187
OrdersIterator & operator++() noexcept
Advances the iterator to the next order.
Definition OrderBook.h:250
std::random_access_iterator_tag iterator_concept
Definition OrderBook.h:186
bool valid() const noexcept
Indicates whether the iterator is valid.
Definition OrderBook.h:210
OrdersIterator operator+(difference_type v) const noexcept
Returns an iterator advanced by the given offset.
Definition OrderBook.h:259
pointer operator->() const noexcept
Returns a pointer to the current element.
Definition OrderBook.h:224
OrdersIterator() noexcept=default
Creates an empty iterator.
friend std::strong_ordering operator<=>(const OrdersIterator &l, const OrdersIterator &r) noexcept
Compares two iterators using three-way ordering.
Definition OrderBook.h:342
friend OrdersIterator::difference_type operator-(const OrdersIterator &l, const OrdersIterator &r) noexcept
Returns the distance between two iterators.
Definition OrderBook.h:359
OrdersIterator & operator-=(difference_type v) noexcept
Moves the iterator backward by the given offset.
Definition OrderBook.h:303
NullableFixedPointDecimal< Int64, IntegralConstant< Int8, -4 >, IntegralConstant< Int64, -9223372036854775807LL-1 > > PriceOptional
Optional price (4 decimal places).
Definition Composites.h:160
Messaging::UInt8 UInt8
Definition Integral.h:31
Messaging::PriceOptional Price
Alias for Price type (4 decimal places)
Definition OrderBook.h:133
UInt64 InstrumentId
Alias for Instrument Id type.
Definition OrderBook.h:124
UInt64 OrderId
Alias for Order Id type.
Definition OrderBook.h:127
bool operator==(const TimeSpan &left, const TimeSpan &right)
Compares with other instance for equality.
Definition Time.h:327
Messaging::UInt64 UInt64
Definition Integral.h:40
ONIXS_B3_UMDF_MD_API std::ostream & operator<<(std::ostream &stream, const LoggerSettings &settings)
Int64 Quantity
Alias for Quantity type.
Definition OrderBook.h:130
ONIXS_B3_UMDF_MD_API void validate(const OrderBook &)
For testing only.
Messaging::Int64 Int64
Definition Integral.h:39
STL namespace.
Enum
Types of Market Data update action.
Definition OrderBook.h:543
size_t chunksAmount
Amount of memory chunks.
Definition OrderBook.h:55
bool preferHugePage
Use Huge pages for chunk allocation.
Definition OrderBook.h:65
size_t orderBooksAmount
Amount of order book objects.
Definition OrderBook.h:51
std::string toString() const
Returns string representation of the instance.
Definition OrderBook.h:529
Quantity quantity() const noexcept
Quantity.
Definition OrderBook.h:155
OrderId orderId() const noexcept
Order Id.
Definition OrderBook.h:149
ONIXS_B3_UMDF_MD_API void toString(std::string &) const
Appends representation of the instance to the string.
bool marketOrder() const
Indicates whether the order is the market order.
Definition OrderBook.h:158
Price price() const noexcept
Price.
Definition OrderBook.h:152
Order(OrderId orderId, Price price, Quantity quantity) noexcept
Initializes the instances according to specified attributes.
Definition OrderBook.h:517