Skip to content

Commit 3614555

Browse files
committed
Bring the PT orderbook in line with reality
1 parent 9b42e19 commit 3614555

File tree

2 files changed

+64
-37
lines changed

2 files changed

+64
-37
lines changed

src/examples/trading/server/price_time_order_book.cpp

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,45 @@ void price_time_order_book::process_new_order(order_management::new_order o)
3939

4040
bool price_time_order_book::find_match(order_management::new_order& o)
4141
{
42-
auto& other_side = (o.side == order_side::buy) ? sell_orders_ : buy_orders_;
43-
44-
// Is there anything at all to match on the other side?
45-
if (other_side.empty())
46-
return false;
47-
48-
// Do we cross the order on the other side of the book?
49-
order passive_order = other_side.top();
50-
if ((o.side == order_side::buy && o.price < passive_order.price)
51-
|| (o.side == order_side::sell && o.price > passive_order.price))
52-
return false;
53-
54-
// Work out how much is going to trade and deduct from both orders.
55-
unsigned int trade_quantity = std::min(o.quantity, passive_order.quantity);
56-
o.quantity -= trade_quantity;
57-
passive_order.quantity -= trade_quantity;
58-
59-
// If the passive order has no remaining quantity, permanently remove it from
60-
// the book. Otherwise, reenter it into the book with its updated quantity.
61-
other_side.pop();
62-
if (passive_order.quantity > 0)
63-
other_side.push(passive_order);
64-
65-
// Dispatch the trade to the market data bus.
66-
market_data::trade md_trade;
67-
md_trade.sequence_number = 0;
68-
md_trade.symbol = symbol();
69-
md_trade.aggressor_side = o.side;
70-
md_trade.price = passive_order.price;
71-
md_trade.quantity = trade_quantity;
72-
market_data_bus_.dispatch_event(md_trade);
73-
74-
return true;
42+
const auto impl = [&]<typename Side>(Side other_side) -> bool
43+
{
44+
// Is there anything at all to match on the other side?
45+
if (other_side.empty())
46+
return false;
47+
48+
// Do we cross the order on the other side of the book?
49+
order passive_order = other_side.top();
50+
if ((o.side == order_side::buy && o.price < passive_order.price)
51+
|| (o.side == order_side::sell && o.price > passive_order.price))
52+
return false;
53+
54+
// Work out how much is going to trade and deduct from both orders.
55+
unsigned int trade_quantity = std::min(o.quantity, passive_order.quantity);
56+
o.quantity -= trade_quantity;
57+
passive_order.quantity -= trade_quantity;
58+
59+
// If the passive order has no remaining quantity, permanently remove it from
60+
// the book. Otherwise, reenter it into the book with its updated quantity.
61+
other_side.pop();
62+
if (passive_order.quantity > 0)
63+
other_side.push(passive_order);
64+
65+
// Dispatch the trade to the market data bus.
66+
market_data::trade md_trade;
67+
md_trade.sequence_number = 0;
68+
md_trade.symbol = symbol();
69+
md_trade.aggressor_side = o.side;
70+
md_trade.price = passive_order.price;
71+
md_trade.quantity = trade_quantity;
72+
market_data_bus_.dispatch_event(md_trade);
73+
74+
return true;
75+
};
76+
77+
if (o.side == order_side::buy)
78+
return impl(sell_orders_);
79+
else
80+
return impl(buy_orders_);
7581
}
7682

7783
void price_time_order_book::add_order(const order_management::new_order& o)
@@ -81,8 +87,11 @@ void price_time_order_book::add_order(const order_management::new_order& o)
8187
passive_order.price = o.price;
8288
passive_order.time = next_time_++;
8389
passive_order.quantity = o.quantity;
84-
auto& this_side = (o.side == order_side::buy) ? buy_orders_ : sell_orders_;
85-
this_side.push(passive_order);
90+
91+
if (o.side == order_side::buy)
92+
buy_orders_.push(passive_order);
93+
else
94+
sell_orders_.push(passive_order);
8695

8796
// Dispatch the new order to the market data bus.
8897
market_data::new_order md_new_order;

src/examples/trading/server/price_time_order_book.hpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,29 @@ class price_time_order_book : public order_book
4949
bool operator<(const order& other) const;
5050
};
5151

52+
struct buy_comparator
53+
{
54+
bool operator() (const order& o1, const order& o2)
55+
{
56+
if (o1.price > o2.price) return true;
57+
if (o1.price < o2.price) return false;
58+
return (o1.time < o2.time);
59+
}
60+
};
61+
62+
struct sell_comparator
63+
{
64+
bool operator() (const order& o1, const order& o2)
65+
{
66+
return std::tie(o1.price,o1.time) < std::tie(o2.price,o2.time);
67+
}
68+
};
69+
5270
std::experimental::strand<std::experimental::system_executor> strand_;
5371
market_data_bus& market_data_bus_;
5472
std::uint64_t next_time_;
55-
std::priority_queue<order> buy_orders_;
56-
std::priority_queue<order> sell_orders_;
73+
std::priority_queue<order, std::vector<order>, buy_comparator> buy_orders_;
74+
std::priority_queue<order, std::vector<order>, sell_comparator> sell_orders_;
5775
};
5876

5977
#endif

0 commit comments

Comments
 (0)