OnixS C++ SGX Titan ITCH Market Data Handler  1.2.2
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 
20 #pragma once
21 
22 #include <vector>
23 
29 
30 
31 ONIXS_SGXTITAN_ITCH_NAMESPACE_BEGIN
32 
33 struct Order;
34 
35 struct ONIXS_SGXTITAN_ITCH_API OrderInfo
36 {
37  OrderInfo();
38  OrderInfo (OrderId orderId, Quantity quantity);
39 
42 };
43 
44 typedef std::vector<OrderInfo> OrderInfos;
45 
46 /// Encapsulates price level concept.
47 struct ONIXS_SGXTITAN_ITCH_API PriceLevel
48 {
49  /// Initializes the instances according to specified attributes.
50  explicit
51  PriceLevel (const Price& price, Quantity quantity = 0, Quantity numberOfOrders = 0, const OrderInfos& orders = OrderInfos() );
52  PriceLevel();
53 
55 
56 #if defined(ONIXS_SGXTITAN_ITCH_COMPILER_CXX_RVALUE_REFERENCES) && ONIXS_SGXTITAN_ITCH_COMPILER_CXX_RVALUE_REFERENCES
57 
58  PriceLevel(PriceLevel&&)
60 
61  PriceLevel&
62  operator=(PriceLevel&& other)
64 
65 #endif
66 
67  PriceLevel(const PriceLevel&);
68 
69  PriceLevel&
70  operator=(const PriceLevel& other);
71 
72  /// Price value.
73  const Price& price() const;
74 
75  /// Quantify for the given price.
76  Quantity quantity() const;
77 
78  /// Total number of orders of given price.
79  Quantity numberOfOrders() const;
80 
81  /// orders ids for a given level
82  const OrderInfos& orders() const;
83 
84  /// swap values
85  void swap (PriceLevel&) ONIXS_SGXTITAN_ITCH_NOTHROW;
86 
87  /// Returns string representation of the instance.
88  std::string toString() const;
89 
90  /// Appends representation of the instance to the string.
91  void toString (std::string&) const;
92 
93  private:
94  /// Updates price value.
95  void price (const Price& value);
96 
97  /// Updates quantity value.
98  void quantity (Quantity value);
99 
100  /// add order id to the list
101  void addOrderId (OrderId id, Quantity quantity);
102 
103  /// remove order id from the list
104  void removeOrderId (OrderId id);
105 
106  private:
107  Price price_;
108  Quantity qty_;
109  OrderInfos orders_;
110 
111  friend struct Order;
112 };
113 
114 const Price& getPrice (const PriceLevel& level);
115 
117 
118 ONIXS_SGXTITAN_ITCH_API
119 std::ostream& operator << (std::ostream& stream, const PriceLevel& value);
120 
121 /// Miscellaneous traits for PriceLevel class.
122 struct ONIXS_SGXTITAN_ITCH_API PriceLevelCollections
123 {
124  /// Sequential collection of price levels.
125  typedef std::vector<PriceLevel> Array;
126 
127  /// Mutable entry iterator.
128  typedef Array::iterator ArrayEntry;
129 
130  /// Iterator for read-only access.
131  typedef Array::const_iterator ArrayConstEntry;
132 };
133 
134 /// Sequence of price levels.
136 
137 /// Iterator to access price levels with write permissions.
139 
140 /// Iterator over read-only collection of price levels.
142 
143 class ONIXS_SGXTITAN_ITCH_API OrderBook
144 {
145  public:
146  /// Base initialization.
147  OrderBook ();
148 
149  /// Destruction interface.
150  virtual ~OrderBook();
151 
152  /// Unique instrument Id as qualified
153  OrderBookId orderBookId() const;
154 
155  /// Indicates whether book has no bids & asks.
156  bool empty() const;
157 
158  /// Returns a set of descending bid prices for the given security.
159  const PriceLevels& asks() const;
160 
161  /// Returns a set of ascending ask prices for the given security.
162  const PriceLevels& bids() const;
163 
164  /// Returns the maximum book depth.
165  size_t depth() const;
166 
167  /// String presentation of the book.
168  std::string toString() const;
169 
170  /// String presentation of the book.
171  void toString (std::string&) const;
172 
173  /// Returns brief book info.
174  std::string toShortString() const;
175 
176  /// Appends brief book info to the string.
177  void toShortString (std::string&) const;
178 
179  /// Returns formatted presentation of the book.
180  std::string toFormattedString() const;
181 
182  /// Appends Formatted presentation of the book.
183  void toFormattedString (std::string&) const;
184 
185  bool bestAsk (Price& price, Quantity& quantity) const;
186 
187  bool bestBid (Price& price, Quantity& quantity) const;
188 
189  /// sets user data pointer
190  void setUserPointer (void* pointer);
191 
192  /// returns kept user data pointer
193  void* getUserPointer() const;
194 
195  private:
196  virtual const PriceLevels& doAsks() const = 0;
197  virtual const PriceLevels& doBids() const = 0;
198  virtual OrderBookId doOrderBookId() const = 0;
199  virtual size_t doDepth() const = 0;
200  virtual bool doBestAsk (Price& price, Quantity& quantity) const = 0;
201  virtual bool doBestBid (Price& price, Quantity& quantity) const = 0;
202 
203  private:
204  void* userPointer_;
205 };
206 
207 
208 ONIXS_SGXTITAN_ITCH_API
209 std::ostream& operator << (std::ostream& stream, const OrderBook& book);
210 
211 /// checks whether the given book is properly built
212 void checkSanity (const OrderBook& book);
213 
214 ONIXS_SGXTITAN_ITCH_NAMESPACE_END
UInt64 OrderId
Alias for OrderId type.
Definition: Defines.h:40
Miscellaneous traits for PriceLevel class.
Definition: OrderBook.h:122
void checkSanity(const OrderBook &book)
checks whether the given book is properly built
Definition: Book.cpp:455
Encapsulates price level concept.
Definition: OrderBook.h:47
#define ONIXS_SGXTITAN_ITCH_NOTHROW
Definition: Compiler.h:27
UInt64 Quantity
Alias for Quantity type.
Definition: Defines.h:46
UInt32 OrderBookId
Alias for Security Id type.
Definition: Defines.h:43
PriceLevelCollections::Array PriceLevels
Sequence of price levels.
Definition: OrderBook.h:135
std::vector< PriceLevel > Array
Sequential collection of price levels.
Definition: OrderBook.h:125
const Price & getPrice(const PriceLevel &level)
Definition: Book.cpp:213
std::vector< OrderInfo > OrderInfos
Definition: OrderBook.h:44
ONIXS_SGXTITAN_ITCH_API std::ostream & operator<<(std::ostream &stream, const OrderBook &book)
bool isValid(const PriceLevel &level)
Definition: Book.cpp:218
Array::iterator ArrayEntry
Mutable entry iterator.
Definition: OrderBook.h:128
PriceLevelCollections::ArrayEntry PriceLevelsEntry
Iterator to access price levels with write permissions.
Definition: OrderBook.h:138
PriceLevelCollections::ArrayConstEntry PriceLevelsConstEntry
Iterator over read-only collection of price levels.
Definition: OrderBook.h:141
Array::const_iterator ArrayConstEntry
Iterator for read-only access.
Definition: OrderBook.h:131