37#include "fastmm/core/transport.hpp"
75struct CapacityConfig {
140 const double ln = std::log(ratio_);
141 z0_ = 1.0 / (1.0 + ln);
142 for (
int k = 0; k <=
kSteps; ++k) {
143 const double z =
static_cast<double>(k) /
kSteps;
144 table_[
static_cast<std::size_t
>(k)] =
145 z < z0_ ? 1.0 : std::
max(1.0, std::exp((z * (ln + 1.0)) - 1.0));
149 [[nodiscard]]
double at(
double z)
const noexcept {
150 const double c = z <= 0.0 ? 0.0 : (z >= 1.0 ? 1.0 : z);
151 return table_[
static_cast<std::size_t
>(c *
kSteps)];
153 [[nodiscard]]
double z0() const noexcept {
return z0_; }
154 [[nodiscard]]
double ratio() const noexcept {
return ratio_; }
157 std::array<double, kSteps + 1> table_{};
164class CapacityDayPrice {
167 static constexpr int kZero = 16;
169 [[nodiscard]]
static double edge(
int b)
noexcept {
170 return std::exp2(
static_cast<double>(b -
kZero) * 0.25);
173 [[nodiscard]]
static int bucket(
double rel)
noexcept {
174 if (!(rel > 0.0))
return 0;
177 const double m = std::frexp(rel, &e);
180 if (m >= 0.5946035575013605) q = 1;
181 if (m >= 0.7071067811865476) q = 2;
182 if (m >= 0.8408964152537145) q = 3;
183 const int b = ((e - 1) * 4) + q +
kZero;
184 return std::clamp(b, 0,
kBuckets - 1);
187 void note(
double rel)
noexcept { cur_[
static_cast<std::size_t
>(
bucket(rel))] += 1.0; }
189 [[nodiscard]]
double price() const noexcept {
return lambda_; }
190 [[nodiscard]] std::int64_t
used() const noexcept {
return used_; }
194 void tick(std::int64_t now_ns, std::int64_t budget,
double floor,
int kind)
noexcept {
195 constexpr std::int64_t kDayNs = 86'400'000'000'000;
196 constexpr std::int64_t kHourNs = 3'600'000'000'000;
198 for (std::size_t b = 0; b <
kBuckets; ++b) {
199 hist_[b] = (hist_[b] * kKeep) + cur_[b];
203 const std::int64_t day = now_ns / kDayNs;
208 const int h =
static_cast<int>((now_ns % kDayNs) / kHourNs);
209 if (h != hour_ || kind != kind_) {
211 double& d = dem_[
static_cast<std::size_t
>(kind_)][
static_cast<std::size_t
>(hour_)];
212 bool& s = seen_[
static_cast<std::size_t
>(kind_)][
static_cast<std::size_t
>(hour_)];
213 d = s ? (0.8 * d) + (0.2 * hour_count_) : hour_count_;
220 hour_count_ += minute;
222 if (budget <= 0)
return;
225 for (
const double x : hist_) recent += x;
226 const double recent_per_hour = recent * (1.0 - kKeep) * 60.0;
227 const auto dw = [&](
int k) {
228 const auto ki =
static_cast<std::size_t
>(kind);
229 const auto kk =
static_cast<std::size_t
>(k);
230 return seen_[ki][kk] ? std::max(dem_[ki][kk], 1.0) : std::
max(recent_per_hour, 1.0);
232 const double hour_left =
233 1.0 - (
static_cast<double>(now_ns % kHourNs) /
static_cast<double>(kHourNs));
234 double rest = dw(h) * hour_left;
235 for (
int k = h + 1; k < 24; ++k) rest += dw(k);
236 const double remaining =
static_cast<double>(std::max<std::int64_t>(budget - used_, 0));
237 if (remaining >= rest || recent <= 0.0)
return;
239 const double keep = remaining / rest * recent;
241 for (
int b =
kBuckets - 1; b >= 0; --b) {
242 cum += hist_[
static_cast<std::size_t
>(b)];
251 static constexpr double kKeep = 0.9;
252 static constexpr double kTopEdge = 4096.0;
253 std::array<double, kBuckets> cur_{};
254 std::array<double, kBuckets> hist_{};
255 double dem_[2][24] = {};
256 bool seen_[2][24] = {};
257 double hour_count_ = 0.0;
260 std::int64_t day_ = -1;
261 std::int64_t used_ = 0;
262 double lambda_ = 0.0;
271 static constexpr std::int64_t
kTickNs = 60'000'000'000;
280 window_ns_ = std::max<std::int64_t>(cfg_.
window_ms, 1) * 1'000'000;
285 [[nodiscard]]
bool on() const noexcept {
return cfg_.
on(); }
288 [[nodiscard]]
double floor() const noexcept {
return floor_; }
291 return account.value < 32 && ((cfg_.
accounts >> account.value) & 1U) != 0;
295 [[nodiscard]]
bool enforces(
bool move)
const noexcept {
307 std::int64_t now_ns)
noexcept {
308 if (!
prices(account))
return;
309 if (account.value < kMaxVenues) {
310 Window& w = window(account, now_ns);
312 AccountState& a = acct_[account.value];
313 a.minute.peak_used = std::max(a.minute.peak_used,
static_cast<std::uint32_t
>(w.used()));
319 if (
id.value < kMaxInstruments && notional > 0.0) {
320 Yield& c = cell(
id, side, notional);
321 c.placed += notional;
323 Yield& k = class_[
static_cast<std::size_t
>(class_of(notional))];
324 k.placed += notional;
326 total_.placed += notional;
332 if (cfg_.
weight_1m <= 0 || primary.value >= kMaxVenues)
return;
333 Window& w = weight_[primary.value];
335 if (w.id != wid) w =
Window{wid, 0, 0};
342 double order_notional,
343 double fill_notional,
345 std::int64_t now_ns)
noexcept {
346 if (!
prices(account))
return;
347 if (first && cfg_.
fill_credit && account.value < kMaxVenues) {
348 Window& w = window(account, now_ns);
349 if (w.credit < w.sent) {
354 if (
id.value < kMaxInstruments && order_notional > 0.0 && fill_notional > 0.0) {
355 cell(
id, side, order_notional).filled += fill_notional;
356 class_[
static_cast<std::size_t
>(class_of(order_notional))].filled += fill_notional;
357 total_.filled += fill_notional;
365 std::int64_t now_ns)
const noexcept {
372 if (cfg_.
orders_10s > 0 && account.value < kMaxVenues) {
373 const Window& w = acct_[account.value].window;
374 q.
window_used = w.id == now_ns / window_ns_ ? w.used() : 0;
378 if (cfg_.
weight_1m > 0 && primary.value < kMaxVenues) {
379 const Window& w = weight_[primary.value];
380 const std::int64_t used = w.id == now_ns /
kWeightWindowNs ? w.used() : 0;
383 (threshold_.
at(
static_cast<double>(used) /
static_cast<double>(cfg_.
weight_1m)) - 1.0);
385 q.
day = all_.
price() + (account.value < kMaxVenues ? acct_[account.value].day.price() : 0.0);
396 const int c = class_of(notional);
398 const double cls = shrink(class_[
static_cast<std::size_t
>(c)], global);
400 shrink(cells_[
id.value][
static_cast<std::size_t
>(side)][
static_cast<std::size_t
>(c)], cls);
415 std::int64_t now_ns = 0)
noexcept {
416 if (value != value) value = std::numeric_limits<double>::infinity();
417 const bool pass = !(value <
price);
419 std::int64_t& last = refused_[
id.value][
static_cast<std::size_t
>(side)];
420 const std::int64_t w = (now_ns / window_ns_) + 1;
421 if (last == w)
return false;
424 const double rel = floor_ > 0.0 ? value / floor_ : std::numeric_limits<double>::infinity();
426 if (account.
value >= kMaxVenues)
return pass;
427 AccountState& a = acct_[account.
value];
429 CapacityMinute& m = a.minute;
430 if (m.decided == 0) {
434 m.price_min = std::min(m.price_min,
price);
435 m.price_max = std::max(m.price_max,
price);
438 m.price_sum +=
price;
441 ++(move ? m.priced_out_move : m.priced_out_new);
444 ++(move ? m.observed_move : m.observed_new);
456 [[nodiscard]]
bool recheck_due(std::int64_t now_ns)
const noexcept {
457 return credited_ || now_ns >= recheck_ns_;
459 void rechecked(std::int64_t now_ns)
noexcept {
461 recheck_ns_ = ((now_ns / window_ns_) + 1) * window_ns_;
463 [[nodiscard]]
bool tick_due(std::int64_t now_ns)
const noexcept {
return now_ns >= tick_ns_; }
466 template <
class Emit>
467 void tick(std::int64_t now_ns, Emit&& emit)
noexcept {
468 const bool first = tick_ns_ == 0;
471 last_decay_ns_ = now_ns;
474 const double dt =
static_cast<double>(now_ns - last_decay_ns_) * 1e-9;
475 last_decay_ns_ = now_ns;
479 for (Yield& y : class_) decay(y, k);
480 for (
auto& inst : cells_)
481 for (
auto& side : inst)
482 for (Yield& y : side) decay(y, k);
485 const int kind = weekend(now_ns) ? 1 : 0;
487 for (std::size_t v = 0; v < kMaxVenues; ++v) {
488 AccountState& a = acct_[v];
489 a.day.tick(now_ns, cfg_.
orders_1d, floor_, kind);
490 if (a.minute.decided != 0 || a.minute.sent != 0) {
491 emit(
VenueId{
static_cast<std::uint8_t
>(v)}, a.minute);
497 return all_.
price() + (account.
value < kMaxVenues ? acct_[account.
value].day.price() : 0.0);
502 std::int64_t
id = -1;
503 std::int64_t sent = 0;
504 std::int64_t credit = 0;
505 [[nodiscard]] std::int64_t used() const noexcept {
506 return std::max<std::int64_t>(sent - credit, 0);
514 struct AccountState {
516 CapacityDayPrice day;
517 CapacityMinute minute;
520 Window& window(
VenueId account, std::int64_t now_ns)
noexcept {
522 const std::int64_t wid = now_ns / window_ns_;
523 if (w.id != wid) w =
Window{wid, 0, 0};
526 [[nodiscard]]
int class_of(
double notional)
const noexcept {
528 if (r < 2.0)
return 0;
529 if (r < 6.0)
return 1;
530 if (r < 12.0)
return 2;
531 if (r < 22.0)
return 3;
532 if (r < 60.0)
return 4;
536 return cells_[
id.value][
static_cast<std::size_t
>(side)]
537 [
static_cast<std::size_t
>(class_of(notional))];
540 [[nodiscard]]
static double shrink(
const Yield& y,
double prior)
noexcept {
541 if (!(y.n > 0.0) || !(y.placed > 0.0))
return prior;
543 return (y.filled + (prior * w)) / (y.placed + w);
545 static void decay(Yield& y,
double k)
noexcept {
550 void update_floor() noexcept {
553 [[nodiscard]]
static bool weekend(std::int64_t ns)
noexcept {
554 const auto wd = (ns / 86'400'000'000'000 + 4) % 7;
555 return wd == 0 || wd == 6;
558 CapacityConfig cfg_{};
559 CapacityThreshold threshold_{};
560 std::int64_t window_ns_ = 10'000'000'000;
562 std::array<AccountState, kMaxVenues> acct_{};
563 std::array<Window, kMaxVenues> weight_{};
564 CapacityDayPrice all_{};
566 std::array<Yield, kClasses> class_{};
568 CapacityMinute totals_{};
570 std::int64_t recheck_ns_ = 0;
571 bool credited_ =
false;
572 std::int64_t tick_ns_ = 0;
573 std::int64_t last_decay_ns_ = 0;
static constexpr int kZero
bucket of value L: [L, L * 2^(1/4))
Definition capacity_price.hpp:169
void note(double rel) noexcept
Definition capacity_price.hpp:189
void tick(std::int64_t now_ns, std::int64_t budget, double floor, int kind) noexcept
Once a minute: fold the minute's demand in, roll the day, and fit the price.
Definition capacity_price.hpp:196
static int bucket(double rel) noexcept
The bucket of a value v given as a multiple of L (v / L); infinite: the top bucket.
Definition capacity_price.hpp:175
double price() const noexcept
Definition capacity_price.hpp:191
void note_sent() noexcept
Definition capacity_price.hpp:190
static double edge(int b) noexcept
The lower edge of bucket b as a multiple of L.
Definition capacity_price.hpp:171
std::int64_t used() const noexcept
Definition capacity_price.hpp:192
static constexpr int kBuckets
Definition capacity_price.hpp:168
double floor() const noexcept
Definition capacity_price.hpp:290
double learned_value(InstrumentId id, Side side, double notional) const noexcept
The learned value of a quote of notional on (id, side), quote currency.
Definition capacity_price.hpp:394
static constexpr double kShrinkOrders
a cell's estimate leans on its class's for this
Definition capacity_price.hpp:272
double price(VenueId account, VenueId primary, std::int64_t now_ns) const noexcept
Definition capacity_price.hpp:389
static constexpr int kClasses
by notional over the minimum: <2, <6, <12, <22, <60, more
Definition capacity_price.hpp:270
bool decide(VenueId account, double value, double price, bool move, bool enforced, InstrumentId id=InstrumentId{}, Side side=Side::Buy, std::int64_t now_ns=0) noexcept
An action of value (a move: its gain) was priced at price on account; returns whether it passes.
Definition capacity_price.hpp:408
void tick(std::int64_t now_ns, Emit &&emit) noexcept
Once a minute: decay the yields, refit the floor and the day prices, and hand each account's minute t...
Definition capacity_price.hpp:466
static constexpr double kPriorYield
filled per placed notional before any is learned
Definition capacity_price.hpp:271
static constexpr std::int64_t kTickNs
Definition capacity_price.hpp:273
bool recheck_due(std::int64_t now_ns) const noexcept
A priced-out level is applied again once a window has rolled over or a fill gave a slot back.
Definition capacity_price.hpp:455
static constexpr std::int64_t kWeightWindowNs
Definition capacity_price.hpp:274
static constexpr double kYieldHalfLifeS
Definition capacity_price.hpp:275
void on_fill(VenueId account, InstrumentId id, Side side, double order_notional, double fill_notional, bool first, std::int64_t now_ns) noexcept
A fill of an order placed with order_notional; first: the order's first fill.
Definition capacity_price.hpp:340
CapacityMode mode() const noexcept
Definition capacity_price.hpp:288
bool on() const noexcept
Definition capacity_price.hpp:287
void rechecked(std::int64_t now_ns) noexcept
Definition capacity_price.hpp:458
bool tick_due(std::int64_t now_ns) const noexcept
Definition capacity_price.hpp:462
bool prices(VenueId account) const noexcept
Whether orders on account are priced ([capacity] venues).
Definition capacity_price.hpp:292
double day_price(VenueId account) const noexcept
Definition capacity_price.hpp:495
void on_request(VenueId primary, std::int64_t now_ns) noexcept
A cancel (or any other request) of the pool: weight only.
Definition capacity_price.hpp:332
void on_send(VenueId account, VenueId primary, InstrumentId id, Side side, double notional, std::int64_t now_ns) noexcept
A new order or a replace went to account (of the pool whose primary is primary).
Definition capacity_price.hpp:303
const CapacityMinute & totals() const noexcept
Definition capacity_price.hpp:451
const CapacityThreshold & threshold() const noexcept
Definition capacity_price.hpp:289
CapacityQuote quote(VenueId account, VenueId primary, std::int64_t now_ns) const noexcept
Definition capacity_price.hpp:363
void configure(const CapacityConfig &cfg) noexcept
Definition capacity_price.hpp:277
const CapacityConfig & config() const noexcept
Definition capacity_price.hpp:286
bool enforces(bool move) const noexcept
Whether a refusal below the price is enforced for a new order or for a move.
Definition capacity_price.hpp:297
h(z): the knapsack threshold as a multiple of L, precomputed.
Definition capacity_price.hpp:137
double at(double z) const noexcept
The multiple of L at window share z (clamped to [0, 1]).
Definition capacity_price.hpp:151
double z0() const noexcept
Definition capacity_price.hpp:155
void configure(double ratio) noexcept
Definition capacity_price.hpp:140
static constexpr int kSteps
Definition capacity_price.hpp:139
double ratio() const noexcept
Definition capacity_price.hpp:156
Instrument: the single definition of a tradable contract (5.3 / 5.15).
Definition strategy.hpp:23
@ Window
an open [[windows]] window with action = "pull" covers the instrument
@ Off
none was asked for in this session
constexpr Fixed< T > max(Fixed< T > a, Fixed< T > b) noexcept
Definition fixed_point.hpp:418
Side
Definition enums.hpp:12
CapacityMode
Definition capacity_price.hpp:51
@ New
new orders are refused below their price; moves are observed
@ Observe
prices are computed, counted and recorded; nothing is refused
@ Enforce
new orders and moves are refused below their price
StrongId< struct InstrumentTag, std::uint32_t > InstrumentId
Dense index into the engine's instrument table.
Definition strong_id.hpp:38
constexpr std::size_t kMaxInstruments
Definition instrument.hpp:26
constexpr std::string_view to_string(ParamType t) noexcept
Definition params.hpp:63
CapacityValue
The value of a quote level whose strategy states none.
Definition capacity_price.hpp:72
@ Learned
notional * yield(instrument, side, size class) * margin_bps
@ Off
worth any price: the level is sent as without [capacity]
StrongId< struct VenueTag, std::uint8_t > VenueId
Definition strong_id.hpp:39
Order: the OMS's 128-byte record for one order across its lifetime.
Strongly typed identifiers.
Definition capacity_price.hpp:77
double min_notional
The threshold's range in notional, filled by the engine: the smallest minimum notional of the instrum...
Definition capacity_price.hpp:100
std::int64_t orders_1d
Each account's orders per UTC day (Binance Spot: 200,000); 0: no day price per account.
Definition capacity_price.hpp:87
CapacityValue value
Definition capacity_price.hpp:79
bool on() const noexcept
Definition capacity_price.hpp:105
std::int64_t orders_per_day
The strategy's own orders per UTC day over every account; 0: no budget.
Definition capacity_price.hpp:89
double max_notional
Definition capacity_price.hpp:101
bool fill_credit
The venue gives a slot back on an order's first fill (Binance's unfilled order count).
Definition capacity_price.hpp:85
double margin_bps
value = "learned": what a filled unit of notional earns, bps (rebate plus edge).
Definition capacity_price.hpp:94
std::int64_t window_ms
Each account's order window: its length and the orders it admits (Binance Spot: 10 s,...
Definition capacity_price.hpp:82
std::int64_t orders_10s
Definition capacity_price.hpp:83
std::uint32_t accounts
The accounts priced, bit per VenueId ([capacity] venues: those venues and their pools' members); the ...
Definition capacity_price.hpp:104
CapacityMode mode
Definition capacity_price.hpp:78
double full_move_bps
A move of this many bps or more is worth a whole new order when the strategy states no keep value: th...
Definition capacity_price.hpp:97
std::int64_t weight_1m
Request weight per minute shared by the accounts of a pool (one IP); 0: not priced.
Definition capacity_price.hpp:92
A minute of one account (CapacityRecord): what was priced and refused.
Definition capacity_price.hpp:122
std::uint32_t sent
new orders and replaces sent
Definition capacity_price.hpp:124
double price_sum
Definition capacity_price.hpp:132
std::uint32_t peak_used
the most orders in one window
Definition capacity_price.hpp:129
std::uint32_t decided
actions priced
Definition capacity_price.hpp:123
std::uint32_t observed_new
would have been priced out (observe, or moves under "new")
Definition capacity_price.hpp:127
std::uint32_t priced_out_move
Definition capacity_price.hpp:126
void clear() noexcept
Definition capacity_price.hpp:133
std::uint32_t priced_out_new
Definition capacity_price.hpp:125
double price_min
Definition capacity_price.hpp:130
double price_max
Definition capacity_price.hpp:131
std::uint32_t observed_move
Definition capacity_price.hpp:128
What an action costs on one account now (ctx.capacity).
Definition capacity_price.hpp:109
double price
the whole price: what an action's value must reach
Definition capacity_price.hpp:110
double window
the account window's part (L * h(z))
Definition capacity_price.hpp:112
double floor
L, the value of a minimum-notional order.
Definition capacity_price.hpp:111
double window_share
z of the account's window
Definition capacity_price.hpp:115
std::int64_t window_used
Definition capacity_price.hpp:116
double day
the account's and the strategy's day prices
Definition capacity_price.hpp:114
double weight
the pool's request weight part
Definition capacity_price.hpp:113
bool on
Definition capacity_price.hpp:118
std::int64_t window_orders
orders_10s (0: the window is not priced)
Definition capacity_price.hpp:117
Rep value
Definition strong_id.hpp:26