OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  7.4.2
API documentation
EobiBook.cpp
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 #include <sstream>
20 
21 #include <boost/format.hpp>
22 #include <boost/foreach.hpp>
23 
25 #include "EobiBookImpl.h"
26 
27 namespace OnixS {
28 namespace Eurex {
29 namespace MarketData {
30 namespace EOBI {
31 
32 
33 namespace
34 {
35 
36 void bestBid2string(std::stringstream& ss, Decimal price, Quantity quantity)
37 {
38  ss << boost::format(" - | %14d | %20.4f |") % quantity % (double)price;
39 }
40 
41 void bestAsk2string(std::stringstream& ss, Decimal price, Quantity quantity)
42 {
43  ss << boost::format("%20.4f | %14d | - |") % (double)price % quantity ;
44 }
45 
46 void bid2string(std::stringstream& ss, const PriceLevel& bid)
47 {
48  ss << boost::format(" %9d | %14d | %20.4f |") % bid.numberOfOrders() % bid.quantity() % static_cast<double>(bid.price());
49 }
50 
51 void ask2string(std::stringstream& ss, const PriceLevel& ask)
52 {
53  ss << boost::format("%20.4f | %14d | %9d |") % static_cast<double>(ask.price()) % ask.quantity() % ask.numberOfOrders();
54 }
55 
56 void printLevels(std::stringstream& presentation, const PriceLevels& levels)
57 {
58  if (!levels.empty())
59  {
60  PriceLevelsConstEntry lastLevel = --levels.end();
61 
62  for (PriceLevelsConstEntry level = levels.begin();
63  level != levels.end();
64  ++level)
65  {
66  presentation << level->toString();
67 
68  if (level != lastLevel)
69  presentation << ", ";
70  }
71  }
72 }
73 
74 }// namespace
75 
76 
78  orderId_(0),
79  quantity_(0)
80 {
81 }
82 
84  orderId_(orderId),
85  quantity_(quantity)
86 {
87 }
88 
89 PriceLevel::PriceLevel (const Decimal& price, QuantityType quantity, QuantityType /*numberOfOrders*/, const OrderInfos& orders) :
90  price_(price),
91  qty_(quantity),
92  orders_(orders)
93 {}
94 
96  price_(),
97  qty_(0)
98 {
99  orders_.reserve(5); // \todo:
100 }
101 
102 
104 {
105  return price_;
106 }
107 
108 void PriceLevel::price(const Decimal& value)
109 {
110  price_ = value;
111 }
112 
114 {
115  return qty_;
116 }
117 
119 {
120  qty_ = value;
121 }
122 
124 {
125  return static_cast<Quantity>(orders_.size());
126 }
127 
129 {
130  return orders_;
131 }
132 
133 void PriceLevel::swap(PriceLevel &obj) throw()
134 {
135  std::swap(price_, obj.price_);
136  std::swap(qty_, obj.qty_);
137  std::swap(orders_, obj.orders_);
138 }
139 
140 void PriceLevel::addOrderId(OrderId id, QuantityType quantity)
141 {
142  BOOST_ASSERT(id != 0);
143  BOOST_ASSERT(quantity != 0);
144 
145  orders_.push_back(OrderInfo(id, quantity));
146 }
147 
148 struct FindById : public std::binary_function<OrderInfo, OrderId, bool>
149 {
150  bool operator()(const OrderInfo& oInf, OrderId id) const
151  {
152  BOOST_ASSERT(id != 0);
153  BOOST_ASSERT(oInf.orderId_ != 0);
154  BOOST_ASSERT(oInf.quantity_ != 0);
155 
156  return (oInf.orderId_ == id);
157  }
158 };
159 
160 void PriceLevel::removeOrderId(OrderId id)
161 {
162  BOOST_ASSERT(std::find_if(orders_.begin(), orders_.end(), std::bind2nd(FindById(), id)) != orders_.end());
163  orders_.erase(std::remove_if(orders_.begin(), orders_.end(), std::bind2nd(FindById(), id)), orders_.end());
164 }
165 
166 const Decimal& getPrice (const PriceLevel& level)
167 {
168  return level.price();
169 }
170 
171 bool isValid (const PriceLevel& level)
172 {
173  BOOST_ASSERT(level.quantity() >= level.numberOfOrders());
174  BOOST_ASSERT(level.numberOfOrders() == (QuantityType)level.orders().size());
175 
176  return (level.quantity() != 0);
177 }
178 
179 std::string PriceLevel::toString() const
180 {
181  std::string str;
182  toString (str);
183  return str;
184 }
185 
186 void PriceLevel::toString (std::string& str) const
187 {
188  std::stringstream ss;
189 
190  ss << "PriceLevel [price="
191  << price_.toString()
192  << ", quantity="
193  << qty_
194  << ", numberOfOrders="
195  << numberOfOrders()
196  << ", orders= [";
197 
198  BOOST_FOREACH(OrderInfo info, orders_)
199  {
200  ss << info.orderId_ << " - " << info.quantity_ << ", ";
201  }
202 
203  ss << "] ]";
204 
205  str = ss.str();
206 };
207 
208 std::ostream& operator << (std::ostream& stream, const PriceLevel& value)
209 {
210  stream << value.toString();
211  return stream;
212 }
213 
215  userPointer_(nullptr)
216 {
217 }
218 
220 {
221 }
222 
223 void OrderBook::setUserPointer(void* pointer)
224 {
225  userPointer_ = pointer;
226 }
227 
229 {
230  return userPointer_;
231 }
232 
233 bool OrderBook::bestAsk (Decimal& price, QuantityType& quantity) const
234 {
235  return doBestAsk(price, quantity);
236 }
237 
238 bool OrderBook::bestBid (Decimal& price, QuantityType& quantity) const
239 {
240  return doBestBid(price, quantity);
241 }
242 
243 size_t OrderBook::depth() const
244 {
245  return doDepth();
246 }
247 
249 {
250  return doSecurityId();
251 }
252 
253 bool OrderBook::empty() const
254 {
255  return doAsks().empty() && doBids().empty();
256 }
257 
259 {
260  return doAsks();
261 }
262 
264 {
265  return doBids();
266 }
267 
268 std::string OrderBook::toString() const
269 {
270  std::string str;
271 
272  toString(str);
273 
274  return str;
275 }
276 
277 void OrderBook::toString (std::string& str) const
278 {
279  std::stringstream ss;
280  ss
281  << "Book [securityId="
282  << securityId()
283  << ", depth="
284  << depth();
285 
286  ss << ", bids: ";
287  printLevels(ss, bids());
288 
289  ss << ", asks: ";
290  printLevels(ss, asks());
291 
292  ss << ']';
293 
294  str = ss.str();
295 }
296 
297 std::string OrderBook::toShortString() const
298 {
299  std::string str;
300 
301  toShortString(str);
302 
303  return str;
304 }
305 
306 void OrderBook::toShortString (std::string& str) const
307 {
308  std::stringstream ss;
309  ss << "Book ["
310  << securityId()
311  << ", depth=" << depth() << "]";
312  str = ss.str();
313 }
314 
315 std::string OrderBook::toFormattedString() const
316 {
317  std::string str;
318 
319  toFormattedString (str);
320 
321  return str;
322 }
323 
324 void OrderBook::toFormattedString (std::string& str) const
325 {
326  std::stringstream ss;
327 
328  const std::string briefInfo = toShortString();
329 
330  ss << briefInfo << "\n";
331 
332 
333  ss << " |-----------------------------------------------------------------------------------------------------------|\n";
334  ss << " | BID | | ASC |\n";
335  ss << " |----------------------------------------------------+-+----------------------------------------------------|\n";
336  ss << " | OrderCount | Quantity | Price | | Price | Quantity | OrderCount |\n";
337  ss << " |------------+----------------+----------------------+-+----------------------+----------------+------------|\n";
338 
339  Decimal bestBidPrice;
340  QuantityType bestBidQuantity = 0;
341  bool bestBidAvailable = bestBid(bestBidPrice, bestBidQuantity);
342 
343  Decimal bestAskPrice;
344  QuantityType bestAskQuantity = 0;
345  bool bestAskAvailable = bestAsk(bestAskPrice, bestAskQuantity);
346 
347  if( bestBidAvailable || bestAskAvailable )
348  {
349  ss
350  << " "
351  << "b"
352  << " | ";
353 
354  bestBid2string(ss, bestBidPrice, bestBidQuantity);
355 
356  ss << " | ";
357 
358  bestAsk2string(ss, bestAskPrice, bestAskQuantity);
359 
360  ss << "\n";
361 
362  ss
363  << " "
364  << "-"
365  << " |";
366 
367  ss << "------------+----------------+----------------------+-+----------------------+----------------+------------|\n";
368  }
369 
370  static const PriceLevel zeroPriceLevel(Decimal(0), 0, 0);
371 
372  using namespace std;
373 
374  const size_t depth = max(bids().size(), asks().size());
375 
376  for (size_t level = 0; level < depth; ++level)
377  {
378  ss
379  << ((level < 9) ? " " : " ")
380  << level + 1
381  << " | ";
382 
383  bid2string(
384  ss,
385  level < bids().size()
386  ? bids().at(level) : zeroPriceLevel);
387 
388  ss << " | ";
389 
390 
391  ask2string(
392  ss,
393  level < asks().size()
394  ? asks().at(level) : zeroPriceLevel);
395 
396 
397  if (level + 1 != depth)
398  ss << "\n";
399  }
400 
401  ss << "\n";
402  ss << " |-----------------------------------------------------------------------------------------------------------|\n";
403 
404  str = ss.str();
405 }
406 
407 void checkSanity(const OrderBook& book)
408 {
409  static_cast<const Implementation::OrderBookInternal&>(book).checkSanity();
410 }
411 
412 
413 }}}}
PriceLevelCollections::ArrayConstEntry PriceLevelsConstEntry
Iterator over read-only collection of price levels.
Definition: EobiBook.h:135
ONIXS_EUREX_EMDI_API std::ostream & operator<<(std::ostream &stream, const EobiHandlerSettings &settings)
void * getUserPointer() const
returns kept user data pointer
Definition: EobiBook.cpp:228
QuantityType numberOfOrders() const
Total number of orders of given price.
Definition: EobiBook.cpp:123
bool bestAsk(Decimal &price, QuantityType &quantity) const
Definition: EobiBook.cpp:233
STL namespace.
std::string toString() const
Returns string representation of the instance.
Definition: EobiBook.cpp:179
bool empty() const
Indicates whether book has no bids & asks.
Definition: EobiBook.cpp:253
void checkSanity(const OrderBook &book)
checks whether the given book is properly built
Definition: EobiBook.cpp:407
bool isValid() const
Validity check.
bool bestBid(Decimal &price, QuantityType &quantity) const
Definition: EobiBook.cpp:238
size_t depth() const
Returns the maximum book depth.
Definition: EobiBook.cpp:243
Definition: Defines.h:30
Decimal type for better precision.
Definition: Numeric.h:63
std::string toString() const
String presentation of the book.
Definition: EobiBook.cpp:268
void printLevels(std::string &presentation, const PriceLevels &levels)
Definition: OrderBook.cpp:111
Int64 SecurityId
Alias for Security Id type.
Definition: Defines.h:51
std::string toShortString() const
Returns brief book info.
Definition: EobiBook.cpp:297
const Decimal & price() const
Price value.
Definition: EobiBook.cpp:103
PriceLevelCollections::Array PriceLevels
Sequence of price levels.
Definition: EobiBook.h:129
const PriceLevels & asks() const
Returns a set of descending bid prices for the given security.
Definition: EobiBook.cpp:258
const Decimal & getPrice(const PriceLevel &level)
Definition: EobiBook.cpp:166
bool operator()(const OrderInfo &oInf, OrderId id) const
Definition: EobiBook.cpp:150
void swap(PriceLevel &)
swap values
Definition: EobiBook.cpp:133
QuantityType quantity() const
Quantify for the given price.
Definition: EobiBook.cpp:113
std::string toFormattedString() const
Returns formatted presentation of the book.
Definition: EobiBook.cpp:315
UInt64 Quantity
Alias for Quantity type.
Definition: Defines.h:54
virtual ~OrderBook()
Destruction interface.
Definition: EobiBook.cpp:219
SecurityId securityId() const
Unique instrument Id as qualified.
Definition: EobiBook.cpp:248
void setUserPointer(void *pointer)
sets user data pointer
Definition: EobiBook.cpp:223
const PriceLevels & bids() const
Returns a set of ascending ask prices for the given security.
Definition: EobiBook.cpp:263
Encapsulates price level concept.
Definition: EobiBook.h:55
std::vector< OrderInfo > OrderInfos
Definition: EobiBook.h:52
const OrderInfos & orders() const
orders ids for a given level
Definition: EobiBook.cpp:128