OnixS C++ B3 Binary UMDF Market Data Handler  1.6.3
API documentation
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 namespace OnixS
26 {
27  namespace B3
28  {
29  namespace MarketData
30  {
31  namespace UMDF
32  {
33  /// Settings for the `OrderBookPool`.
34  ///
35  /// To improve performance, the OrderBook keeps some of its internals in the preallocated
36  /// memory areas (chunks).
37  ///
38  /// The chunks are initially owned by the OrderBookPool object. The `OrderBookPool` object
39  /// is shared between the `OrderBook`s (and can be shared between several instances of the handlers).
40  ///
41  /// When a chunk is taken from the pool, it is not shared between the other order books,
42  /// so the number of the chunks must be at least equal to the number of order books.
43  /// A single order book can use from 1 to 255 chunks if necessary.
44  ///
45  /// The number of books equals the number of securities on a channel or the size of the security filter (if used).
46  ///
47  /// Placing the order book's internals into a single chunk improves data locality
48  /// and can thus improve performance, so the chunk must be sufficient to fit the book content.
49  /// On the other hand, making a chunk too big can lead to excessive memory consumption.
51  {
52  /// Amount of memory chunks.
53  /// @note If the pool is exhausted, the chunks are allocated dynamically and a warning is risen.
54  size_t chunksAmount = 1000;
55 
56  /// Length of a chunk.
57  /// The maximum chunk length should not be much more than the size of memory necessary to store all orders for security.
58  /// @note The minimal value is 1024.
59  size_t chunkLength = 512 * 1024;
60 
61  /// Use Huge pages for chunk allocation.
62  /// @note Takes effect on Linux only.
63  /// @note Huge pages must be enabled on the system level.
64  bool preferHugePage = false;
65 
66  /// Defines growth policy
67  struct GrowthPolicy
68  {
69  enum Policy
70  {
71  ///
73 
74  ///
76  };
77 
79 
80  /// Argument
81  double arg;
82  };
83 
85  };
86 
87  /// A shared memory pool for building order books.
88  class ONIXS_B3_UMDF_MD_API OrderBookPool
89  {
90  public:
91  ///
92  explicit OrderBookPool(const OrderBookPoolSettings&);
93 
94  ///
95  ~OrderBookPool();
96 
97  struct Statistic
98  {
99  ///
100  int64_t maxChunksCountInUse_ = 0;
101  };
102 
103  ///
104  const Statistic& statistic() const noexcept
105  {
106  return statistic_;
107  }
108 
109  private:
110  OrderBookPool(const OrderBookPool&) = delete;
111  OrderBookPool& operator=(const OrderBookPool&) = delete;
112 
113  friend struct OrderBookPoolHelper;
114  Statistic statistic_;
115  void* const impl_;
116  };
117 
118  /// Alias for Instrument Id type.
120 
121  /// Alias for Order Id type.
122  typedef UInt64 OrderId;
123 
124  /// Alias for Quantity type.
125  typedef Int64 Quantity;
126 
127  /// Alias for Price type (4 decimal places)
129 
130  /// Order information
131  struct Order
132  {
133  /// Initializes the instances according to specified attributes.
134  Order(OrderId orderId, Price price, Quantity quantity) ONIXS_B3_UMDF_MD_NOTHROW;
135 
136  /// Returns string representation of the instance.
137  std::string toString() const;
138 
139  /// Appends representation of the instance to the string.
140  ONIXS_B3_UMDF_MD_API
141  void toString(std::string&) const;
142 
143  /// Order Id
144  OrderId orderId() const ONIXS_B3_UMDF_MD_NOTHROW { return orderId_; };
145 
146  /// Price
147  Price price() const ONIXS_B3_UMDF_MD_NOTHROW { return price_; };
148 
149  /// Quantity
150  Quantity quantity() const ONIXS_B3_UMDF_MD_NOTHROW { return quantity_; };
151 
152  /// Indicates whether the order is the market order.
153  bool marketOrder() const { return price_.isNull(); }
154 
155  private:
156  OrderId orderId_;
157  Price price_;
158  Quantity quantity_;
159  };
160 
161  /// compare
162  bool operator==(const Order& l, const Order& r) ONIXS_B3_UMDF_MD_NOTHROW;
163 
164  ///
165  ONIXS_B3_UMDF_MD_API
166  std::ostream& operator<<(std::ostream& stream, const Order& value);
167 
168  /// \private
169  namespace Implementation
170  {
171  class InstrumentRepository;
172  class OrderStorage;
173  struct ONIXS_B3_UMDF_MD_ALIGNAS(1) BookOrderId { char _[3]; };
174  typedef const BookOrderId* OrdersIterator;
175  };
176 
177  /// STL-like iterator over OrderBook levels
179  {
180  public:
181  typedef std::random_access_iterator_tag iterator_category;
182  typedef const Order value_type;
183  typedef ptrdiff_t difference_type;
184  typedef value_type* pointer;
185  typedef value_type& reference;
186 
187  ///
188  OrdersIterator(const OrdersIterator& r) =default;
189 
191  {
192  OrdersIterator tmp(r);
193  tmp.swap(*this);
194  return *this;
195  }
196 
197  void swap(OrdersIterator& other) noexcept
198  {
199  std::swap(storage_, other.storage_);
200  std::swap(pos_, other.pos_);
201  }
202 
203  ONIXS_B3_UMDF_MD_API reference operator*() const noexcept;
204  ONIXS_B3_UMDF_MD_API pointer operator->() const noexcept;
205  ONIXS_B3_UMDF_MD_API reference operator[] (difference_type n) const noexcept;
206 
207  OrdersIterator operator++(int) noexcept { return {storage_, pos_++}; }
208  OrdersIterator& operator++() noexcept { ++pos_; return *this; }
209  OrdersIterator operator+ (difference_type v) const noexcept { return {storage_, pos_ + v};}
210  OrdersIterator operator+= (difference_type v) noexcept { return {storage_, pos_ += v};}
211 
212  OrdersIterator operator--(int) noexcept { return {storage_, pos_--}; }
213  OrdersIterator& operator--() noexcept { --pos_; return *this; }
214  OrdersIterator operator- (difference_type v) const noexcept { return {storage_, pos_ - v};}
215  OrdersIterator operator-= (difference_type v) noexcept { return {storage_, pos_ -= v};}
216 
217  bool operator==(const OrdersIterator& rhs) const noexcept { return pos_ == rhs.pos_; }
218  bool operator!=(const OrdersIterator& rhs) const noexcept { return pos_ != rhs.pos_; }
219 
221  {
222  return l.pos_ - r.pos_;
223  }
224 
225  private:
226  friend class ConstOrdersRange;
227  OrdersIterator(const Implementation::OrderStorage* storage, Implementation::OrdersIterator pos) noexcept
228  : storage_(storage)
229  , pos_(pos)
230  {
231  }
232 
233  private:
234  const Implementation::OrderStorage* storage_{};
236  };
237 
238  /// A range of orders
240  {
241  public:
242  OrdersIterator begin() const noexcept
243  {
244  return cbegin();
245  }
246 
247  OrdersIterator end() const noexcept
248  {
249  return cend();
250  }
251 
252  OrdersIterator cbegin() const noexcept
253  {
254  return {storage_, begin_};
255  }
256 
257  OrdersIterator cend() const noexcept
258  {
259  return {storage_, end_};
260  }
261 
262  bool empty() const noexcept
263  {
264  return begin_ == end_;
265  }
266 
267  size_t size() const noexcept
268  {
269  return static_cast<size_t>(std::distance(begin_, end_));
270  }
271 
272  const Order& operator[](size_t idx) const noexcept
273  {
274  assert(idx < size());
275  return begin().operator[](idx);
276  }
277 
278  private:
279  friend class OrderBook;
280  ConstOrdersRange(Implementation::OrdersIterator begin, Implementation::OrdersIterator end, const Implementation::OrderStorage* storage) noexcept
281  : begin_(begin)
282  , end_(end)
283  , storage_(storage)
284  {}
285 
286  private:
289  const Implementation::OrderStorage* storage_;
290  };
291 
292  /// Order Book.
293  /// A set of bid and ask orders for the given
294  /// security (bids are descending and asks are ascending).
295  class ONIXS_B3_UMDF_MD_API OrderBook
296  {
297  public:
298  /// Instrument Id
299  inline InstrumentId instrumentId() const ONIXS_B3_UMDF_MD_NOTHROW
300  {
301  return id_;
302  }
303 
304  /// Indicates whether book has no bids & asks.
305  bool empty() const noexcept;
306 
307  /// Returns a set of descending ask prices for the given security.
308  ConstOrdersRange asks() const noexcept;
309 
310  /// Returns a set of ascending bid prices for the given security.
311  ConstOrdersRange bids() const noexcept;
312 
313  /// Returns the last processed packet sequence number of the incremental channel
314  Messaging::SeqNum lastMessageSeqNumApplied() const noexcept;
315 
316  /// Returns the last processed RptSeq (sequence number per instrument update) for this instrument.
317  Messaging::RptSeq lastRptSeq() const noexcept;
318 
319  /// String presentation of the book.
320  std::string toString() const;
321 
322  /// String presentation of the book.
323  void toString(std::string&) const;
324 
325  /// Returns brief book info.
326  std::string toShortString() const;
327 
328  /// Appends brief book info to the string.
329  void toShortString(std::string&) const;
330 
331  /// Returns formatted presentation of the book.
332  std::string toFormattedString() const;
333 
334  /// Appends Formatted presentation of the book.
335  void toFormattedString (std::string&) const;
336 
337  ///
338  OrderBook(const OrderBook&) = delete;
339  OrderBook(OrderBook&&) = delete;
340  OrderBook& operator=(const OrderBook&) = delete;
341 
342  protected:
343  OrderBook(size_t depth, const Implementation::InstrumentRepository* instrumentRepository, InstrumentId id);
344  ~OrderBook();
345 
346  const size_t depth_;
347  const InstrumentId id_;
348  const Implementation::InstrumentRepository* const instrumentRepository_;
349 
350  private:
351  const Implementation::OrderStorage* getStorage() const noexcept;
352  };
353 
354  /// For testing only.
355  ONIXS_B3_UMDF_MD_API
356  void validate(const OrderBook&);
357 
358  inline Order::Order(OrderId orderId, Price price, Quantity quantity) ONIXS_B3_UMDF_MD_NOTHROW
359  : orderId_(orderId)
360  , price_(price)
361  , quantity_(quantity)
362  {
363  }
364 
365  inline bool operator==(const Order& l, const Order& r) ONIXS_B3_UMDF_MD_NOTHROW
366  {
367  return (l.orderId() == r.orderId()) && (l.price().mantissa() == r.price().mantissa()) && (l.quantity() == r.quantity());
368  }
369 
370  inline std::string Order::toString() const
371  {
372  std::string str;
373  toString(str);
374  return str;
375  }
376 
377  }
378  }
379  }
380 }
Settings for the OrderBookPool.
Definition: OrderBook.h:50
bool marketOrder() const
Indicates whether the order is the market order.
Definition: OrderBook.h:153
#define ONIXS_B3_UMDF_MD_NOTHROW
Definition: Compiler.h:120
ONIXS_B3_UMDF_MD_API std::ostream & operator<<(std::ostream &stream, const LoggerSettings &settings)
std::string toString() const
Returns string representation of the instance.
Definition: OrderBook.h:370
Messaging::UInt64 UInt64
Definition: Integral.h:40
bool operator!=(const OrdersIterator &rhs) const noexcept
Definition: OrderBook.h:218
ONIXS_B3_UMDF_MD_API void validate(const OrderBook &)
For testing only.
void swap(OrdersIterator &other) noexcept
Definition: OrderBook.h:197
Int64 Quantity
Alias for Quantity type.
Definition: OrderBook.h:125
size_t chunksAmount
Amount of memory chunks.
Definition: OrderBook.h:54
Messaging::PriceOptional Price
Alias for Price type (4 decimal places)
Definition: OrderBook.h:128
STL-like iterator over OrderBook levels.
Definition: OrderBook.h:178
bool operator==(const OrdersIterator &rhs) const noexcept
Definition: OrderBook.h:217
std::random_access_iterator_tag iterator_category
Definition: OrderBook.h:181
const Order & operator[](size_t idx) const noexcept
Definition: OrderBook.h:272
Definition: Handler.h:31
UInt32 SeqNum
Sequence number inside the given channel.
Definition: Fields.h:154
const Implementation::InstrumentRepository *const instrumentRepository_
Definition: OrderBook.h:348
const Statistic & statistic() const noexcept
Definition: OrderBook.h:104
OrdersIterator begin() const noexcept
Definition: OrderBook.h:242
OrdersIterator operator--(int) noexcept
Definition: OrderBook.h:212
Price price() const
Price.
Definition: OrderBook.h:147
friend OrdersIterator::difference_type operator-(const OrdersIterator &l, const OrdersIterator &r) noexcept
Definition: OrderBook.h:220
OrderId orderId() const
Order Id.
Definition: OrderBook.h:144
A nullable real number with a constant exponent.
Definition: Decimal.h:116
OrdersIterator cbegin() const noexcept
Definition: OrderBook.h:252
#define ONIXS_B3_UMDF_MD_ALIGNAS(X)
Definition: Compiler.h:151
OrdersIterator end() const noexcept
Definition: OrderBook.h:247
Messaging::Int64 Int64
Definition: Integral.h:39
OrdersIterator cend() const noexcept
Definition: OrderBook.h:257
OrdersIterator & operator++() noexcept
Definition: OrderBook.h:208
Timestamp operator-(const Timestamp &timestamp, const TimeSpan &timeSpan)
Subtracts time interval from given time point.
Definition: Time.h:805
InstrumentId instrumentId() const
Instrument Id.
Definition: OrderBook.h:299
Quantity quantity() const
Quantity.
Definition: OrderBook.h:150
OrdersIterator operator++(int) noexcept
Definition: OrderBook.h:207
UInt64 OrderId
Alias for Order Id type.
Definition: OrderBook.h:122
UInt32 RptSeq
Sequence number per instrument update.
Definition: Fields.h:186
OrdersIterator & operator--() noexcept
Definition: OrderBook.h:213
A shared memory pool for building order books.
Definition: OrderBook.h:88
bool operator==(const TimeSpan &left, const TimeSpan &right)
Compares with other instance for equality.
Definition: Time.h:327
UInt64 InstrumentId
Alias for Instrument Id type.
Definition: OrderBook.h:119
Timestamp operator+(const Timestamp &timestamp, const TimeSpan &timeSpan)
Adds time interval to given time point.
Definition: Time.h:791
OrdersIterator & operator=(const OrdersIterator &r) noexcept
Definition: OrderBook.h:190
bool preferHugePage
Use Huge pages for chunk allocation.
Definition: OrderBook.h:64