7#include <nlohmann/json.hpp>
12#include <initializer_list>
17#include <unordered_map>
39 std::string_view who) {
40 if (value ==
"reject_if_crossing") {
43 if (value ==
"accept") {
46 if (value ==
"reject") {
49 if (!native.empty() && value == native) {
52 throw std::runtime_error(std::format(
53 "{}: post_only_policy must be \"reject_if_crossing\", \"accept\" or \"reject\"{}, not \"{}\"", who,
54 native.empty() ? std::string() : std::format(
" or \"{}\"", native), value));
65 if (qty <= 0 || qty % QTY_SCALE != 0 || qty / QTY_SCALE > std::numeric_limits<uint32_t>::max()) {
68 return static_cast<uint32_t
>(qty /
QTY_SCALE);
88 [[nodiscard]]
bool allow(uint64_t now_ns)
noexcept {
92 const uint64_t window = now_ns / 1'000'000'000ULL;
93 if (window != window_) {
97 return ++count_ <= per_sec_;
100 [[nodiscard]] uint32_t
per_sec() const noexcept {
return per_sec_; }
104 uint64_t window_ = 0;
115 if (!config.is_object()) {
118 for (
const auto& [key, value] : config.items()) {
119 if (key ==
"default") {
120 table.default_fee_ = value.get<
double>();
122 table.fees_[key] = value.get<
double>();
129 [[nodiscard]]
double per_contract(std::initializer_list<std::string_view> names)
const {
130 for (
const auto name : names) {
131 auto it = fees_.find(name);
132 if (it != fees_.end()) {
139 [[nodiscard]]
double default_fee() const noexcept {
return default_fee_; }
140 void set(std::string_view name,
double fee) { fees_.insert_or_assign(std::string(name), fee); }
144 std::unordered_map<std::string, double, StringHash, std::equal_to<>> fees_;
145 double default_fee_ = 0.0;
154 event.side = request.
side;
155 event.price = request.
price;
158 event.set_reason(reason);
double per_contract(std::initializer_list< std::string_view > names) const
double default_fee() const noexcept
void set_default(double fee) noexcept
void set(std::string_view name, double fee)
static FeeTable from_json(const nlohmann::json &config)
bool allow(uint64_t now_ns) noexcept
uint32_t per_sec() const noexcept
RateLimiter(uint32_t per_sec=0) noexcept
PostOnlyPolicy parse_post_only_policy(const std::string &value, std::string_view native, std::string_view who)
uint32_t whole_lots(qty_t qty) noexcept
qty_t lots_to_qty(int64_t lots) noexcept
OrderRejectEvent reject_of(const NewOrderEvent &request, BrokerId broker, std::string_view reason)
const char * post_only_problem(PostOnlyPolicy policy, const SharedTopOfBook *top, Side side, OrderType type, TimeInForce tif, price_t price, bool &warned, std::string_view who)
bool rests(OrderType type, TimeInForce tif) noexcept
constexpr int64_t QTY_SCALE
char instrument[MAX_INSTRUMENT_LENGTH]