14 return static_cast<qty_t>(
mul_div_floor(
static_cast<uint64_t
>(a),
static_cast<uint64_t
>(b),
static_cast<uint64_t
>(c)));
26 allocations_.reserve(256);
29void ProRataMatchingEngine::allocate(
const slick::orderbook::detail::PriceLevelL3 &level,
qty_t qty,
OrderBook &book,
bool resolve) {
32 for (
const auto &book_order : level.orders) {
33 allocations_.push_back({book_order.order_id, book_order.quantity, 0,
34 resolve ? book.
findOrder(book_order.order_id) :
nullptr});
35 total += book_order.quantity;
40 for (
auto &a : allocations_) {
50 if (config_.fifo_percent > 0) {
51 qty_t fifo = floorLot(share_of(qty, config_.fifo_percent, 100));
53 for (
auto &a : allocations_) {
57 a.qty = std::min(fifo, a.size);
64 const qty_t pro_rata = left;
65 const qty_t base = total - (qty - left);
66 for (
auto &a : allocations_) {
67 qty_t share = floorLot(share_of(pro_rata, a.size - a.qty, base));
68 if (share < config_.min_allocation) {
78 for (
auto &a : allocations_) {
82 const qty_t extra = std::min(left, a.size - a.qty);
92 return std::make_tuple(*done, std::vector<TradeSummaryInfo>{});
95 MatchContext ctx{*
this, book, event_time, seq_num};
98 bool abandoned =
false;
100 while (order_qty > 0 && !abandoned) {
101 auto best_level = book.getBestLevel<
static_cast<slick::orderbook::Side
>(SIDE)>();
105 assert(!best_level->orders.empty() &&
"Price level has no orders");
107 const price_t level_price = best_level->price;
108 allocate(*best_level, order_qty, book, check_self_match);
111 if (check_self_match) [[unlikely]] {
114 bool cancelled =
false;
115 for (
const auto &a : allocations_) {
119 if (!
handleSelfMatch(*
this, order, a.order, book, request_time, event_time, seq_num, smp_mode)) {
130 for (
const auto &a : allocations_) {
135 auto *full_book_order = check_self_match ? a.order : book.
findOrder(a.order_id);
136 if (!
fillResting<SIDE>(ctx, order, a.order_id, full_book_order, level_price, a.qty, order_qty)) [[unlikely]] {
148std::vector<TradeSummaryInfo> ProRataMatchingEngine::matchFeed(uint64_t order_id,
price_t price,
qty_t &qty, OrderBook &book,
slick::sim::time_t event_time, uint64_t seq_num) {
149 MatchContext ctx{*
this, book, event_time, seq_num};
152 auto best_level = book.getBestLevel<
static_cast<slick::orderbook::Side
>(SIDE)>();
156 assert(!best_level->orders.empty() &&
"Price level has no orders");
158 const price_t level_price = best_level->price;
159 allocate(*best_level, qty, book,
false);
161 for (
const auto &a : allocations_) {
176 ? matchOrder<Side::SELL>(order, order_price, order_qty, book, request_time, event_time, seq_num, smp_mode)
177 : matchOrder<Side::BUY>(order, order_price, order_qty, book, request_time, event_time, seq_num, smp_mode);
182 ? matchFeed<Side::SELL>(order_id, price, qty, book, event_time, seq_num)
183 : matchFeed<Side::BUY>(order_id, price, qty, book, event_time, seq_num);
Order * findOrder(uint64_t id)
MatchingEngine(slick::queue< OrderResponse > &response_queue)
std::tuple< OrdRejectReason, std::vector< TradeSummaryInfo > > match(Order *order, price_t order_price, qty_t order_qty, OrderBook &book, time_t request_time, time_t event_time, uint64_t seq_num=0, SelfMatchPreventionMode smp_mode=SelfMatchPreventionMode::NONE) override
ProRataMatchingEngine(slick::queue< OrderResponse > &response_queue, const ProRataConfig &config={})
const ProRataConfig & config() const noexcept
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 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,...
Conversions between wire decimals with a constant exponent of -9 (CME PRICE9, ICE Decimal9) and the s...
bool isPriceBetterOrEqual(price_t a, price_t b)
uint64_t mul_div_floor(uint64_t a, uint64_t b, uint64_t c) noexcept
floor(a * b / c), with the product held at 128 bits.
constexpr price_t NULL_PRICE
Pro-rata allocation rules. Quantities use the venue's fixed-point scale.
size_t legs_hint
Legs a summary started at the current level may reach, aggressor included.
std::vector< TradeSummaryInfo > summaries