FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
quote_manager.hpp
Go to the documentation of this file.
1#pragma once
2// QuoteManager (5.6): diffs the strategy's DesiredQuotes against the orders resting at the
3// venue and emits the minimal set of New / Cancel / Replace actions.
4//
5// Rules:
6// * level i of a side maps to slot i (one order per slot);
7// * hysteresis: keep an order whose price is within min_requote_ticks of the desired
8// price and whose leaves cover min_qty_bps of the desired quantity;
9// * never re-quote a slot within min_requote_interval of its last change;
10// * orders in a Pending* state are never touched: the slot records the target instead and applies
11// it when the order's ack or terminal update arrives via on_order_update() (dropped if the
12// instrument is pulled first);
13// * a slot only counts an order as its own if it is live and carries the slot's tag (order
14// slots are reused, and a replace changes the order's id);
15// * while an instrument is pulled, an order that becomes working late (its ack arrived after
16// the pull) is cancelled at once;
17// * a New or Replace refused by the venue (other than a post-only cross, or an order the venue
18// no longer has) blocks new orders and replaces on that side for reject_backoff, doubling on
19// each further refusal up to reject_backoff_max and reset by an ack on that side, so a side
20// the venue keeps refusing (no balance, a price outside its band during a move) is not resent
21// on every requote; a requote that meets a working order meanwhile cancels it, as on a venue
22// without replace, and the New waits for the backoff to end. A New or Replace the engine's own
23// checks refuse for its price (price collar, fat-finger band: QuoteAction::refused) backs off
24// the same way, since the next requote during a move asks for a price just as far out;
25// * an instrument whose venue supports replace (set_replace(), with supports_replace) gets a
26// single Replace; otherwise Cancel now and New only once the cancel's terminal update arrives
27// via on_order_update() (cancel-then-new keeps momentary exposure down and never double-quotes
28// a level, and the New is checked against the balance once the old order's hold is released);
29// * such a deferred New (QuoteAction::deferred) that the balance no longer covers -- the order it
30// replaced filled instead of cancelling -- is withheld (QuoteAction::withheld, kept_balance)
31// rather than refused by risk: the target predates the fill, the strategy's next quotes decide;
32// * pause_quotes() holds an instrument's orders where they rest (the engine during a
33// reconciliation): nothing is sent, moved or cancelled until resume(), which re-applies the
34// last desired quotes against them, so an order that still matches is kept rather than
35// cancelled and sent again; pull_quotes(keep_desired) does the same with the orders cancelled;
36// * a move towards the touch (a bid up, an ask down) that the placer holds (QuoteAction::held:
37// the account the order rests on has no more than QuoteParams::room_reserve orders left in its
38// venue order windows) keeps the resting order where it is: the move would spend one of the
39// last orders, and with cancel-then-new a New the window then refuses leaves the level empty.
40// Moves away from the touch and pulls still go. The strategy's next quotes try the move again;
41// * a New or a move the placer prices out (QuoteAction::priced_out: [capacity], its value is
42// below
43// the price of the order slot it takes, core/capacity_price.hpp) is neither a reject nor a
44// backoff: a move keeps the resting order where it is, a New leaves the level empty, and the
45// instrument joins a queue the engine re-applies (retry_priced()) when the price falls. A move
46// towards the touch buys its gain (the level's value less its keep value); a move away from it
47// cancels for free and buys its New as a new order;
48// * hold_side() holds one side on every instrument (the engine's fill-cluster guard,
49// core/fill_cluster.hpp) or on one (its hazard groups, core/hazard.hpp): pull_side() cancels
50// what rests there, reconcile() and every retry treat the side's levels as empty while the
51// desired quotes keep them, a target recorded for a pending order on the side is not placed,
52// and reapply() puts the last desired quotes back when the hold ends;
53// * a New or Replace the placer holds back for want of an order token (QuoteAction::rate_limited)
54// is neither a reject nor a backoff: the instrument joins a FIFO of starved instruments, and
55// the engine re-applies their last desired quotes (retry()) in that order as tokens come back,
56// so a burst of requotes on many instruments is served in turn instead of refused event by
57// event.
58//
59// Actions are executed by the caller through a Placer callable so the QuoteManager stays
60// independent of risk/OMS/transport wiring:
61// bool place(QuoteAction& a); // New: sets a.handle on success; returns false on reject
63#include "fastmm/core/config_macros.hpp"
64#include "fastmm/core/containers/ring_buffer.hpp"
65#include "fastmm/core/containers/static_vector.hpp"
68#include "fastmm/core/oms.hpp"
69#include "fastmm/core/time.hpp"
70
71#include <algorithm>
72#include <array>
73#include <cstddef>
74#include <cstdint>
75#include <limits>
76
77namespace fastmm {
78
79inline constexpr std::size_t kMaxQuoteLevels = 8;
80
82 StaticVector<Level, kMaxQuoteLevels> bids; // index 0 = closest to mid
83 StaticVector<Level, kMaxQuoteLevels> asks;
84 // The pool account a level's order goes from (NewOrderRequest::account); invalid = the engine's
85 // automatic routing. A resting order on another account is cancelled and placed again.
86 std::array<VenueId, kMaxQuoteLevels> bid_account{};
87 std::array<VenueId, kMaxQuoteLevels> ask_account{};
88 // What a level's order is worth, in the quote currency ([capacity], core/capacity_price.hpp):
89 // `value` placed at the level's price, `keep` the order already resting in the slot where it
90 // rests. kNoValue: not stated ([capacity] value decides).
91 std::array<float, kMaxQuoteLevels> bid_value = no_values();
92 std::array<float, kMaxQuoteLevels> ask_value = no_values();
93 std::array<float, kMaxQuoteLevels> bid_keep = no_values();
94 std::array<float, kMaxQuoteLevels> ask_keep = no_values();
95 void clear() noexcept {
96 bids.clear();
97 asks.clear();
98 bid_account = {};
99 ask_account = {};
100 bid_value = no_values();
101 ask_value = no_values();
102 bid_keep = no_values();
103 ask_keep = no_values();
104 }
105 [[nodiscard]] VenueId account(Side s, std::uint32_t lvl) const noexcept {
106 return lvl < kMaxQuoteLevels ? (s == Side::Buy ? bid_account : ask_account)[lvl] : VenueId{};
108 // Sets the value of level `lvl` of a side and, optionally, the keep value of its resting order.
109 void set_value(Side s, std::uint32_t lvl, float value, float keep = kNoValue) noexcept {
110 if (lvl >= kMaxQuoteLevels) return;
112 (s == Side::Buy ? bid_keep : ask_keep)[lvl] = keep;
113 }
114 [[nodiscard]] float value(Side s, std::uint32_t lvl) const noexcept {
115 return lvl < kMaxQuoteLevels ? (s == Side::Buy ? bid_value : ask_value)[lvl] : kNoValue;
117 [[nodiscard]] float keep(Side s, std::uint32_t lvl) const noexcept {
118 return lvl < kMaxQuoteLevels ? (s == Side::Buy ? bid_keep : ask_keep)[lvl] : kNoValue;
120 [[nodiscard]] const StaticVector<Level, kMaxQuoteLevels>& side(Side s) const noexcept {
121 return s == Side::Buy ? bids : asks;
123 [[nodiscard]] bool empty() const noexcept { return bids.empty() && asks.empty(); }
124 // Append the next level of a side (index 0 first). A non-positive price or quantity is dropped,
125 // as is a level beyond kMaxQuoteLevels; returns whether the level was added.
126 bool bid(Price px, Qty qty) noexcept {
127 return px.is_positive() && qty.is_positive() && bids.push_back(Level{px, qty});
129 bool ask(Price px, Qty qty) noexcept {
130 return px.is_positive() && qty.is_positive() && asks.push_back(Level{px, qty});
132 // ... with the level's value and its resting order's keep value ([capacity]).
133 bool bid(Price px, Qty qty, float value, float keep = kNoValue) noexcept {
134 if (!bid(px, qty)) return false;
135 set_value(Side::Buy, static_cast<std::uint32_t>(bids.size() - 1), value, keep);
136 return true;
137 }
138 bool ask(Price px, Qty qty, float value, float keep = kNoValue) noexcept {
139 if (!ask(px, qty)) return false;
140 set_value(Side::Sell, static_cast<std::uint32_t>(asks.size() - 1), value, keep);
141 return true;
142 }
143 // Never cross ourselves at level 0: an ask at or below the best bid moves one tick above it.
144 void uncross(Price tick) noexcept {
145 if (!bids.empty() && !asks.empty() && bids[0].price >= asks[0].price)
146 asks[0].price = bids[0].price + tick;
147 }
148
149 private:
150 static constexpr std::array<float, kMaxQuoteLevels> no_values() noexcept {
151 std::array<float, kMaxQuoteLevels> a{};
152 for (float& v : a) v = kNoValue;
153 return a;
154 }
155};
156
157struct QuoteParams {
158 std::int64_t min_requote_ticks = 1; // |desired - resting| < this -> keep
160 std::int64_t min_qty_bps = 8000; // leaves >= 80 % of desired -> qty ok
161 bool supports_replace = false; // off: never replace; on: where the instrument's venue can
162 // Replace only if every instrument's venue can (the engine before replace was per venue; set to
163 // replay journals recorded then).
164 bool replace_all_venues = false;
165 bool post_only = true;
166 Duration reject_backoff = milliseconds(1000); // 0 = no backoff after venue rejects
168 // Order tokens ([risk] orders_per_sec) a quote that does not reduce the position leaves for the
169 // orders a strategy sends itself (hedges, unwinds) and for quotes that do. Read by the engine's
170 // placer; 0 = quotes may take the last token.
171 std::int64_t token_reserve = 0;
172 // Order tokens every quote leaves for the orders a strategy sends itself (ctx.send, a hedge or an
173 // unwind), on top of token_reserve for the quotes that add risk. A quote short of a token waits
174 // for one; a sent order short of one is refused RateLimit, and the waiting quotes take each token
175 // as it comes back, so without this a sent order can find none for as long as quotes wait.
176 // 0 = quotes may take the last token.
177 std::int64_t send_reserve = 0;
178 // Orders an account's venue order windows (ctx.order_budget) keep for News into empty levels and
179 // for moves away from the touch: with no more than this many left, a resting quote is not moved
180 // towards the touch (held, QuoteStats::kept_room). Read by the engine's placer; 0 = only when
181 // none is left; -1 = never held.
182 std::int64_t room_reserve = 0;
183};
185enum class QuoteActionKind : std::uint8_t { New, Cancel, Replace };
186
191 std::uint32_t level;
192 Handle<Order> handle; // Cancel/Replace input; New output
193 ClientOrderId cl_ord_id; // New output (the placer fills it)
197 // New: the target was recorded while an earlier order held the slot and is placed now that it
198 // ended (on_order_update), so it was decided against a balance that order may have changed.
199 bool deferred;
200 bool withheld; // New output: not sent, the balance does not cover it (deferred only)
201 // New / Replace output: not sent, no order token for it now (QuoteParams::token_reserve,
202 // send_reserve); the instrument is retried when tokens come back.
203 bool rate_limited;
204 VenueId account{}; // New: the pool account to send it from (invalid = automatic)
205 // New / Replace output: the pre-trade check that refused it (None: sent, or not refused by one).
207 // Cancel / Replace output: not sent, the move goes towards the touch and the order's account is
208 // short of order room (QuoteParams::room_reserve); the order stays as it is. A Cancel that moves
209 // a quote carries the target in `price` and `qty`; a pull carries none and is never held.
210 bool held = false;
211 // New / Replace / a Cancel that moves: the level's value and the resting order's keep value
212 // (DesiredQuotes::value, keep; kNoValue: not stated).
213 float value = kNoValue;
214 float keep = kNoValue;
215 // Output: not sent, the action is worth less than the order slot it takes ([capacity]).
216 bool priced_out = false;
217 // A Cancel that moves, output: the move was bought, so the New after it is not priced again.
218 // A deferred New, input: the move that freed its slot was bought.
219 bool paid = false;
220};
222struct QuoteStats {
223 std::uint64_t news = 0;
224 std::uint64_t cancels = 0;
225 std::uint64_t replaces = 0;
226 std::uint64_t kept_hysteresis = 0;
227 std::uint64_t kept_interval = 0;
228 std::uint64_t skipped_pending = 0;
229 std::uint64_t rejected = 0;
230 std::uint64_t reject_backoffs = 0; // venue rejects that started or extended a side backoff
231 std::uint64_t kept_backoff = 0; // News withheld because their side was backing off
232 std::uint64_t kept_balance = 0; // deferred News withheld: the balance no longer covers them
233 std::uint64_t kept_rate_limit = 0; // News / Replaces held back for want of an order token
234 std::uint64_t kept_room = 0; // moves towards the touch held: the account is short of room
235 std::uint64_t retries = 0; // retry() calls that re-applied a starved instrument's quotes
236 std::uint64_t priced_out = 0; // News not sent: worth less than their order slot ([capacity])
237 std::uint64_t priced_out_moves = 0; // moves not made, the order left where it rests
240class QuoteManager {
241 static constexpr std::int64_t kNever = std::numeric_limits<std::int64_t>::max();
243 public:
244 explicit QuoteManager(const QuoteParams& p = {}) noexcept : params_(p) { resolve_replace(); }
245
246 [[nodiscard]] const QuoteParams& params() const noexcept { return params_; }
247 void set_params(const QuoteParams& p) noexcept {
248 params_ = p;
249 resolve_replace();
250 }
251 [[nodiscard]] const QuoteStats& stats() const noexcept { return stats_; }
252 // Whether the instrument's venue amends in place (default true; supports_replace still applies).
253 void set_replace(InstrumentId id, bool on) noexcept {
254 InstState& st = state_[id.value];
255 st.venue_no_replace = !on;
256 st.replace = params_.supports_replace && on;
257 }
258 [[nodiscard]] bool replaces(InstrumentId id) const noexcept { return state_[id.value].replace; }
259
260 // user_tag encoding so a terminal OmsUpdate can be mapped back to its slot.
261 [[nodiscard]] static constexpr std::uint32_t make_tag(Side s, std::uint32_t level) noexcept {
262 return 0x5100'0000U | (static_cast<std::uint32_t>(s) << 8) | level;
264 [[nodiscard]] static constexpr bool is_quote_tag(std::uint32_t tag) noexcept {
265 return (tag & 0xFFFF'0000U) == 0x5100'0000U;
267
268 // Reconciles desired vs resting for one instrument. Returns the number of actions placed.
269 template <class Placer>
270 std::uint32_t reconcile(const Instrument& inst,
271 const DesiredQuotes& desired,
272 const Oms& oms,
273 Timestamp now,
274 Placer&& place) noexcept {
275 InstState& st = state_[inst.id.value];
276 st.pulled = false;
277 st.paused = false;
278 st.resumable = false;
279 st.desired = desired;
280 std::uint32_t actions = 0;
281 for (Side side : {Side::Buy, Side::Sell}) {
282 const auto s = static_cast<std::size_t>(side);
283 const auto& want = st.desired.side(side);
284 // A level the ladder does not ask for and whose slot is empty has nothing to reconcile.
285 const std::uint32_t n = std::max(static_cast<std::uint32_t>(want.size()), st.used[s]);
286 std::uint32_t used = 0;
287 for (std::uint32_t lvl = 0; lvl < n; ++lvl) {
288 Slot& slot = st.slots[s][lvl];
289 const bool has_want = !held(st, side) && lvl < want.size() && want[lvl].qty.is_positive();
290 actions += reconcile_slot(inst,
291 st,
292 oms,
293 side,
294 lvl,
295 slot,
296 has_want ? want[lvl] : Level{},
297 st.desired.account(side, lvl),
298 st.desired.value(side, lvl),
299 st.desired.keep(side, lvl),
300 now,
301 place);
302 if (!slot.empty()) used = lvl + 1;
303 }
304 st.used[s] = used;
305 }
306 return actions;
307 }
308
309 // Cancels every working quote on the instrument and suppresses re-quoting until the
310 // next reconcile(). With keep_desired (a pause, not a decision to stop quoting) the last desired
311 // quotes are kept for resume(), unless the instrument was already pulled.
312 template <class Placer>
313 std::uint32_t pull_quotes(const Instrument& inst,
314 const Oms& oms,
315 Placer&& place,
316 bool keep_desired = false) noexcept {
317 InstState& st = state_[inst.id.value];
318 if (keep_desired) {
319 st.resumable = st.resumable || (!st.pulled && !st.desired.empty());
320 } else {
321 st.resumable = false;
322 st.desired.clear();
323 }
324 st.pulled = true;
325 st.paused = false;
326 std::uint32_t actions = 0;
327 for (Side side : {Side::Buy, Side::Sell}) {
328 for (std::uint32_t lvl = 0; lvl < kMaxQuoteLevels; ++lvl) {
329 Slot& slot = st.slots[static_cast<std::size_t>(side)][lvl];
330 slot.apply_want = false;
331 if (!owns(oms, inst, side, lvl, slot)) continue;
332 const Order& o = oms.get(slot.handle);
333 if (!o.is_working()) continue; // pending: cannot touch; a later pull will catch it
334 actions += cancel(inst, side, lvl, slot, place);
335 }
336 }
337 return actions;
338 }
339
340 // Holds the instrument's orders as they are until resume(): no New, Cancel or Replace, and a
341 // target recorded for a slot is dropped (resume() re-applies the whole ladder). The last desired
342 // quotes are kept for resume() unless the instrument was already pulled. Sends nothing.
343 void pause_quotes(InstrumentId id) noexcept {
344 InstState& st = state_[id.value];
345 st.resumable = st.resumable || (!st.pulled && !st.desired.empty());
346 st.paused = true;
347 for (auto& side : st.slots)
348 for (Slot& slot : side) slot.apply_want = false;
349 }
350 [[nodiscard]] bool paused(InstrumentId id) const noexcept { return state_[id.value].paused; }
351
352 // Ends a pause and re-applies the desired quotes kept by pause_quotes() or
353 // pull_quotes(keep_desired) to the orders resting now. Returns the actions placed.
354 template <class Placer>
355 std::uint32_t resume(const Instrument& inst,
356 const Oms& oms,
358 Placer&& place) noexcept {
359 InstState& st = state_[inst.id.value];
360 st.paused = false;
361 if (!st.resumable) return 0;
362 const DesiredQuotes desired = st.desired;
363 return reconcile(inst, desired, oms, now, place);
364 }
365 [[nodiscard]] bool resumable(InstrumentId id) const noexcept {
366 return state_[id.value].resumable;
368
369 // Holds `side` on every instrument (on) or ends the hold (off). While it holds, the side's levels
370 // count as empty wherever quotes are reconciled; the desired quotes keep them for reapply().
371 void hold_side(Side side, bool on) noexcept { side_held_[static_cast<std::size_t>(side)] = on; }
372 [[nodiscard]] bool side_held(Side side) const noexcept {
373 return side_held_[static_cast<std::size_t>(side)];
375 // The same for one instrument (the engine's hazard groups, core/hazard.hpp): the side is held
376 // while either hold is on.
377 void hold_side(InstrumentId id, Side side, bool on) noexcept {
378 state_[id.value].side_held[static_cast<std::size_t>(side)] = on;
380 [[nodiscard]] bool side_held(InstrumentId id, Side side) const noexcept {
381 return state_[id.value].side_held[static_cast<std::size_t>(side)];
383 // Cancels the instrument's working quotes on `side`, now; one still pending is cancelled when its
384 // ack arrives. The other side and the desired quotes stay as they are. Returns the actions
385 // placed.
386 template <class Placer>
387 std::uint32_t pull_side(
388 const Instrument& inst, Side side, const Oms& oms, Timestamp now, Placer&& place) noexcept {
389 InstState& st = state_[inst.id.value];
390 const auto s = static_cast<std::size_t>(side);
391 std::uint32_t actions = 0;
392 std::uint32_t used = 0;
393 for (std::uint32_t lvl = 0; lvl < st.used[s]; ++lvl) {
394 Slot& slot = st.slots[s][lvl];
395 actions += reconcile_slot(
396 inst, st, oms, side, lvl, slot, Level{}, VenueId{}, kNoValue, kNoValue, now, place);
397 if (!slot.empty()) used = lvl + 1;
398 }
399 st.used[s] = used;
400 return actions;
401 }
402 // Re-applies the instrument's last desired quotes (a hold ended); one pulled or paused stays as
403 // it is.
404 template <class Placer>
405 std::uint32_t reapply(const Instrument& inst,
406 const Oms& oms,
408 Placer&& place) noexcept {
409 InstState& st = state_[inst.id.value];
410 if (st.pulled || st.paused || st.desired.empty()) return 0;
411 const DesiredQuotes desired = st.desired;
412 return reconcile(inst, desired, oms, now, place);
413 }
414
415 // Instruments whose quotes were held back for want of an order token, oldest first.
416 [[nodiscard]] bool starved() const noexcept { return !starved_.empty(); }
417 [[nodiscard]] std::size_t starved_count() const noexcept { return starved_.size(); }
418 // Takes the oldest starved instrument off the queue (the caller checks starved() first).
419 [[nodiscard]] InstrumentId pop_starved() noexcept {
420 const InstrumentId id{starved_.front()};
421 starved_.pop();
422 state_[id.value].starved = false;
423 return id;
424 }
425 // Re-applies the last desired quotes of an instrument popped from the starved queue; one that was
426 // pulled meanwhile stays pulled. An action held back again queues it at the back.
427 template <class Placer>
428 std::uint32_t retry(const Instrument& inst,
429 const Oms& oms,
431 Placer&& place) noexcept {
432 InstState& st = state_[inst.id.value];
433 if (st.pulled || st.paused) return 0;
434 ++stats_.retries;
435 const DesiredQuotes desired = st.desired;
436 return reconcile(inst, desired, oms, now, place);
437 }
438
439 // Instruments with a New held back by a side's reject backoff: once the backoff ends, the engine
440 // re-applies their last desired quotes (retry_backed_off), so a side comes back by itself rather
441 // than at the strategy's next requote, which a quiet market may not bring.
442 [[nodiscard]] bool backed_off() const noexcept { return !backed_off_.empty(); }
443 [[nodiscard]] bool backoff_due(Timestamp now) const noexcept { return now >= backoff_due_; }
444 // Takes each instrument whose held-back side may quote at `now` off the queue and calls
445 // f(InstrumentId); the others stay queued.
446 template <class F>
447 void take_backoff_due(Timestamp now, F&& f) noexcept {
448 if (now < backoff_due_) return;
449 backoff_due_ = Timestamp{kNever};
450 for (std::size_t n = backed_off_.size(); n > 0; --n) {
451 const std::uint16_t id = backed_off_.front();
452 backed_off_.pop();
453 InstState& st = state_[id];
454 if (now < st.retry_at) {
455 static_cast<void>(backed_off_.try_push(id));
456 backoff_due_ = std::min(backoff_due_, st.retry_at);
457 continue;
458 }
459 st.retry_at = Timestamp{kNever};
460 f(InstrumentId{id});
461 }
462 }
463 // Re-applies the last desired quotes of an instrument take_backoff_due() handed out; one pulled
464 // meanwhile stays pulled. A side still backing off queues it again.
465 template <class Placer>
466 std::uint32_t retry_backed_off(const Instrument& inst,
467 const Oms& oms,
469 Placer&& place) noexcept {
470 InstState& st = state_[inst.id.value];
471 if (st.pulled || st.paused) return 0;
472 const DesiredQuotes desired = st.desired;
473 return reconcile(inst, desired, oms, now, place);
474 }
475
476 // Instruments with a level priced out ([capacity]): the engine re-applies their last desired
477 // quotes (retry_priced) when the price may have fallen, a window rolling over or a fill giving a
478 // slot back.
479 [[nodiscard]] bool priced_out() const noexcept { return !priced_.empty(); }
480 // Takes every instrument queued now off the queue and calls f(InstrumentId); one priced out
481 // again during f queues itself for the next time.
482 template <class F>
483 void take_priced(F&& f) noexcept {
484 for (std::size_t n = priced_.size(); n > 0; --n) {
485 const std::uint16_t id = priced_.front();
486 priced_.pop();
487 state_[id].priced = false;
488 f(InstrumentId{id});
489 }
490 }
491 template <class Placer>
492 std::uint32_t retry_priced(const Instrument& inst,
493 const Oms& oms,
495 Placer&& place) noexcept {
496 InstState& st = state_[inst.id.value];
497 if (st.pulled || st.paused) return 0;
498 const DesiredQuotes desired = st.desired;
499 return reconcile(inst, desired, oms, now, place);
500 }
501
502 // Feed every OmsUpdate here. A terminal update frees the slot it belonged to and, if a target
503 // was recorded for it (cancel-then-new, or a requote that met the order pending), places the New
504 // now. The ack of an order that was pending at a requote applies the recorded target. While the
505 // instrument is pulled, an order that has just become working is cancelled instead.
506 template <class Placer>
507 void on_order_update(const OmsUpdate& u,
508 const Instrument& inst,
509 const Oms& oms,
510 Timestamp now,
511 Placer&& place) noexcept {
512 if (!is_quote_tag(u.order.user_tag)) return;
513 const Side side = static_cast<Side>((u.order.user_tag >> 8) & 1U);
514 const std::uint32_t lvl = u.order.user_tag & 0xFFU;
515 if (lvl >= kMaxQuoteLevels || u.order.instrument != inst.id) return;
516 InstState& st = state_[inst.id.value];
517 Slot& slot = st.slots[static_cast<std::size_t>(side)][lvl];
518 if (u.refused != RejectReason::None) on_venue_reject(st, side, u.refused, now);
519 if (!u.terminal) {
520 if ((u.prev == OrderState::PendingNew || u.prev == OrderState::PendingReplace) &&
521 u.refused == RejectReason::None && u.order.is_working())
522 st.backoff[static_cast<std::size_t>(side)] = Duration{}; // the venue takes this side
523 if (!u.handle.valid() || slot.handle.idx != u.handle.idx ||
524 !owns(oms, inst, side, lvl, slot) || !u.order.is_working())
525 return;
526 if (st.pulled) {
527 static_cast<void>(cancel(inst, side, lvl, slot, place));
528 return;
529 }
530 if (st.paused) return;
531 if (slot.apply_want &&
532 (u.prev == OrderState::PendingNew || u.prev == OrderState::PendingReplace)) {
533 slot.apply_want = false;
534 static_cast<void>(reconcile_slot(inst,
535 st,
536 oms,
537 side,
538 lvl,
539 slot,
540 held(st, side) ? Level{} : slot.want,
541 slot.want_account,
542 slot.want_value,
543 slot.want_keep,
544 now,
545 place));
546 }
547 return;
548 }
549 // The terminal order was this slot's unless the slot already holds a newer live order. Ids
550 // are not compared: a replace changes the order's id, and a stale id used to drop the update
551 // and leave the slot waiting for a terminal state forever.
552 if (owns(oms, inst, side, lvl, slot)) return;
553 slot.handle = Handle<Order>{};
554 slot.cl_ord_id = ClientOrderId{};
555 slot.awaiting_terminal = false;
556 if (st.pulled || st.paused || !slot.apply_want) return;
557 slot.apply_want = false;
558 if (!slot.want.qty.is_positive() || held(st, side)) return;
559 if (backing_off(st, side, now)) {
560 kept_backoff(st, inst.id, side);
561 return;
562 }
563 submit_new(inst,
564 side,
565 lvl,
566 slot,
567 slot.want,
568 slot.want_account,
569 slot.want_value,
570 now,
571 place,
572 /*deferred=*/true);
573 }
574
575 [[nodiscard]] Handle<Order> slot_handle(InstrumentId id,
576 Side s,
577 std::uint32_t level) const noexcept {
578 return state_[id.value].slots[static_cast<std::size_t>(s)][level].handle;
579 }
580 [[nodiscard]] bool pulled(InstrumentId id) const noexcept { return state_[id.value].pulled; }
581 // The last ladder reconcile() applied to the instrument (empty after a pull).
582 [[nodiscard]] const DesiredQuotes& desired(InstrumentId id) const noexcept {
583 return state_[id.value].desired;
585 // The instrument waits for an order token (a New or Replace was rate-limited).
586 [[nodiscard]] bool starved(InstrumentId id) const noexcept { return state_[id.value].starved; }
587 // The side is in its venue-reject backoff at `now`.
588 [[nodiscard]] bool backing_off(InstrumentId id, Side s, Timestamp now) const noexcept {
589 return backing_off(state_[id.value], s, now);
591
592 private:
593 struct Slot {
594 Handle<Order> handle{};
595 ClientOrderId cl_ord_id{};
596 Timestamp last_requote{};
597 Level want{}; // target to apply once the slot's pending order resolves
598 VenueId want_account{}; // ... and its account (DesiredQuotes::account)
599 float want_value = kNoValue; // ... and its value and keep value ([capacity])
600 float want_keep = kNoValue;
601 bool paid = false; // the move that is freeing the slot was bought: its New is not priced
602 bool apply_want = false; // `want` is pending (qty 0: no quote on this level)
603 bool awaiting_terminal = false;
604 // No order and nothing to apply: reconciling the slot against no level changes nothing.
605 [[nodiscard]] bool empty() const noexcept {
606 return !handle.valid() && !apply_want && !awaiting_terminal;
607 }
608 };
609 struct InstState {
610 Slot slots[2][kMaxQuoteLevels];
611 // Per side, 1 + the highest level whose slot was not empty after the last reconcile(). A slot
612 // only fills at a level the ladder asks for, so no slot above max(used, ladder) holds anything.
613 std::uint32_t used[2] = {};
614 DesiredQuotes desired;
615 bool pulled = false;
616 bool paused = false; // pause_quotes(): orders held where they rest until resume()
617 bool resumable = false; // paused or pulled with keep_desired: resume() re-applies `desired`
618 bool side_held[2] = {}; // hold_side(id, side)
619 bool venue_no_replace = false; // the venue cannot amend in place (set_replace)
620 bool replace = false; // supports_replace && !venue_no_replace
621 Duration backoff[2] = {}; // current backoff per side (0 = none)
622 Timestamp blocked_until[2] = {}; // no News or Replaces on the side before this
623 bool starved = false; // queued in starved_
624 bool priced = false; // queued in priced_
625 Timestamp retry_at{kNever}; // queued in backed_off_: a held-back side's backoff ends
626 };
627
628 [[nodiscard]] bool held(const InstState& st, Side side) const noexcept {
629 const auto i = static_cast<std::size_t>(side);
630 return side_held_[i] || st.side_held[i];
631 }
632 [[nodiscard]] static bool backing_off(const InstState& st, Side side, Timestamp now) noexcept {
633 const auto i = static_cast<std::size_t>(side);
634 return st.backoff[i].ns > 0 && now < st.blocked_until[i];
635 }
636 // A New on `side` waits for the side's backoff: queue the instrument to be re-applied when it
637 // ends.
638 void kept_backoff(InstState& st, InstrumentId id, Side side) noexcept {
639 ++stats_.kept_backoff;
640 const Timestamp until = st.blocked_until[static_cast<std::size_t>(side)];
641 if (st.retry_at.ns == kNever) {
642 if (!backed_off_.try_push(static_cast<std::uint16_t>(id.value))) return;
643 }
644 st.retry_at = std::min(st.retry_at, until);
645 backoff_due_ = std::min(backoff_due_, until);
646 }
647 void on_venue_reject(InstState& st, Side side, RejectReason reason, Timestamp now) noexcept {
648 // A post-only cross is a stale price, fixed by the next requote, and a replace of an order the
649 // venue no longer has lost a race with its fill or cancel; other venue rejects (no balance,
650 // filters, a price outside the venue's band, permissions) repeat until something changes.
652 return;
653 back_off(st, side, now);
654 }
655 // Starts or doubles the side's backoff.
656 void back_off(InstState& st, Side side, Timestamp now) noexcept {
657 if (params_.reject_backoff.ns <= 0) return;
658 const auto i = static_cast<std::size_t>(side);
659 Duration next = st.backoff[i].ns <= 0 ? params_.reject_backoff : st.backoff[i] + st.backoff[i];
660 if (params_.reject_backoff_max.ns > 0 && next.ns > params_.reject_backoff_max.ns)
661 next = params_.reject_backoff_max;
662 st.backoff[i] = next;
663 st.blocked_until[i] = now + next;
664 ++stats_.reject_backoffs;
665 }
666
667 // One slot of reconcile(): keep, cancel, replace or place so the slot converges on `target`
668 // (qty 0: no quote).
669 template <class Placer>
670 std::uint32_t reconcile_slot(const Instrument& inst,
671 InstState& st,
672 const Oms& oms,
673 Side side,
674 std::uint32_t lvl,
675 Slot& slot,
676 Level target,
677 VenueId account,
678 float value,
679 float keep,
680 Timestamp now,
681 Placer& place) noexcept {
682 const bool has_want = target.qty.is_positive();
683 if (owns(oms, inst, side, lvl, slot)) {
684 const Order& o = oms.get(slot.handle);
685 if (is_pending(o.state)) {
686 // Cannot touch it now; apply the target when its ack or terminal update arrives.
687 ++stats_.skipped_pending;
688 slot.want = target;
689 slot.want_account = account;
690 slot.want_value = value;
691 slot.want_keep = keep;
692 slot.apply_want = true;
693 return 0;
694 }
695 slot.apply_want = false;
696 if (!has_want) return cancel(inst, side, lvl, slot, place);
697 // Asked for on another account: an order cannot move between accounts.
698 if (account.valid() && o.venue != account) {
699 slot.want = target;
700 slot.want_account = account;
701 slot.want_value = value;
702 slot.want_keep = keep;
703 slot.apply_want = true;
704 slot.paid = false;
705 return cancel(inst, side, lvl, slot, place);
706 }
707 const std::int64_t dpx = (target.price - o.price).abs().raw / inst.tick.raw;
708 const bool qty_ok = static_cast<Int128>(o.leaves_qty().raw) * 10'000 >=
709 static_cast<Int128>(target.qty.raw) * params_.min_qty_bps;
710 if (dpx < params_.min_requote_ticks && qty_ok) {
711 ++stats_.kept_hysteresis;
712 return 0;
713 }
714 if (now - slot.last_requote < params_.min_requote_interval) {
715 ++stats_.kept_interval;
716 return 0;
717 }
718 // A side backing off sends no replace either: the order is cancelled and the New waits.
719 if (st.replace && !backing_off(st, side, now) && !relocates(place, o, account)) {
720 QuoteAction a{QuoteActionKind::Replace,
721 inst.id,
722 side,
723 lvl,
724 slot.handle,
726 target.price,
727 target.qty,
728 params_.post_only,
729 false,
730 false,
731 false};
732 a.value = value;
733 a.keep = keep;
734 if (!place(a)) {
735 if (a.priced_out) {
736 // Away from the touch the resting order is not wanted where it is: cancel it (free)
737 // and buy its New as a new order. Towards the touch it stays.
738 if (side == Side::Buy ? target.price < o.price : target.price > o.price)
739 return move(inst, side, lvl, slot, target, account, value, keep, place);
740 ++stats_.priced_out_moves;
741 queue_priced(st, inst.id);
742 } else if (a.held) {
743 ++stats_.kept_room;
744 } else {
745 held_back(st, inst.id, a, now);
746 }
747 return 0;
748 }
749 ++stats_.replaces;
750 slot.last_requote = now;
751 return 1;
752 }
753 return move(inst, side, lvl, slot, target, account, value, keep, place);
754 }
755 // No resting order in this slot. A cancel still in flight keeps the order live (Pending*,
756 // handled above), so any earlier order is final in the OMS and there is nothing left to
757 // wait for, even if its terminal update never reached on_order_update().
758 slot.handle = Handle<Order>{};
759 slot.awaiting_terminal = false;
760 slot.apply_want = false;
761 if (!has_want) return 0;
762 if (backing_off(st, side, now)) {
763 kept_backoff(st, inst.id, side);
764 return 0;
765 }
766 return submit_new(inst, side, lvl, slot, target, account, value, now, place);
767 }
768
769 // The placer wants the resting order moved by cancel and new (Engine::relocates).
770 template <class Placer>
771 [[nodiscard]] static bool relocates(Placer& place, const Order& o, VenueId account) noexcept {
772 if constexpr (requires {
773 { place.relocates(o, account) } -> std::convertible_to<bool>;
774 }) {
775 return place.relocates(o, account);
776 } else {
777 return false;
778 }
779 }
780
781 // The slot's order is live and really the slot's: order slots are reused by later orders.
782 [[nodiscard]] static bool owns(const Oms& oms,
783 const Instrument& inst,
784 Side side,
785 std::uint32_t lvl,
786 const Slot& slot) noexcept {
787 if (!slot.handle.valid() || !oms.is_live(slot.handle)) return false;
788 const Order& o = oms.get(slot.handle);
789 return o.instrument == inst.id && o.user_tag == make_tag(side, lvl);
790 }
791
792 // Cancel-then-new of a resting quote to `target`: the cancel carries the target, so the placer
793 // can hold a move towards the touch (QuoteAction::held) and leave the order where it is.
794 template <class Placer>
795 std::uint32_t move(const Instrument& inst,
796 Side side,
797 std::uint32_t lvl,
798 Slot& slot,
799 Level target,
800 VenueId account,
801 float value,
802 float keep,
803 Placer& place) noexcept {
804 QuoteAction a{QuoteActionKind::Cancel,
805 inst.id,
806 side,
807 lvl,
808 slot.handle,
810 target.price,
811 target.qty,
812 false,
813 false,
814 false,
815 false};
816 a.value = value;
817 a.keep = keep;
818 slot.want = target;
819 slot.want_account = account;
820 slot.want_value = value;
821 slot.want_keep = keep;
822 slot.apply_want = true;
823 slot.paid = false;
824 if (!place(a)) {
825 if (a.priced_out) {
826 slot.apply_want = false;
827 ++stats_.priced_out_moves;
828 queue_priced(state_[inst.id.value], inst.id);
829 } else if (a.held) {
830 slot.apply_want = false;
831 ++stats_.kept_room;
832 } else {
833 ++stats_.rejected;
834 }
835 return 0;
836 }
837 slot.paid = a.paid;
838 ++stats_.cancels;
839 slot.awaiting_terminal = true;
840 return 1;
841 }
842
843 template <class Placer>
844 std::uint32_t cancel(
845 const Instrument& inst, Side side, std::uint32_t lvl, Slot& slot, Placer& place) noexcept {
846 QuoteAction a{QuoteActionKind::Cancel,
847 inst.id,
848 side,
849 lvl,
850 slot.handle,
852 Price{},
853 Qty{},
854 false,
855 false,
856 false,
857 false};
858 if (!place(a)) {
859 ++stats_.rejected;
860 return 0;
861 }
862 ++stats_.cancels;
863 slot.awaiting_terminal = true;
864 return 1;
865 }
866 template <class Placer>
867 std::uint32_t submit_new(const Instrument& inst,
868 Side side,
869 std::uint32_t lvl,
870 Slot& slot,
871 Level target,
872 VenueId account,
873 float value,
874 Timestamp now,
875 Placer& place,
876 bool deferred = false) noexcept {
877 QuoteAction a{QuoteActionKind::New,
878 inst.id,
879 side,
880 lvl,
881 Handle<Order>{},
883 target.price,
884 target.qty,
885 params_.post_only,
886 deferred,
887 false,
888 false,
889 account};
890 a.value = value;
891 a.paid = deferred && slot.paid;
892 slot.paid = false;
893 if (!place(a) || !a.handle.valid()) {
894 if (a.priced_out) {
895 ++stats_.priced_out;
896 queue_priced(state_[inst.id.value], inst.id);
897 } else if (a.withheld) {
898 ++stats_.kept_balance;
899 } else {
900 held_back(state_[inst.id.value], inst.id, a, now);
901 }
902 return 0;
903 }
904 ++stats_.news;
905 slot.handle = a.handle;
906 slot.cl_ord_id = a.cl_ord_id;
907 slot.last_requote = now;
908 slot.awaiting_terminal = false;
909 return 1;
910 }
911
912 // A failed New or Replace: a rate-limited one waits in the starved queue, anything else was
913 // refused; refused for its price, its side backs off.
914 void held_back(InstState& st, InstrumentId id, const QuoteAction& a, Timestamp now) noexcept {
915 if (!a.rate_limited) {
916 ++stats_.rejected;
917 if (a.refused == RejectReason::PriceCollar || a.refused == RejectReason::FatFinger)
918 back_off(st, a.side, now);
919 return;
920 }
921 ++stats_.kept_rate_limit;
922 if (st.starved) return;
923 st.starved = true;
924 static_cast<void>(starved_.try_push(static_cast<std::uint16_t>(id.value)));
925 }
926
927 void queue_priced(InstState& st, InstrumentId id) noexcept {
928 if (st.priced) return;
929 st.priced = priced_.try_push(static_cast<std::uint16_t>(id.value));
930 }
931
932 void resolve_replace() noexcept {
933 for (InstState& st : state_) st.replace = params_.supports_replace && !st.venue_no_replace;
934 }
935
936 QuoteParams params_;
937 QuoteStats stats_{};
938 InstState state_[kMaxInstruments] = {};
939 RingBuffer<std::uint16_t, kMaxInstruments> starved_; // each instrument at most once
940 RingBuffer<std::uint16_t, kMaxInstruments> priced_; // priced out, each at most once
941 // Instruments with a New held back by a backoff (InstState::retry_at), each at most once, and the
942 // earliest retry_at among them.
943 RingBuffer<std::uint16_t, kMaxInstruments> backed_off_;
944 Timestamp backoff_due_{kNever};
945 bool side_held_[2] = {}; // hold_side()
946};
947
948} // namespace fastmm
The capacity price ([capacity]): every action that uses an order slot is a purchase.
Definition oms.hpp:159
const Order & get(Handle< Order > h) const noexcept
Definition oms.hpp:627
Definition quote_manager.hpp:242
const QuoteParams & params() const noexcept
Definition quote_manager.hpp:248
bool starved() const noexcept
Instruments whose quotes were held back for want of an order token, oldest first.
Definition quote_manager.hpp:418
std::uint32_t retry_backed_off(const Instrument &inst, const Oms &oms, Timestamp now, Placer &&place) noexcept
Re-applies the last desired quotes of an instrument take_backoff_due() handed out; one pulled meanwhi...
Definition quote_manager.hpp:468
std::uint32_t reapply(const Instrument &inst, const Oms &oms, Timestamp now, Placer &&place) noexcept
Re-applies the instrument's last desired quotes (a hold ended); one pulled or paused stays as it is.
Definition quote_manager.hpp:407
std::uint32_t reconcile(const Instrument &inst, const DesiredQuotes &desired, const Oms &oms, Timestamp now, Placer &&place) noexcept
Reconciles desired vs resting for one instrument. Returns the number of actions placed.
Definition quote_manager.hpp:272
std::uint32_t pull_side(const Instrument &inst, Side side, const Oms &oms, Timestamp now, Placer &&place) noexcept
Cancels the instrument's working quotes on side, now; one still pending is cancelled when its ack arr...
Definition quote_manager.hpp:389
std::uint32_t retry_priced(const Instrument &inst, const Oms &oms, Timestamp now, Placer &&place) noexcept
Definition quote_manager.hpp:494
bool backed_off() const noexcept
Instruments with a New held back by a side's reject backoff: once the backoff ends,...
Definition quote_manager.hpp:444
std::size_t starved_count() const noexcept
Definition quote_manager.hpp:419
static constexpr std::uint32_t make_tag(Side s, std::uint32_t level) noexcept
user_tag encoding so a terminal OmsUpdate can be mapped back to its slot.
Definition quote_manager.hpp:263
const QuoteStats & stats() const noexcept
Definition quote_manager.hpp:253
void take_priced(F &&f) noexcept
Takes every instrument queued now off the queue and calls f(InstrumentId); one priced out again durin...
Definition quote_manager.hpp:485
bool resumable(InstrumentId id) const noexcept
Definition quote_manager.hpp:367
void pause_quotes(InstrumentId id) noexcept
Holds the instrument's orders as they are until resume(): no New, Cancel or Replace,...
Definition quote_manager.hpp:345
bool side_held(Side side) const noexcept
Definition quote_manager.hpp:374
bool backing_off(InstrumentId id, Side s, Timestamp now) const noexcept
The side is in its venue-reject backoff at now.
Definition quote_manager.hpp:590
std::uint32_t retry(const Instrument &inst, const Oms &oms, Timestamp now, Placer &&place) noexcept
Re-applies the last desired quotes of an instrument popped from the starved queue; one that was pulle...
Definition quote_manager.hpp:430
static constexpr bool is_quote_tag(std::uint32_t tag) noexcept
Definition quote_manager.hpp:266
bool paused(InstrumentId id) const noexcept
Definition quote_manager.hpp:352
bool backoff_due(Timestamp now) const noexcept
Definition quote_manager.hpp:445
const DesiredQuotes & desired(InstrumentId id) const noexcept
The last ladder reconcile() applied to the instrument (empty after a pull).
Definition quote_manager.hpp:584
std::uint32_t resume(const Instrument &inst, const Oms &oms, Timestamp now, Placer &&place) noexcept
Ends a pause and re-applies the desired quotes kept by pause_quotes() or pull_quotes(keep_desired) to...
Definition quote_manager.hpp:357
void set_replace(InstrumentId id, bool on) noexcept
Whether the instrument's venue amends in place (default true; supports_replace still applies).
Definition quote_manager.hpp:255
bool replaces(InstrumentId id) const noexcept
Definition quote_manager.hpp:260
void set_params(const QuoteParams &p) noexcept
Definition quote_manager.hpp:249
Handle< Order > slot_handle(InstrumentId id, Side s, std::uint32_t level) const noexcept
Definition quote_manager.hpp:577
bool pulled(InstrumentId id) const noexcept
Definition quote_manager.hpp:582
void hold_side(Side side, bool on) noexcept
Holds side on every instrument (on) or ends the hold (off).
Definition quote_manager.hpp:373
void on_order_update(const OmsUpdate &u, const Instrument &inst, const Oms &oms, Timestamp now, Placer &&place) noexcept
Feed every OmsUpdate here.
Definition quote_manager.hpp:509
InstrumentId pop_starved() noexcept
Takes the oldest starved instrument off the queue (the caller checks starved() first).
Definition quote_manager.hpp:421
void take_backoff_due(Timestamp now, F &&f) noexcept
Takes each instrument whose held-back side may quote at now off the queue and calls f(InstrumentId); ...
Definition quote_manager.hpp:449
std::uint32_t pull_quotes(const Instrument &inst, const Oms &oms, Placer &&place, bool keep_desired=false) noexcept
Cancels every working quote on the instrument and suppresses re-quoting until the next reconcile().
Definition quote_manager.hpp:315
bool priced_out() const noexcept
Instruments with a level priced out ([capacity]): the engine re-applies their last desired quotes (re...
Definition quote_manager.hpp:481
Instrument: the single definition of a tradable contract (5.3 / 5.15).
Event bus message structs (5.5).
Definition strategy.hpp:23
StrongId< struct ClientOrderTag, std::uint64_t, 0 > ClientOrderId
48-bit: session_epoch(16) << 32 | seq(32). 0 is never issued.
Definition strong_id.hpp:41
constexpr Duration milliseconds(std::int64_t v) noexcept
Definition time.hpp:38
constexpr bool is_pending(OrderState s) noexcept
Definition enums.hpp:93
RejectReason
Ordered exactly like RiskEngine::check_new evaluates them (5.7), followed by OMS / transport / venue ...
Definition enums.hpp:123
Side
Definition enums.hpp:12
Fixed< struct QtyTag > Qty
Definition fixed_point.hpp:333
constexpr float kNoValue
What an order or a quote level is worth when the strategy did not say ([capacity],...
Definition order.hpp:98
StrongId< struct InstrumentTag, std::uint32_t > InstrumentId
Dense index into the engine's instrument table.
Definition strong_id.hpp:38
Fixed< struct PriceTag > Price
Definition fixed_point.hpp:332
constexpr std::size_t kMaxInstruments
Definition instrument.hpp:26
QuoteActionKind
Definition quote_manager.hpp:187
constexpr std::size_t kMaxQuoteLevels
Definition quote_manager.hpp:81
StrongId< struct VenueTag, std::uint8_t > VenueId
Definition strong_id.hpp:39
Order management (5.8): a Pool<Order> + OpenHashMap<ClientOrderId, Handle> + a ring of recently termi...
Definition quote_manager.hpp:83
StaticVector< Level, kMaxQuoteLevels > asks
Definition quote_manager.hpp:85
std::array< float, kMaxQuoteLevels > bid_keep
Definition quote_manager.hpp:95
std::array< VenueId, kMaxQuoteLevels > ask_account
Definition quote_manager.hpp:89
void clear() noexcept
Definition quote_manager.hpp:97
void set_value(Side s, std::uint32_t lvl, float value, float keep=kNoValue) noexcept
Sets the value of level lvl of a side and, optionally, the keep value of its resting order.
Definition quote_manager.hpp:111
std::array< float, kMaxQuoteLevels > ask_value
Definition quote_manager.hpp:94
std::array< float, kMaxQuoteLevels > ask_keep
Definition quote_manager.hpp:96
const StaticVector< Level, kMaxQuoteLevels > & side(Side s) const noexcept
Definition quote_manager.hpp:122
VenueId account(Side s, std::uint32_t lvl) const noexcept
Definition quote_manager.hpp:107
float keep(Side s, std::uint32_t lvl) const noexcept
Definition quote_manager.hpp:119
bool bid(Price px, Qty qty) noexcept
Append the next level of a side (index 0 first).
Definition quote_manager.hpp:128
StaticVector< Level, kMaxQuoteLevels > bids
index 0 = closest to mid
Definition quote_manager.hpp:84
bool empty() const noexcept
Definition quote_manager.hpp:125
std::array< float, kMaxQuoteLevels > bid_value
What a level's order is worth, in the quote currency ([capacity], core/capacity_price....
Definition quote_manager.hpp:93
std::array< VenueId, kMaxQuoteLevels > bid_account
The pool account a level's order goes from (NewOrderRequest::account); invalid = the engine's automat...
Definition quote_manager.hpp:88
bool ask(Price px, Qty qty) noexcept
Definition quote_manager.hpp:131
void uncross(Price tick) noexcept
Never cross ourselves at level 0: an ask at or below the best bid moves one tick above it.
Definition quote_manager.hpp:146
float value(Side s, std::uint32_t lvl) const noexcept
Definition quote_manager.hpp:116
Definition time.hpp:22
std::int64_t ns
Definition time.hpp:23
Definition strong_id.hpp:54
Definition instrument.hpp:29
InstrumentId id
4
Definition instrument.hpp:42
One price level. Shared by book messages and the L2 book.
Definition messages.hpp:31
Definition oms.hpp:83
Definition order.hpp:16
constexpr bool is_working() const noexcept
Definition order.hpp:51
Definition quote_manager.hpp:189
bool paid
A Cancel that moves, output: the move was bought, so the New after it is not priced again.
Definition quote_manager.hpp:221
bool post_only
Definition quote_manager.hpp:198
bool withheld
New output: not sent, the balance does not cover it (deferred only)
Definition quote_manager.hpp:202
VenueId account
New: the pool account to send it from (invalid = automatic)
Definition quote_manager.hpp:206
bool rate_limited
New / Replace output: not sent, no order token for it now (QuoteParams::token_reserve,...
Definition quote_manager.hpp:205
float value
New / Replace / a Cancel that moves: the level's value and the resting order's keep value (DesiredQuo...
Definition quote_manager.hpp:215
bool deferred
New: the target was recorded while an earlier order held the slot and is placed now that it ended (on...
Definition quote_manager.hpp:201
bool priced_out
Output: not sent, the action is worth less than the order slot it takes ([capacity]).
Definition quote_manager.hpp:218
InstrumentId instrument
Definition quote_manager.hpp:191
Price price
Definition quote_manager.hpp:196
ClientOrderId cl_ord_id
New output (the placer fills it)
Definition quote_manager.hpp:195
float keep
Definition quote_manager.hpp:216
RejectReason refused
New / Replace output: the pre-trade check that refused it (None: sent, or not refused by one).
Definition quote_manager.hpp:208
Side side
Definition quote_manager.hpp:192
bool held
Cancel / Replace output: not sent, the move goes towards the touch and the order's account is short o...
Definition quote_manager.hpp:212
std::uint32_t level
Definition quote_manager.hpp:193
Qty qty
Definition quote_manager.hpp:197
QuoteActionKind kind
Definition quote_manager.hpp:190
Handle< Order > handle
Cancel/Replace input; New output.
Definition quote_manager.hpp:194
Definition quote_manager.hpp:159
Duration min_requote_interval
Definition quote_manager.hpp:161
Duration reject_backoff
0 = no backoff after venue rejects
Definition quote_manager.hpp:168
std::int64_t min_qty_bps
leaves >= 80 % of desired -> qty ok
Definition quote_manager.hpp:162
Duration reject_backoff_max
Definition quote_manager.hpp:169
std::int64_t send_reserve
Order tokens every quote leaves for the orders a strategy sends itself (ctx.send, a hedge or an unwin...
Definition quote_manager.hpp:179
std::int64_t min_requote_ticks
|desired - resting| < this -> keep
Definition quote_manager.hpp:160
std::int64_t token_reserve
Order tokens ([risk] orders_per_sec) a quote that does not reduce the position leaves for the orders ...
Definition quote_manager.hpp:173
bool replace_all_venues
Replace only if every instrument's venue can (the engine before replace was per venue; set to replay ...
Definition quote_manager.hpp:166
bool post_only
Definition quote_manager.hpp:167
std::int64_t room_reserve
Orders an account's venue order windows (ctx.order_budget) keep for News into empty levels and for mo...
Definition quote_manager.hpp:184
bool supports_replace
off: never replace; on: where the instrument's venue can
Definition quote_manager.hpp:163
Definition quote_manager.hpp:224
std::uint64_t rejected
Definition quote_manager.hpp:231
std::uint64_t kept_hysteresis
Definition quote_manager.hpp:228
std::uint64_t cancels
Definition quote_manager.hpp:226
std::uint64_t kept_interval
Definition quote_manager.hpp:229
std::uint64_t reject_backoffs
venue rejects that started or extended a side backoff
Definition quote_manager.hpp:232
std::uint64_t priced_out
News not sent: worth less than their order slot ([capacity])
Definition quote_manager.hpp:238
std::uint64_t kept_room
moves towards the touch held: the account is short of room
Definition quote_manager.hpp:236
std::uint64_t priced_out_moves
moves not made, the order left where it rests
Definition quote_manager.hpp:239
std::uint64_t retries
retry() calls that re-applied a starved instrument's quotes
Definition quote_manager.hpp:237
std::uint64_t kept_backoff
News withheld because their side was backing off.
Definition quote_manager.hpp:233
std::uint64_t news
Definition quote_manager.hpp:225
std::uint64_t skipped_pending
Definition quote_manager.hpp:230
std::uint64_t kept_balance
deferred News withheld: the balance no longer covers them
Definition quote_manager.hpp:234
std::uint64_t replaces
Definition quote_manager.hpp:227
std::uint64_t kept_rate_limit
News / Replaces held back for want of an order token.
Definition quote_manager.hpp:235
Rep value
Definition strong_id.hpp:26
constexpr bool valid() const noexcept
Definition strong_id.hpp:31
Definition time.hpp:45
Time primitives.