Skip to content

Commit

Permalink
Move SimpleOrder-specific logic to SimpleOrderBook
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtheschmitzer committed Apr 8, 2013
1 parent e8a2a81 commit 557a4a6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
30 changes: 9 additions & 21 deletions src/book/depth_order_book.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ class DepthOrderBook : public OrderBook<OrderPtr> {
const DepthTracker& depth() const;

private:
FillId fill_id_;
DepthTracker depth_;
TypedBboListener* bbo_listener_;
TypedDepthListener* depth_listener_;
};

template <class OrderPtr, int SIZE>
DepthOrderBook<OrderPtr, SIZE>::DepthOrderBook()
: fill_id_(0),
bbo_listener_(NULL),
: bbo_listener_(NULL),
depth_listener_(NULL)
{
}
Expand All @@ -79,15 +77,14 @@ DepthOrderBook<OrderPtr, SIZE>::perform_callback(DobCallback& cb)
OrderBook<OrderPtr>::perform_callback(cb);
switch(cb.type) {
case DobCallback::cb_order_accept:
cb.order->accept();
// If the order is a limit order
if (cb.order->is_limit()) {
// If the order is completely filled on acceptance, do not modify
// depth unnecessarily
if (cb.match_qty == cb.order->order_qty()) {
if (cb.accept_match_qty == cb.order->order_qty()) {
// Don't tell depth about this order - it's going away immediately.
// Instead tell Depth about future fills to ignore
depth_.ignore_fill_qty(cb.match_qty, cb.order->is_buy());
depth_.ignore_fill_qty(cb.accept_match_qty, cb.order->is_buy());
} else {
// Add to bid or ask depth
depth_.add_order(cb.order->price(),
Expand Down Expand Up @@ -118,37 +115,28 @@ DepthOrderBook<OrderPtr, SIZE>::perform_callback(DobCallback& cb)
inbound_order_filled,
cb.order->is_buy());
}
// Increment fill ID once
++fill_id_;
// Update the orders
Cost fill_cost = cb.fill_qty * cb.fill_price;
cb.matched_order->fill(cb.fill_qty, fill_cost, fill_id_);
cb.order->fill(cb.fill_qty, fill_cost, fill_id_);
break;
}
case DobCallback::cb_order_cancel:
// If the order is a limit order
if (cb.order->is_limit()) {
// If the close erases a level
depth_.close_order(cb.order->price(),
cb.order->open_qty(),
cb.cxl_open_qty,
cb.order->is_buy());
}
cb.order->cancel();
break;

case DobCallback::cb_order_replace:
{
// Remember current values
Price current_price = cb.order->price();
Quantity current_qty = cb.order->open_qty();

// Modify the order itself
cb.order->replace(cb.new_order_qty, cb.new_price);
Quantity current_qty = cb.repl_curr_open_qty;
Quantity new_qty = current_qty + cb.repl_size_delta;

// Notify the depth
depth_.replace_order(current_price, cb.new_price,
current_qty, cb.order->open_qty(),
depth_.replace_order(cb.order->price(), cb.repl_new_price,
current_qty, new_qty,
cb.order->is_buy());
break;
}
Expand All @@ -165,11 +153,11 @@ DepthOrderBook<OrderPtr, SIZE>::perform_callback(DobCallback& cb)
(depth_.asks()->changed_since(last_change))) {
bbo_listener_->on_bbo_change(this, &depth_);
}

}
// Start tracking changes again...
depth_.published();
}
break;

default:
// Nothing
Expand Down
46 changes: 46 additions & 0 deletions src/impl/simple_order_book.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,54 @@ namespace liquibook { namespace impl {
// @brief binding of DepthOrderBook template with SimpleOrder* order pointer.
template <int SIZE = 5>
class SimpleOrderBook : public book::DepthOrderBook<SimpleOrder*, SIZE> {
public:
typedef book::Callback<SimpleOrder*> SimpleCallback;
typedef uint32_t FillId;

SimpleOrderBook();

// Override callback handling to update SimpleOrder state
virtual void perform_callback(SimpleCallback& cb);
private:
FillId fill_id_;
};

template <int SIZE>
SimpleOrderBook<SIZE>::SimpleOrderBook()
: fill_id_(0)
{
}

template <int SIZE>
inline void
SimpleOrderBook<SIZE>::perform_callback(SimpleCallback& cb)
{
book::DepthOrderBook<SimpleOrder*, SIZE>::perform_callback(cb);
switch(cb.type) {
case SimpleCallback::cb_order_accept:
cb.order->accept();
break;
case SimpleCallback::cb_order_fill: {
// Increment fill ID once
++fill_id_;
// Update the orders
book::Cost fill_cost = cb.fill_qty * cb.fill_price;
cb.matched_order->fill(cb.fill_qty, fill_cost, fill_id_);
cb.order->fill(cb.fill_qty, fill_cost, fill_id_);
break;
}
case SimpleCallback::cb_order_cancel:
cb.order->cancel();
break;
case SimpleCallback::cb_order_replace:
// Modify the order itself
cb.order->replace(cb.repl_size_delta, cb.repl_new_price);
break;
default:
// Nothing
break;
}
}
} }

#endif

0 comments on commit 557a4a6

Please sign in to comment.