OnixS C++ Fenics UST BIMP Market Data Handler  1.1.0
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 #include <vector>
26 #include <memory>
27 
28 namespace OnixS
29 {
30  namespace FenicsUST
31  {
32  namespace MarketData
33  {
34  namespace Bimp
35  {
36  struct ONIXS_FENICSUST_BIMP_API OrderInfo
37  {
40 
43  };
44 
45  typedef std::vector<OrderInfo> OrderInfos;
46 
47  /// Encapsulates price level concept.
48  struct ONIXS_FENICSUST_BIMP_API PriceLevel
49  {
50  /// Default initialization.
51  PriceLevel();
52 
53  /// Initializes the instances according to specified attributes.
54  explicit
55  PriceLevel (const Price& price,
56  Quantity quantity = 0,
57  Quantity numberOfOrders = 0,
58  Integer8 imestamp = 0,
59  const OrderInfos& orders = OrderInfos() );
60 
61 #if defined(ONIXS_FENICSUST_BIMP_COMPILER_CXX_RVALUE_REFERENCES) && ONIXS_FENICSUST_BIMP_COMPILER_CXX_RVALUE_REFERENCES
62 
63  PriceLevel(PriceLevel&&)
65 
66  PriceLevel&
67  operator=(PriceLevel&& other)
69 #endif
70 
71 #if defined(ONIXS_FENICSUST_BIMP_COMPILER_CXX_DEFAULTED_FUNCTIONS) && ONIXS_FENICSUST_BIMP_COMPILER_CXX_DEFAULTED_FUNCTIONS
72  PriceLevel(const PriceLevel&) = default;
73 #else
74  PriceLevel(const PriceLevel&);
75 #endif
76 
77  PriceLevel&
78  operator=(const PriceLevel& other);
79 
80  /// Price8 value.
81  const Price& getPrice() const
83 
84  /// Updates price value.
85  void setPrice (const Price& value)
87 
88  /// Quantify for the given price.
89  Quantity quantity() const
91 
92  /// Updates quantity value.
93  void quantity (Quantity value)
95 
96  /// Total number of orders of given price.
97  Quantity numberOfOrders() const
99 
100  /// Updates total number of orders.
101  void numberOfOrders (Quantity value)
103 
104  /// Timestamp
105  Integer8 timestamp() const
107 
108  ///
109  void timestamp(Integer8)
111 
112  /// Returns string representation of the instance.
113  std::string toString() const;
114 
115  /// Appends representation of the instance to the string.
116  void toString (std::string&) const;
117 
118  /// swap values
119  void swap (PriceLevel&)
121 
122  /// returns list of order IDs for the price level
123  const OrderInfos& orders() const
125 
126  /// add OrderId to the list
127  void addOrderId(OrderId id, Quantity quantity);
128 
129  /// remove OrderId from the list
130  void removeOrderId (OrderId id);
131 
132  /// upate order by Id
133  void updateOrderId(OrderId id, Quantity quantity);
134 
135  private:
136  Price price_;
137  Quantity qty_;
138  Quantity ordersQty_;
139  Integer8 timestamp_;
140  OrderInfos orders_;
141  };
142 
143  /// compare
144  bool operator== (const PriceLevel& l, const PriceLevel& r)
146 
147  /// Indicates whether the instance of PriceLevel is valid.
148  bool isValid (const PriceLevel& level)
150 
151  /// Returns value of Price8
152  const Price& getPrice (const PriceLevel& level)
154 
155  ONIXS_FENICSUST_BIMP_API
156  std::ostream& operator << (std::ostream& stream, const PriceLevel& value);
157 
158 
159  /// Miscellaneous traits for PriceLevel class.
160  struct ONIXS_FENICSUST_BIMP_API PriceLevelCollections
161  {
162  /// Sequential collection of price levels.
163  typedef std::vector<PriceLevel> Array;
164 
165  /// Mutable entry iterator.
166  typedef Array::iterator ArrayEntry;
167 
168  /// Iterator for read-only access.
169  typedef Array::const_iterator ArrayConstEntry;
170  };
171 
172  /// Sequence of price levels.
174 
175  /// Iterator to access price levels with write permissions.
177 
178  /// Iterator over read-only collection of price levels.
180 
181  /// Order Book.
182  /// A set of bid prices and ask prices for the given
183  /// security (bids are descending and asks are ascending).
184  /// The quantity is provided at each price level.
185  class ONIXS_FENICSUST_BIMP_API OrderBook
186  {
187  public:
188  // Base initialization.
189  OrderBook ();
190 
191  /// Destruction interface.
192  virtual ~OrderBook() {};
193 
194  /// Instrument Id
195  InstrumentId instrumentId() const;
196 
197  /// Indicates whether book has no bids & asks.
198  bool empty() const
200 
201  bool bestAsk (Price& price, Quantity& quantity) const;
202 
203  bool bestBid (Price& price, Quantity& quantity) const;
204 
205  /// String presentation of the book.
206  std::string toString() const;
207 
208  /// String presentation of the book.
209  void toString (std::string&) const;
210 
211  /// Returns brief book info.
212  std::string toShortString() const;
213 
214  /// Appends brief book info to the string.
215  void toShortString (std::string&) const;
216 
217  /// Returns formatted presentation of the book.
218  std::string toFormattedString() const;
219 
220  /// Appends Formatted presentation of the book.
221  void toFormattedString (std::string&) const;
222 
223  /// Returns a set of descending bid prices for the given security.
224  const PriceLevels& asks() const;
225 
226  /// Returns a set of ascending ask prices for the given security.
227  const PriceLevels& bids() const;
228 
229  /// Returns Last Message Seq Num Applied to the Book
230  UInt64 lastMessageSeqNumApplied() const;
231 
232 
233  private:
234  virtual const PriceLevels& doAsks() const = 0;
235  virtual const PriceLevels& doBids() const = 0;
236  virtual bool doBestAsk (Price& price, Quantity& quantity) const = 0;
237  virtual bool doBestBid (Price& price, Quantity& quantity) const = 0;
238  virtual InstrumentId doInstrumentID() const = 0;
239  virtual UInt64 doLastMessageSeqNumApplied() const = 0;
240  };
241 
242  inline const Price& PriceLevel::getPrice() const
243  ONIXS_FENICSUST_BIMP_NOTHROW
244  {
245  return price_;
246  }
247 
248  inline void PriceLevel::setPrice (const Price& value)
249  ONIXS_FENICSUST_BIMP_NOTHROW
250  {
251  price_ = value;
252  }
253 
255  ONIXS_FENICSUST_BIMP_NOTHROW
256  {
257  return qty_;
258  }
259 
260  inline void PriceLevel::quantity (Quantity value)
261  ONIXS_FENICSUST_BIMP_NOTHROW
262  {
263  qty_ = value;
264  }
265 
267  ONIXS_FENICSUST_BIMP_NOTHROW
268  {
269  return ordersQty_;
270  }
271 
273  ONIXS_FENICSUST_BIMP_NOTHROW
274  {
275  ordersQty_ = value;
276  }
277 
278  inline const Price& getPrice (const PriceLevel& level)
279  ONIXS_FENICSUST_BIMP_NOTHROW
280  {
281  return level.getPrice();
282  }
283 
284  inline bool isValid (const PriceLevel& level)
285  ONIXS_FENICSUST_BIMP_NOTHROW
286  {
287  return (level.quantity() != 0);
288  }
289 
291  ONIXS_FENICSUST_BIMP_NOTHROW
292  {
293  return timestamp_;
294  }
295 
296  inline void PriceLevel::timestamp(Integer8 timestamp)
297  ONIXS_FENICSUST_BIMP_NOTHROW
298  {
299  timestamp_ = timestamp;
300  }
301 
302  inline bool operator== (const PriceLevel& l, const PriceLevel& r)
303  ONIXS_FENICSUST_BIMP_NOTHROW
304  {
305  return (l.quantity() == r.quantity() ) && (l.numberOfOrders() == r.numberOfOrders() ) && (l.getPrice() == r.getPrice() );
306  }
307 
308  inline const PriceLevels& OrderBook::asks() const
309  {
310  return doAsks();
311  }
312 
313  inline const PriceLevels& OrderBook::bids() const
314  {
315  return doBids();
316  }
317 
318  inline bool OrderBook::bestAsk (Price& price, Quantity& quantity ) const
319  {
320  return doBestAsk (price, quantity);
321  }
322 
323  inline bool OrderBook::bestBid (Price& price, Quantity& quantity ) const
324  {
325  return doBestBid (price, quantity);
326  }
327 
329  {
330  return doInstrumentID();
331  }
332 
333  inline bool OrderBook::empty() const
334  ONIXS_FENICSUST_BIMP_NOTHROW
335  {
336  return bids().empty() && asks().empty();
337  }
338 
340  {
341  return doLastMessageSeqNumApplied();
342  }
343  }
344  }
345  }
346 }
Quantity numberOfOrders() const ONIXS_FENICSUST_BIMP_NOTHROW
Total number of orders of given price.
Definition: OrderBook.h:266
bool bestBid(Price &price, Quantity &quantity) const
Definition: OrderBook.h:323
InstrumentId instrumentId() const
Instrument Id.
Definition: OrderBook.h:328
ONIXS_FENICSUST_BIMP_API std::ostream & operator<<(std::ostream &stream, const ServiceDescriptor &descriptor)
Quantity quantity() const ONIXS_FENICSUST_BIMP_NOTHROW
Quantify for the given price.
Definition: OrderBook.h:254
bool operator==(const PriceLevel &l, const PriceLevel &r) ONIXS_FENICSUST_BIMP_NOTHROW
compare
Definition: OrderBook.h:302
PriceLevelCollections::ArrayEntry PriceLevelsEntry
Iterator to access price levels with write permissions.
Definition: OrderBook.h:176
PriceLevelCollections::Array PriceLevels
Sequence of price levels.
Definition: OrderBook.h:173
virtual ~OrderBook()
Destruction interface.
Definition: OrderBook.h:192
const Price & getPrice() const ONIXS_FENICSUST_BIMP_NOTHROW
Price8 value.
Definition: OrderBook.h:242
UInt64 lastMessageSeqNumApplied() const
Returns Last Message Seq Num Applied to the Book.
Definition: OrderBook.h:339
bool bestAsk(Price &price, Quantity &quantity) const
Definition: OrderBook.h:318
PriceLevelCollections::ArrayConstEntry PriceLevelsConstEntry
Iterator over read-only collection of price levels.
Definition: OrderBook.h:179
const Price & getPrice(const PriceLevel &level) ONIXS_FENICSUST_BIMP_NOTHROW
Returns value of Price8.
Definition: OrderBook.h:278
Integer8 timestamp() const ONIXS_FENICSUST_BIMP_NOTHROW
Timestamp.
Definition: OrderBook.h:290
Encapsulates price level concept.
Definition: OrderBook.h:48
const PriceLevels & bids() const
Returns a set of ascending ask prices for the given security.
Definition: OrderBook.h:313
Integer8 InstrumentId
Alias for Instrument Id type.
Definition: Defines.h:88
void setPrice(const Price &value) ONIXS_FENICSUST_BIMP_NOTHROW
Updates price value.
Definition: OrderBook.h:248
Integer8 Quantity
Alias for Quantity type.
Definition: Defines.h:97
Array::const_iterator ArrayConstEntry
Iterator for read-only access.
Definition: OrderBook.h:169
#define ONIXS_FENICSUST_BIMP_NOTHROW
Definition: Compiler.h:27
std::vector< OrderInfo > OrderInfos
Definition: OrderBook.h:45
Miscellaneous traits for PriceLevel class.
Definition: OrderBook.h:160
std::vector< PriceLevel > Array
Sequential collection of price levels.
Definition: OrderBook.h:163
const PriceLevels & asks() const
Returns a set of descending bid prices for the given security.
Definition: OrderBook.h:308
SignedInteger8 Price
Alias for Order Id type.
Definition: Defines.h:94
bool empty() const ONIXS_FENICSUST_BIMP_NOTHROW
Indicates whether book has no bids & asks.
Definition: OrderBook.h:333
Array::iterator ArrayEntry
Mutable entry iterator.
Definition: OrderBook.h:166
Integer8 OrderId
Alias for Order Id type.
Definition: Defines.h:91
bool isValid(const PriceLevel &level) ONIXS_FENICSUST_BIMP_NOTHROW
Indicates whether the instance of PriceLevel is valid.
Definition: OrderBook.h:284