OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  8.1.0
API documentation
OrderBook.cpp
Go to the documentation of this file.
1 // Copyright 2002-2010 ONIX SOLUTIONS LIMITED. All rights reserved.
2 //
3 // This software owned by ONIX SOLUTIONS LIMITED [ONIXS] and is protected by copyright law
4 // and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable ONIXS Software
7 // Services Agreement (the Agreement) and Customer end user license agreements granting
8 // a non-assignable, non-transferable and non-exclusive license to use the software
9 // for it's own data processing purposes under the terms defined in the Agreement.
10 //
11 // Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
12 // of this source code or associated reference material to any other location for further reproduction
13 // or redistribution, and any amendments to this copyright notice, are expressly prohibited.
14 //
15 // Any reproduction or redistribution for sale or hiring of the Software not in accordance with
16 // the terms of the Agreement is a violation of copyright law.
17 //
18 #include <boost/format.hpp>
19 
21 
22 #include "OrderBookSnapshot.h"
23 #include "Utils.h"
24 #include "EditableOrderBook.h"
25 
26 
27 using namespace std;
28 using namespace OnixS::Util;
29 
30 namespace OnixS
31 {
32  namespace Eurex
33  {
34  namespace MarketData
35  {
36  PriceLevel::PriceLevel()
37  : price_()
38  , qty_(-1)
39  , ordersQty_(0)
40  {
41  }
42 
44  : price_(price)
45  , qty_(qty)
46  , ordersQty_(orderCount)
47  {
48  }
49 
50  void PriceLevel::toString(string& str) const
51  {
52  str << "PriceLevel [price=";
53 
54  price_.toString(str);
55 
56  str << ", quantity="
57  << qty_
58  << ", numberOfOrders="
59  << ordersQty_
60  << "]";
61  }
62 
63  bool isValid (const PriceLevel& level)
64  {
65  BOOST_ASSERT(level.quantity() >= level.numberOfOrders());
66 
67  return (level.quantity() != 0);
68  }
69 
71  {
72  return
73  l.quantity() == r.quantity() &&
74  l.numberOfOrders() == r.numberOfOrders() &&
75  l.price() == r.price()
76  ;
77  }
78 
79  std::ostream& operator<< (std::ostream& s, const OnixS::Eurex::MarketData::PriceLevel& lvl)
80  {
81  s << lvl.toString();
82  return s;
83  }
84 
86  SecurityId securityId, size_t bookDepth)
87  : securityId_(securityId)
88  , depth_(bookDepth)
89  {
90  }
91 
93  {
94  }
95 
96  void* OrderBook::operator new(size_t bookSize)
97  {
98  return malloc(bookSize);
99  }
100 
101  void OrderBook::operator delete(void* book)
102  {
103  return free(book);
104  }
105 
107  {
108  return new OrderBookSnapshot(*this);
109  }
110 
111  void printLevels(std::string& presentation, const PriceLevels& levels)
112  {
113  if (!levels.empty())
114  {
115  PriceLevelsConstEntry lastLevel = --levels.end();
116 
117  for (PriceLevelsConstEntry level = levels.begin();
118  level != levels.end();
119  ++level)
120  {
121  level->toString(presentation);
122 
123  if (level != lastLevel)
124  presentation << ", ";
125  }
126  }
127  }
128 
129  void OrderBook::toString(std::string& presentation) const
130  {
131  presentation
132  << "Book [securityId="
133  << securityId_
134  << ", depth="
135  << depth_;
136 
137  presentation << ", bids: ";
138  printLevels(presentation, bids());
139 
140  presentation << ", asks: ";
141  printLevels(presentation, asks());
142 
143  presentation << ']';
144  }
145 
146  namespace
147  {
148  void bestBid2string(string& str, const Decimal& price, Quantity quantity)
149  {
150  str += (boost::format("%19d %20.4f") % quantity % static_cast<double>(price)).str();
151  }
152 
153  void bestAsk2string(string& str, const Decimal& price, Quantity quantity)
154  {
155  str += (boost::format("%20.4f %9d")
156  % static_cast<double>(price)
157  % quantity).str();
158  }
159 
160  void bid2string(string& str, const PriceLevel& bid)
161  {
162  str += (boost::format("%9d %9d %20.4f")
163  % bid.numberOfOrders()
164  % bid.quantity()
165  % (double)bid.price()).str();
166  }
167 
168  void ask2string(string& str, const PriceLevel& ask)
169  {
170  str += (boost::format("%20.4f %9d %9d ")
171  % static_cast<double>(ask.price())
172  % ask.quantity() % ask.numberOfOrders()).str();
173  }
174  };
175 
176  void OrderBook::toFormattedString(std::string& formattedStr) const
177  {
178  const string briefInfo = toShortString();
179 
180  formattedStr << briefInfo;
181 
182  formattedStr <<
183  " PriceLevel |"
184  " BID: OrderCount Quantity Price |"
185  " ASK: Price Quantity OrderCount \n";
186 
187  Decimal bestBidPrice;
188  Quantity bestBidQuantity = 0;
189  const bool bestBidAvailable = bestBid(bestBidPrice, bestBidQuantity);
190 
191  Decimal bestAskPrice;
192  Quantity bestAskQuantity = 0;
193  const bool bestAskAvailable = bestAsk(bestAskPrice, bestAskQuantity);
194 
195  if( bestBidAvailable || bestAskAvailable )
196  {
197  formattedStr
198  << briefInfo
199  << " "
200  << "-"
201  << " | ";
202 
203  bestBid2string(formattedStr, bestBidPrice, bestBidQuantity);
204 
205  formattedStr << " | ";
206 
207  bestAsk2string(formattedStr, bestAskPrice, bestAskQuantity);
208 
209  formattedStr << "\n";
210 
211  formattedStr
212  << briefInfo
213  << " "
214  << "-"
215  << " | ";
216 
217  formattedStr << "---------------------------------------------------------------------------------\n";
218  }
219 
220 
221  static const PriceLevel zeroPriceLevel(0, 0, 0);
222  for (size_t level = 0; level < depth_; ++level)
223  {
224  formattedStr
225  << briefInfo
226  << ((level < 9) ? " " : " ")
227  << level + 1
228  << " | ";
229 
230  bid2string(
231  formattedStr,
232  level < bids().size()
233  ? bids().at(level) : zeroPriceLevel);
234 
235  formattedStr << " | ";
236 
237  ask2string(
238  formattedStr,
239  level < asks().size()
240  ? asks().at(level) : zeroPriceLevel);
241 
242  if (level + 1 != depth_)
243  formattedStr << "\n";
244  }
245  }
246 
247  void OrderBook::toShortString(std::string& presentation) const
248  {
249  presentation << "Book ["
250  << securityId_
251  << ", depth=" << depth_ << "]";
252  }
253 
254  void checkSanity(const OrderBook& book)
255  {
256  static_cast<const Implementation::EditableOrderBook&>(book).checkSanity();
257  }
258  }
259  }
260 }
261 
void checkSanity(const OrderBook &book)
checks whether the given book is properly built
Definition: OrderBook.cpp:254
OrderBook * snapshot() const
Creates immutable snapshot of the book.
Definition: OrderBook.cpp:106
virtual bool bestBid(Decimal &price, Quantity &quantity) const =0
Returns best implied bid.
bool operator==(const FieldValueRef &ref, const std::string &str)
Quantity numberOfOrders() const
Total number of orders of given price.
Definition: OrderBook.h:100
Encapsulates price level concept.
Definition: OrderBook.h:33
virtual const PriceLevels & asks() const =0
Returns a set of descending bid prices for the given security.
STL namespace.
OrderBook(SecurityId securityId, size_t bookDepth)
Definition: OrderBook.cpp:85
Decimal price() const
Price value.
Definition: OrderBook.h:80
Definition: Defines.h:30
Decimal type for better precision.
Definition: Numeric.h:63
std::string toString() const
Returns string representation of the instance.
Definition: OrderBook.h:110
PriceLevelCollections::Array PriceLevels
Sequence of price levels.
Definition: OrderBook.h:135
void printLevels(std::string &presentation, const PriceLevels &levels)
Definition: OrderBook.cpp:111
Int64 SecurityId
Alias for Security Id type.
Definition: Defines.h:51
bool isValid(const PriceLevel &level)
Definition: OrderBook.cpp:63
std::string toFormattedString() const
Returns formatted presentation of the book.
Definition: OrderBook.h:267
PriceLevel()
Default initialization.
Definition: OrderBook.cpp:36
virtual ~OrderBook()
Destruction interface.
Definition: OrderBook.cpp:92
void toString(std::string &) const
Appends text presentation to given string.
Definition: Decimal.cpp:217
ONIXS_EUREX_EMDI_API std::ostream & operator<<(std::ostream &stream, const FeedEngineSettings &settings)
virtual const PriceLevels & bids() const =0
Returns a set of ascending ask prices for the given security.
PriceLevelCollections::ArrayConstEntry PriceLevelsConstEntry
Iterator over read-only collection of price levels.
Definition: OrderBook.h:141
std::string toString() const
String presentation of the book.
Definition: OrderBook.h:249
Quantity quantity() const
Quantify for the given price.
Definition: OrderBook.h:90
UInt64 Quantity
Alias for Quantity type.
Definition: Defines.h:54
virtual bool bestAsk(Decimal &price, Quantity &quantity) const =0
Returns best implied ask.
std::string toShortString() const
Returns brief book info.
Definition: OrderBook.h:258