22 #include <util/String.h> 27 #include <boost/foreach.hpp> 29 ONIXS_HANDLER_NAMESPACE_BEGIN
32 bookDepth_(bookDepth),
33 bookAllocator_(bookAllocator)
35 BOOST_ASSERT(bookAllocator_!=
nullptr);
40 bookDepth_(other.bookDepth_),
41 bookAllocator_(other.bookAllocator_)
43 BOOST_ASSERT(bookAllocator_!=
nullptr);
48 BOOST_ASSERT(bookAllocator_!=
nullptr);
49 void* location = bookAllocator_->
alloc();
50 BOOST_ASSERT(location !=
nullptr);
52 if(location ==
nullptr)
53 throw std::logic_error(
"book memory exhausted.");
55 return boost::shared_ptr<OrderBookInternal>(
new(location)
OrderBookInternal(
id, bookDepth_, bookDepth_));
60 OnixS::HandlerCore::MarketData::FullOrderDepthBookCreator<OrderBookInternal, false>::reset();
65 onOrderBookUpdated_(onOrderBookUpdated),
66 snapshotRecoveryInProgress_(false)
68 BOOST_ASSERT(bookAllocator !=
nullptr);
69 BOOST_ASSERT(bookDepth > 0);
74 snapshotRecoveryInProgress_ =
true;
79 snapshotRecoveryInProgress_ =
false;
81 BOOST_FOREACH(
BookPtr ptr, books_)
84 onOrderBookUpdated_(*ptr);
90 BOOST_ASSERT(impl_.get() !=
nullptr);
99 BOOST_ASSERT(checkOrderExistence(bookPtr->askOrders().orders(), message.
orderId(),
false));
104 BOOST_ASSERT(checkOrderExistence(bookPtr->bidOrders().orders(), message.
orderId(),
false));
110 books_.insert(bookPtr);
112 onBookChanged(*bookPtr);
117 struct FindById :
public std::binary_function<const Order*, OrderId, bool>
121 BOOST_ASSERT(order !=
nullptr);
122 return (order->
id_ ==
id);
127 template <
typename OrderList>
128 bool OrderBookHolder::checkOrderExistence(
const OrderList& list,
OrderId orderId,
bool shouldExist)
130 typename OrderList::const_iterator pos = std::find_if(list.begin(), list.end(), std::bind2nd(
FindById(), orderId));
133 if(pos == list.end())
134 throw OperationException (__FUNCTION__, OnixS::Util::format(
"Order does not exist, %llu", orderId).c_str());
137 if(pos != list.end())
138 throw OperationException (__FUNCTION__, OnixS::Util::format(
"Order already exists, %llu", orderId).c_str());
146 if(std::find_if(bookPtr->askOrders().orders().begin(), bookPtr->askOrders().orders().end(), std::bind2nd(
FindById(), orderId)) != bookPtr->askOrders().orders().end())
148 else if(std::find_if(bookPtr->bidOrders().orders().begin(), bookPtr->bidOrders().orders().end(), std::bind2nd(
FindById(), orderId)) != bookPtr->bidOrders().orders().end())
151 throw OperationException (__FUNCTION__, OnixS::Util::format(
"Unknown side for order, %llu", orderId).c_str());
156 BOOST_ASSERT(!snapshotRecoveryInProgress_);
158 BOOST_ASSERT(impl_.get() !=
nullptr);
169 BOOST_ASSERT(checkOrderExistence(bookPtr->askOrders().orders(), orderId,
true));
171 const Order& order = bookPtr->askOrders().find(orderId);
175 bookPtr->askOrders().remove(orderId);
180 bookPtr->askOrders().update(
Order(orderId, price, newQuantity));
184 BOOST_ASSERT(checkOrderExistence(bookPtr->bidOrders().orders(), orderId,
true));
186 const Order& order = bookPtr->bidOrders().find(orderId);
190 bookPtr->bidOrders().remove(orderId);
195 bookPtr->bidOrders().update(
Order(orderId, price, newQuantity));
200 onBookChanged(*bookPtr);
205 BOOST_ASSERT(!snapshotRecoveryInProgress_);
206 BOOST_ASSERT(impl_.get() !=
nullptr);
220 BOOST_ASSERT(checkOrderExistence(bookPtr->askOrders().orders(), orderId,
true));
222 const Order& order = bookPtr->askOrders().find(orderId);
226 bookPtr->askOrders().remove(orderId);
231 bookPtr->askOrders().update(
Order(orderId, price, newQuantity));
235 BOOST_ASSERT(checkOrderExistence(bookPtr->bidOrders().orders(), orderId,
true));
237 const Order& order = bookPtr->bidOrders().find(orderId);
241 bookPtr->bidOrders().remove(orderId);
246 bookPtr->bidOrders().update(
Order(orderId, price, newQuantity));
251 onBookChanged(*bookPtr);
256 BOOST_ASSERT(!snapshotRecoveryInProgress_);
257 BOOST_ASSERT(impl_.get() !=
nullptr);
265 const Side::Enum side = getOrderSide(bookPtr, orderId);
270 bookPtr->askOrders().find(orderId).price_;
272 bookPtr->askOrders().remove(orderId);
273 bookPtr->askOrders().update(
Order(orderId, price, message.
quantity()));
278 bookPtr->bidOrders().find(orderId).price_;
280 bookPtr->bidOrders().remove(orderId);
281 bookPtr->bidOrders().update(
Order(orderId, price, message.
quantity()));
284 onBookChanged(*bookPtr);
289 BOOST_ASSERT(!snapshotRecoveryInProgress_);
290 BOOST_ASSERT(impl_.get() !=
nullptr);
301 BOOST_ASSERT(checkOrderExistence(bookPtr->askOrders().orders(), orderId,
true));
302 bookPtr->askOrders().remove(orderId);
306 BOOST_ASSERT(checkOrderExistence(bookPtr->bidOrders().orders(), orderId,
true));
307 bookPtr->bidOrders().remove(orderId);
312 onBookChanged(*bookPtr);
317 BOOST_ASSERT(impl_.get() !=
nullptr);
319 BOOST_FOREACH(
BookPtr ptr, books_)
330 BOOST_FOREACH(
BookPtr ptr, books_)
333 onOrderBookOutOfDate(*ptr);
339 if (!snapshotRecoveryInProgress_)
340 onOrderBookUpdated_(book);
343 ONIXS_HANDLER_NAMESPACE_END
OrderBookId orderBookId() const
The Order Book ID.
OrderBookRegistry< OrderBookId, OrderBookInternal, OrderBookCreator > BookRegistry
UInt64 OrderId
Alias for OrderId type.
OrderBookHolder(const OnOrderBookUpdatedCallback &onOrderBookUpdated, OrderBookAllocator *bookAllocator, size_t bookDepth=DefaultBookDepth)
OnixS::HandlerCore::MarketData::FullOrderDepthBookCreator< OrderBookInternal, false > FullOrderDepthBookCreator
void onOrderDelete(const OrderDeleteMsg &)
Quantity executedQuantity() const
The quantity being executed.
Side::Enum side() const
The type of order being added.
OrderBookId orderBookId() const
The Order Book ID.
Quantity quantity() const
The new visible quantity of the order.
OrderBookId orderBookId() const
The Order Book ID.
Price price() const
The display price of the new order.
BookRegistry::BookPtr BookPtr
UInt64 Quantity
Alias for Quantity type.
UInt32 OrderBookId
Alias for Security Id type.
void onOrderExecuted(const OrderExecutedMsg &)
Side::Enum side() const
The type of order being added.
void onBookOutOfDate(const boost::function< void(const OrderBookInternal &)> &onOrderBookOutOfDate)
Side::Enum side() const
The type of order being added.
void snapshotRecoveryFinished()
OrderId orderId() const
The identifier assigned to the new order.
OrderBookCreator(size_t bookDepth, OrderBookAllocator *bookAllocator)
Quantity executedQuantity() const
The quantity being executed.
boost::function< void(const OrderBookInternal &)> OnOrderBookUpdatedCallback
void onOrderAdd(const AddOrderMsg &)
void onOrderExecutedWithPrice(const OrderExecutedWithPriceMsg &)
Printable::Enum printable() const
OrderId orderId() const
The order ID is associated with the executed order.
void snapshotRecoveryStarted()
OrderBookId orderBookId() const
The Order Book ID.
OrderId orderId() const
The order ID is associated with the executed order.
Quantity quantity() const
The visible quantity of the order.
void onOrderReplace(const OrderReplaceMsg &)
OrderId orderId() const
The ID of the order being deleted.
Side::Enum side() const
The type of order being added.
boost::shared_ptr< OrderBookInternal > operator()(const OrderBookId &id) const
OrderBookId orderBookId() const
The Order Book ID.