Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
matching_config.cpp
Go to the documentation of this file.
1#include "matching_config.hpp"
4#include <slick/logger.hpp>
5#include <cmath>
6#include <format>
7#include <limits>
8#include <stdexcept>
9#include <string>
10
11using namespace slick::sim;
12using namespace slick::sim::engine;
13
14namespace {
15
16// Both readers check the JSON value before narrowing it. get<int>() turns 100.5
17// into 100 and 2^32 into 0, and a double too big for qty_t has no defined cast.
18
19uint8_t read_percent(const nlohmann::json &params, const char *key) {
20 const auto it = params.find(key);
21 if (it == params.end()) {
22 return 0;
23 }
24 if (it->is_number_integer()) { // floats and bools are not
25 const auto v = it->get<int64_t>(); // an unsigned above INT64_MAX wraps negative
26 if (v >= 0 && v <= 100) {
27 return static_cast<uint8_t>(v);
28 }
29 }
30 throw std::invalid_argument(std::format("pro_rata.{} must be an integer 0-100, got {}", key, it->dump()));
31}
32
33qty_t read_qty(const nlohmann::json &params, 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()) {
37 return to_qty_t(fallback);
38 }
39 if (it->is_number() && std::isfinite(it->get<double>()) && std::fabs(it->get<double>()) <= kMax) {
40 return to_qty_t(it->get<double>());
41 }
42 throw std::invalid_argument(std::format("pro_rata.{} must be a number within +/-{}, got {}", key, kMax, it->dump()));
43}
44
45} // namespace
46
48 if (lot_size <= 0) {
49 throw std::invalid_argument(std::format("pro_rata.lot_size must be positive, got {}", to_qty_double(lot_size)));
50 }
51 if (min_allocation < 0) {
52 throw std::invalid_argument(std::format("pro_rata.min_allocation must not be negative, got {}",
54 }
55 if (fifo_percent > 100) {
56 throw std::invalid_argument(std::format("pro_rata.fifo_percent must be 0-100, got {}", fifo_percent));
57 }
58}
59
60MatchingConfig MatchingConfig::from_json(const nlohmann::json &config) {
62 try {
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");
67 }
68 return out;
69 }
70 if (algorithm != "pro_rata") {
71 throw std::invalid_argument(std::format(
72 "unknown matching_algorithm '{}'; expected fifo or pro_rata", algorithm));
73 }
74
76 const auto params = config.value("pro_rata", nlohmann::json::object());
77 if (!params.is_object()) { // find() on a non-object finds nothing, which would mean defaults
78 throw std::invalid_argument(std::format("pro_rata must be an object, got {}", params.dump()));
79 }
80 out.pro_rata.lot_size = read_qty(params, "lot_size", 1.0);
81 out.pro_rata.min_allocation = read_qty(params, "min_allocation", 0.0);
82 out.pro_rata.fifo_percent = read_percent(params, "fifo_percent");
83 } catch (const nlohmann::json::exception &e) {
84 throw std::invalid_argument(std::format("bad matching config: {}", e.what()));
85 }
86 out.pro_rata.validate();
87 return out;
88}
89
90std::unique_ptr<MatchingEngine> slick::sim::engine::createMatchingEngine(const MatchingConfig &config,
91 slick::queue<OrderResponse> &response_queue) {
92 if (config.type == MatchingEngine::Type::ProRata) {
93 return std::make_unique<ProRataMatchingEngine>(response_queue, config.pro_rata);
94 }
95 return std::make_unique<FifoMatchingEngine>(response_queue);
96}
std::unique_ptr< MatchingEngine > createMatchingEngine(const MatchingConfig &config, slick::queue< OrderResponse > &response_queue)
constexpr int32_t DOUBLE_MULTIPLIER
Definition types.hpp:29
int_fast64_t qty_t
Definition types.hpp:22
constexpr qty_t to_qty_t(double qty)
Definition types.hpp:50
constexpr double to_qty_double(qty_t qty)
Definition types.hpp:54
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...
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