4#include <slick/logger.hpp>
19uint8_t read_percent(
const nlohmann::json ¶ms,
const char *key) {
20 const auto it = params.find(key);
21 if (it == params.end()) {
24 if (it->is_number_integer()) {
25 const auto v = it->get<int64_t>();
26 if (v >= 0 && v <= 100) {
27 return static_cast<uint8_t
>(v);
30 throw std::invalid_argument(std::format(
"pro_rata.{} must be an integer 0-100, got {}", key, it->dump()));
33qty_t read_qty(
const nlohmann::json ¶ms,
const char *key,
double fallback) {
34 constexpr double kMax =
static_cast<double>(std::numeric_limits<qty_t>::max() /
DOUBLE_MULTIPLIER);
35 const auto it = params.find(key);
36 if (it == params.end()) {
39 if (it->is_number() && std::isfinite(it->get<
double>()) && std::fabs(it->get<
double>()) <= kMax) {
42 throw std::invalid_argument(std::format(
"pro_rata.{} must be a number within +/-{}, got {}", key, kMax, it->dump()));
49 throw std::invalid_argument(std::format(
"pro_rata.lot_size must be positive, got {}",
to_qty_double(
lot_size)));
52 throw std::invalid_argument(std::format(
"pro_rata.min_allocation must not be negative, got {}",
56 throw std::invalid_argument(std::format(
"pro_rata.fifo_percent must be 0-100, got {}",
fifo_percent));
63 const auto algorithm = config.value(
"matching_algorithm", std::string(
"fifo"));
64 if (algorithm ==
"fifo") {
65 if (config.contains(
"pro_rata")) {
66 LOG_WARN(
"pro_rata settings ignored: matching_algorithm is fifo");
70 if (algorithm !=
"pro_rata") {
71 throw std::invalid_argument(std::format(
72 "unknown matching_algorithm '{}'; expected fifo or pro_rata", algorithm));
76 const auto params = config.value(
"pro_rata", nlohmann::json::object());
77 if (!params.is_object()) {
78 throw std::invalid_argument(std::format(
"pro_rata must be an object, got {}", params.dump()));
83 }
catch (
const nlohmann::json::exception &e) {
84 throw std::invalid_argument(std::format(
"bad matching config: {}", e.what()));
91 slick::queue<OrderResponse> &response_queue) {
93 return std::make_unique<ProRataMatchingEngine>(response_queue, config.
pro_rata);
95 return std::make_unique<FifoMatchingEngine>(response_queue);
std::unique_ptr< MatchingEngine > createMatchingEngine(const MatchingConfig &config, slick::queue< OrderResponse > &response_queue)
constexpr int32_t DOUBLE_MULTIPLIER
constexpr qty_t to_qty_t(double qty)
constexpr double to_qty_double(qty_t qty)
Which engine a venue's symbols match with.
static MatchingConfig from_json(const nlohmann::json &config)
Reads matching_algorithm ("fifo" by default, or "pro_rata") and, for pro-rata, the pro_rata block: lo...
MatchingEngine::Type type
qty_t min_allocation
a pro-rata share below this rounds to zero
void validate() const
Throws std::invalid_argument unless lot_size > 0, min_allocation >= 0 and fifo_percent <= 100.
uint8_t fifo_percent
share of the incoming quantity filled FIFO before pro-rata
qty_t lot_size
allocations round down to whole lots