4#include <slick/logger.hpp>
26 if (book_order ==
nullptr) {
42 uint64_t event_time, uint64_t seq_num,
55 LOG_INFO(
"{} SMP: Canceling resting order {} (client_id={})",
57 book.
deleteOrder(book_order, event_time, seq_num,
false);
58 engine.publishOrderCancel(book_order, request_time);
65 LOG_INFO(
"{} SMP: Rejecting new order {} due to self-match with resting order {} (client_id={})",
81template<S
ide SIDE, Reach REACH>
83 qty_t remaining = order_qty;
85 const auto& levels = book.getLevelsL3(
static_cast<slick::orderbook::Side
>(SIDE));
86 for (
const auto& [level_price, level_data] : levels) {
87 if (remaining <= 0) [[unlikely]] {
94 for (
const auto& book_order : level_data.orders) {
96 if (remaining <= 0) [[unlikely]] {
104 remaining -= std::min<qty_t>(book_order.quantity, remaining);
109 remaining -= std::min<qty_t>(level_data.total_quantity, remaining);
121 qty_t remaining_check_qty = order_qty;
129 const auto& levels = book.getLevelsL3(
static_cast<slick::orderbook::Side
>(SIDE));
132 for (
const auto& [level_price, level_data] : levels) {
139 for (
const auto& book_order : level_data.orders) {
149 LOG_INFO(
"{} FOK check: Skipping self-match with resting order {} due to SMP (CANCEL_RESTING mode)",
155 remaining_check_qty -= std::min<qty_t>(book_order.quantity, remaining_check_qty);
157 if (remaining_check_qty == 0) [[likely]] {
166 return remaining_check_qty == 0;
173template<S
ide SIDE, Reach REACH>
189 effective_qty += order_qty - order->
quantity;
190 if (effective_qty < 0) {
196 LOG_INFO(
"{} Order {} rejected due to SMP (CANCEL_NEWEST mode)", order->
symbol.
view(), order->
id);
202 engine.publishOrderAck(order, request_time);
208 engine.publishOrderCancel(order, request_time);
209 LOG_INFO(
"{} FOK order {} cannot be completely filled, canceling", order->
symbol.
view(), order->
id);
217 order->
price = order_price;
223 engine.publishOrderModify(order, order_price, order_qty, request_time);
227 LOG_INFO(
"{} Order {} has zero leaves quantity after modification, canceling", order->
symbol.
view(), order->
id);
228 engine.publishOrderCancel(order, request_time);
260 summary.
price = price;
261 summary.
qty = trade_qty;
267 summary.
Trades.emplace_back(
Trade{aggressor_id, trade_qty});
268 summary.
Trades.emplace_back(
Trade{book_order_id, trade_qty});
270 ctx.
summaries.emplace_back(std::move(summary));
273 auto &last_summary = ctx.
summaries.back();
274 last_summary.num_orders += 1;
275 last_summary.qty += trade_qty;
276 last_summary.phantom_qty += phantom_qty;
277 last_summary.Trades.front().qty += trade_qty;
278 last_summary.Trades.emplace_back(
Trade{book_order_id, trade_qty});
290 LOG_INFO(
"{} Matching order {} with book order {} for qty {} at price {}", order->
symbol.
view(), order->
id, book_order_id, trade_qty, price);
299 LOG_ERROR(
"{} book rejected execution of order {} for qty {} at price {} (seq_num={}); abandoning match",
311 if (full_book_order) [[unlikely]] {
333 LOG_TRACE(
" {} Matching {} with book order {} for qty {} at price {}", ctx.
book.
symbolName().
view(),
334 aggressor_id, book_order_id, trade_qty, price);
347 LOG_ERROR(
"{} book rejected execution of order {} for qty {} at price {} (seq_num={}); abandoning match with {} unfilled",
354 LOG_INFO(
"{} order {} filled {}@{}, leaves_qty={}, executed_qty={}, avg_fill_price={}", our_order->symbol.view(), our_order->order_id.view(),
to_qty_double(trade_qty),
to_price_double(price),
to_qty_double(our_order->leaves_quantity),
to_qty_double(our_order->cum_quantity),
to_price_double(our_order->avg_fill_price));
355 if (our_order->leaves_quantity == 0) {
369 engine.publishOrderCancel(order, request_time);
370 LOG_INFO(
"{} Order {} cannot rest and was not completely filled, canceling remaining qty {}", order->
symbol.
view(), order->
id, remaining);
Order * findOrder(uint64_t id)
const symbol_name_t & symbolName() const noexcept
Wire-ready, so publishers copy it as a fixed block instead of reading sizeof(field) bytes out of a sh...
bool executeOrder(slick::orderbook::OrderId order_id, slick::orderbook::Quantity executed_quantity, uint64_t timestamp, uint64_t seq_num=0, bool is_last_in_batch=true) override
void deleteOrder(Order *order, uint64_t timestamp, uint64_t seq_num=0, bool is_last_in_batch=true)
void freeOrder(Order *order)
void publishOrderExecution(const Order *order, bool aggressor)
aggressor: the order crossed the spread, as opposed to resting and being hit.
static uint64_t nextTradeId() noexcept
std::string_view view() const noexcept
Reach
How far into a price level an aggressor reaches.
void recordTrade(MatchContext &ctx, uint64_t aggressor_id, uint64_t book_order_id, price_t price, qty_t trade_qty, qty_t phantom_qty)
Adds one fill to the trade summaries, which are aggregated per price level: a new summary when the pr...
bool isSelfMatch(const Order *incoming_order, const Order *book_order)
bool fillRestingFromFeed(MatchContext &ctx, uint64_t aggressor_id, uint64_t book_order_id, price_t price, qty_t trade_qty, qty_t &qty)
One fill of market-data liquidity against a resting order.
void cancelUnrestableRemainder(MatchingEngine &engine, const Order *order, qty_t remaining, slick::sim::time_t request_time)
What cannot rest is cancelled: an IOC's remainder, and a market order's, which would otherwise rest a...
bool canFillCompletely(const Order *order, price_t order_price, qty_t order_qty, OrderBook &book, SelfMatchPreventionMode smp_mode)
bool handleSelfMatch(MatchingEngine &engine, const Order *incoming_order, Order *book_order, OrderBook &book, slick::sim::time_t request_time, uint64_t event_time, uint64_t seq_num, SelfMatchPreventionMode smp_mode)
bool fillResting(MatchContext &ctx, Order *order, uint64_t book_order_id, Order *full_book_order, price_t price, qty_t trade_qty, qty_t remaining)
One fill of a client order against a resting order.
std::optional< OrdRejectReason > prepareOrder(MatchingEngine &engine, Order *order, price_t order_price, qty_t order_qty, OrderBook &book, slick::sim::time_t request_time, SelfMatchPreventionMode smp_mode)
Everything a client order goes through before it matches: the CANCEL_NEWEST pre-check,...
bool wouldSelfMatch(const Order *order, price_t order_price, qty_t order_qty, OrderBook &book)
bool isPriceBetterOrEqual(price_t a, price_t b)
void executeOrder(Order *order, price_t price, qty_t executed_quantity, time_t timestamp)
constexpr Side opposite_side()
constexpr price_t NULL_PRICE
constexpr double to_price_double(price_t price)
constexpr double to_qty_double(qty_t qty)
utils::fixed_string< 32 > symbol
utils::fixed_string< 37 > order_id
bool restsRemainder() const noexcept
Whether what matching leaves of it rests on the book; otherwise it is cancelled.
TimeInForce time_in_force
std::vector< Trade > Trades
qty_t phantom_qty
Portion of qty that was matched against phantom (market-mirroring) liquidity rather than the simulato...
State one match() call carries through its fills.
size_t legs_hint
Legs a summary started at the current level may reach, aggressor included.
std::vector< TradeSummaryInfo > summaries
slick::sim::time_t event_time