@@ -39,39 +39,45 @@ void price_time_order_book::process_new_order(order_management::new_order o)
3939
4040bool 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
7783void 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;
0 commit comments