Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
fifo_matching_engine.cpp
Go to the documentation of this file.
2#include "match_common.hpp"
3
4using namespace slick::sim::engine;
5using namespace slick::sim::engine::detail;
6using namespace slick::sim;
7using namespace slick::sim::utils;
8
10 slick::queue<OrderResponse> &response_queue
11)
12 : MatchingEngine(response_queue)
13{
14}
15
16namespace {
17
18template<Side SIDE>
19std::tuple<OrdRejectReason, std::vector<TradeSummaryInfo>> matchOrder(MatchingEngine& engine, Order* order, price_t order_price, qty_t order_qty, OrderBook &book, slick::sim::time_t request_time, slick::sim::time_t event_time, uint64_t seq_num, SelfMatchPreventionMode smp_mode) {
20 if (auto done = prepareOrder<SIDE, Reach::Queue>(engine, order, order_price, order_qty, book, request_time, smp_mode)) {
21 return std::make_tuple(*done, std::vector<TradeSummaryInfo>{});
22 }
23
24 // Matching loop with SMP
25 MatchContext ctx{engine, book, event_time, seq_num};
26 order_qty = order->leaves_quantity; // Use leaves quantity for matching
27
28 while (order_qty > 0) {
29 auto best_level = book.getBestLevel<static_cast<slick::orderbook::Side>(SIDE)>();
30
31 // Check if we should continue matching at this level
32 if (!best_level || (order_price != NULL_PRICE && !isPriceBetterOrEqual<SIDE>(best_level->price, order_price))) {
33 break;
34 }
35
36 // Refresh orders reference each time (level may have been modified)
37 auto& orders = best_level->orders;
38 assert(!orders.empty() && "Price level has no orders");
39
40 // FIFO: always the front of the queue
41 auto &book_order = *orders.begin();
42
43 // Check for a self-match
44 auto* full_book_order = book.findOrder(book_order.order_id);
45 if (smp_mode != SelfMatchPreventionMode::NONE && isSelfMatch(order, full_book_order)) [[unlikely]] {
46 // Handle self-match according to Symbol's SMP mode
47 bool should_continue = handleSelfMatch(engine, order, full_book_order, book,
48 request_time, event_time, seq_num, smp_mode);
49
50 if (!should_continue) [[unlikely]] {
51 // CANCEL_NEWEST backstop: prepareOrder should already have rejected this.
52 // No cancel is published — the caller emits the one terminal REJECTED.
53 return std::make_tuple(OrdRejectReason::SMP, std::move(ctx.summaries));
54 }
55
56 // CANCEL_RESTING mode: resting order was deleted, continue to next order
57 continue;
58 }
59
60 qty_t trade_qty = std::min<qty_t>(order_qty, book_order.quantity);
61 order_qty -= trade_qty;
62 if (!fillResting<SIDE>(ctx, order, book_order.order_id, full_book_order, best_level->price, trade_qty, order_qty)) [[unlikely]] {
63 break;
64 }
65 }
66
67 cancelUnrestableRemainder(engine, order, order_qty, request_time);
68 return std::make_tuple(OrdRejectReason::NONE, std::move(ctx.summaries));
69}
70
71template<Side SIDE>
72std::vector<TradeSummaryInfo> matchFeed(MatchingEngine& engine, uint64_t order_id, price_t price, qty_t &qty, OrderBook &book, slick::sim::time_t event_time, uint64_t seq_num) {
73 MatchContext ctx{engine, book, event_time, seq_num};
74
75 while (qty > 0) {
76 auto best_level = book.getBestLevel<static_cast<slick::orderbook::Side>(SIDE)>();
77
78 // Check if we should continue matching at this level
79 if (!best_level || (price != NULL_PRICE && !isPriceBetterOrEqual<SIDE>(best_level->price, price))) {
80 break;
81 }
82
83 // Refresh orders reference each time (level may have been modified)
84 auto& orders = best_level->orders;
85 assert(!orders.empty() && "Price level has no orders");
86
87 auto &book_order = *orders.begin();
88 if (!fillRestingFromFeed<SIDE>(ctx, order_id, book_order.order_id, best_level->price,
89 std::min<qty_t>(qty, book_order.quantity), qty)) [[unlikely]] {
90 break;
91 }
92 }
93
94 return std::move(ctx.summaries);
95}
96
97} // namespace
98
99std::tuple<OrdRejectReason, std::vector<TradeSummaryInfo>> FifoMatchingEngine::match(Order* order, price_t order_price, qty_t order_qty, OrderBook &book, slick::sim::time_t request_time, slick::sim::time_t event_time, uint64_t seq_num, SelfMatchPreventionMode smp_mode) {
100 return order->side == Side::BUY
101 ? matchOrder<Side::SELL>(*this, order, order_price, order_qty, book, request_time, event_time, seq_num, smp_mode)
102 : matchOrder<Side::BUY>(*this, order, order_price, order_qty, book, request_time, event_time, seq_num, smp_mode);
103}
104
105std::vector<TradeSummaryInfo> FifoMatchingEngine::match(Side side, uint64_t order_id, price_t price, qty_t &qty, OrderBook &book, slick::sim::time_t event_time, uint64_t seq_num) {
106 return side == Side::BUY
107 ? matchFeed<Side::SELL>(*this, order_id, price, qty, book, event_time, seq_num)
108 : matchFeed<Side::BUY>(*this, order_id, price, qty, book, event_time, seq_num);
109}
Order * findOrder(uint64_t id)
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
FifoMatchingEngine(slick::queue< OrderResponse > &response_queue)
MatchingEngine(slick::queue< OrderResponse > &response_queue)
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...
Definition decimal.hpp:9
bool isPriceBetterOrEqual(price_t a, price_t b)
Definition price.hpp:17
constexpr price_t NULL_PRICE
Definition types.hpp:28
uint_fast64_t time_t
Definition types.hpp:23
SelfMatchPreventionMode
Definition order.hpp:233
int_fast64_t qty_t
Definition types.hpp:22
int_fast64_t price_t
Definition types.hpp:21
qty_t leaves_quantity
Definition order.hpp:316
State one match() call carries through its fills.
std::vector< TradeSummaryInfo > summaries