111 std::string_view
name =
"hedge";
153 enum class State : std::uint8_t {
184 void reset() noexcept {
193 sources_[n_sources_++] = Leg{id,
Ratio{}, 0};
198 hedges_[n_hedges_++] = Leg{id, tolerance, 0};
204 bool start(Ctx& ctx,
const Config& cfg)
noexcept {
208 if (cfg_.
maker && !works_orders<Ctx>) {
209 FASTMM_LOG_ERROR(
"{}: maker mode needs a context that cancels orders; not hedging",
213 if (n_sources_ == 0 || n_hedges_ == 0) {
217 const auto n = ctx.instruments().size();
218 for (std::size_t i = 0; i < n_sources_ + n_hedges_; ++i) {
221 FASTMM_LOG_ERROR(
"{}: instrument {} is not among the {} configured; not hedging",
228 if (inst.inverse() || !inst.contract_multiplier.is_positive()) {
231 ctx.venue_name(inst.venue),
235 for (std::size_t j = 0; j < i; ++j) {
236 if (leg(j).
id ==
id) {
239 ctx.venue_name(inst.venue),
245 for (std::size_t i = 0; i < n_sources_ + n_hedges_; ++i)
246 venue_of_[i] = ctx.instrument(leg(i).
id).venue;
251 void set_config(
const Config& cfg)
noexcept { cfg_ = cfg; }
253 if (hedge < n_hedges_) hedges_[hedge].tolerance = tolerance;
261 for (std::size_t i = 0; i <
kMaxHedges; ++i) {
272 if (!ready_ || !
is_leg(fill.instrument))
return false;
281 if (!ready_ || !u.terminal)
return false;
282 const Order& o = u.order;
283 const std::int64_t now = ctx.now().ns;
284 if (derisk_open_ && o.
cl_ord_id == derisk_id_) {
285 derisk_open_ =
false;
286 if (
uncertain(u)) hold_uncertain(o, now);
291 if (k < 0)
return false;
292 if (maker_open_ && o.
cl_ord_id == maker_id_) {
296 maker_cancel_ =
false;
297 forget_in_ticker(
static_cast<std::size_t
>(k), o);
300 next_ns_ = std::max(next_ns_, now + cfg_.
repeg.
ns);
302 failed(ctx,
static_cast<std::size_t
>(k), now);
306 failed(ctx,
static_cast<std::size_t
>(k), now);
308 if (
uncertain(u)) hold_uncertain(o, now);
316 if (ready_ && hedge_index(
id) >= 0)
update(ctx);
323 const int k = ready_ ? hedge_index(
id) : -1;
324 if (k < 0)
return false;
325 if (m.bid_px.is_positive() && m.ask_px > m.bid_px)
326 ticker_[
static_cast<std::size_t
>(k)] =
327 Ticker{m.bid_px, m.ask_px, m.bid_qty, m.ask_qty, ctx.now().ns};
342 if (!ready_ || m.channel == 0)
return;
343 const std::uint32_t bit = 1U << (m.channel & 31U);
344 for (std::size_t i = 0; i < n_sources_ + n_hedges_; ++i) {
346 if (venue_of_[i] != m.hdr.venue)
continue;
358 if (!ready_ || !
involves(m.hdr.venue))
return false;
367 const std::int64_t now = ctx.now().ns;
368 if (cfg_.
maker && !maker_open_) adopt(ctx);
369 if (maker_open_ && keep_maker(ctx, now))
return;
370 if (in_flight(ctx)) {
374 if (ctx.reconciling()) {
378 if (now < next_ns_) {
398 unblock(
"the residual is under the hedge minimum");
403 unblock(
"the residual is inside the band");
406 std::size_t first = n_hedges_;
407 for (std::size_t i = 0; i < n_hedges_; ++i) {
413 if (first == n_hedges_) {
416 const Instrument& h = ctx.instrument(hedges_[0].
id);
420 ctx.venue_name(h.
venue),
431 const std::optional<NewOrderRequest> sized = order_on(ctx, first,
residual, &cut);
434 unblock(
"the residual is under the hedge minimum");
438 std::size_t k = first;
439 if (!covered(ctx, req)) {
441 for (std::size_t j = first + 1; j < n_hedges_; ++j) {
444 const std::optional<NewOrderRequest> r = order_on(ctx, j,
residual, &cut_j);
445 if (r && covered(ctx, *r)) {
453 if (k == n_hedges_) {
458 "{}: hedge {} {} @ {} held: no usable hedge venue's balance covers it ({} on {}:{}); "
459 "{} unhedged until a balance report or a fill changes that",
465 ctx.venue_name(ctx.instrument(req.
instrument).venue),
477 if (k != active_) switch_to(ctx, k, now);
478 unblock(
"a hedge instrument takes the residual again");
479 const auto sent = ctx.send(req);
493 if (cfg_.
maker && !cross_) {
497 maker_sent_ns_ = now;
498 maker_move_ns_ = now;
502 if (cut && !split_) {
505 "{}: [risk] admits {} {} @ {} of the {} to hedge on {}:{}: it goes out in pieces",
511 ctx.venue_name(ctx.instrument(req.
instrument).venue),
512 ctx.instrument(req.
instrument).symbol.view());
518 [[nodiscard]]
bool ready() const noexcept {
return ready_; }
519 [[nodiscard]]
bool halted() const noexcept {
return halted_; }
521 [[nodiscard]]
bool held() const noexcept {
return held_; }
522 [[nodiscard]]
bool derisking() const noexcept {
return derisking_; }
523 [[nodiscard]]
State state() const noexcept {
return state_; }
524 [[nodiscard]]
const Stats&
stats() const noexcept {
return stats_; }
525 [[nodiscard]] std::size_t
hedge_count() const noexcept {
return n_hedges_; }
527 [[nodiscard]] std::size_t
active() const noexcept {
return active_; }
528 [[nodiscard]]
Qty target() const noexcept {
return target_; }
531 for (std::size_t i = 0; i < n_sources_; ++i) {
532 if (sources_[i].
id ==
id)
return true;
541 for (std::size_t i = 0; i < n_sources_ + n_hedges_; ++i) {
542 if (venue_of_[i] == v)
return true;
549 [[nodiscard]]
Qty residual(
const Ctx& ctx)
const noexcept {
551 for (std::size_t i = 0; i < n_sources_ + n_hedges_; ++i) {
553 r +=
to_base(ctx.instrument(
id), ctx.position(
id).qty);
562 for (std::size_t i = 0; i < n_hedges_; ++i) {
563 const Instrument& inst = ctx.instrument(hedges_[i].
id);
572 [[nodiscard]]
Reason why(
const Ctx& ctx, std::size_t i, std::int64_t now)
const noexcept {
573 const Leg& h = hedges_[i];
574 const VenueId v = venue_of_[n_sources_ + i];
578 const auto& b = ctx.book(h.id);
582 const std::int64_t last = std::max(t.
valid() ? t.
ns : 0, ticker_[i].ns);
592 [[nodiscard]]
bool can_hedge(
const Ctx& ctx)
const noexcept {
593 if (!ready_ || halted_)
return false;
594 const std::int64_t now = ctx.now().
ns;
595 for (std::size_t i = 0; i < n_hedges_; ++i) {
602 [[nodiscard]]
Status status(
const Ctx& ctx)
const noexcept {
606 s.
active =
static_cast<std::uint8_t
>(active_);
622 if (!inst.contract_multiplier.is_positive())
return Qty{};
636 std::uint32_t tag)
noexcept {
640 if (inst.max_qty.is_positive() && qty > inst.max_qty) qty = inst.round_qty(inst.max_qty);
641 if (!qty.
is_positive() || qty < inst.min_qty)
return std::nullopt;
644 if (!best_ask.is_positive())
return std::nullopt;
645 px = inst.round_price(best_ask + best_ask * tolerance,
Side::Buy);
647 if (!best_bid.is_positive())
return std::nullopt;
648 px = inst.round_price(best_bid - best_bid * tolerance,
Side::Sell);
651 if (inst.min_notional.is_positive() && inst.notional(px, qty) < inst.min_notional)
659 [[nodiscard]]
static std::optional<NewOrderRequest>
maker_order(
664 if (inst.max_qty.is_positive() && qty > inst.max_qty) qty = inst.round_qty(inst.max_qty);
665 if (!qty.
is_positive() || qty < inst.min_qty)
return std::nullopt;
668 if (inst.min_notional.is_positive() && inst.notional(px, qty) < inst.min_notional)
682 while (q.
is_positive() && inst.notional(price, q) > cap) q = q - inst.lot;
693 Price price)
noexcept {
694 Qty room = h.max_order_qty;
695 room = std::min(room, side ==
Side::Buy ? h.buy_qty : h.sell_qty);
696 room = std::min(room, side ==
Side::Buy ? h.underlying_buy_qty : h.underlying_sell_qty);
697 Notional notional = h.max_order_notional;
699 std::min(notional, side ==
Side::Buy ? h.exposure_buy_notional : h.exposure_sell_notional);
711 if (room >= r.qty)
return false;
712 const Qty q = inst.round_qty(room);
713 if (!q.
is_positive() || q < inst.min_qty)
return false;
714 if (inst.min_notional.is_positive() && inst.notional(r.price, q) < inst.min_notional)
724 const Order& o = u.order;
725 return u.unresolved_qty.is_positive() ||
730 [[nodiscard]]
static constexpr std::string_view
to_string(
Reason r)
noexcept {
739 return "without a book";
743 return "held by the feed-lag gate";
749 [[nodiscard]]
static constexpr std::string_view
to_string(
State s)
noexcept {
760 return "reconciling";
766 return "unavailable";
770 return "inside the band";
788 std::uint32_t down = 0;
791 [[nodiscard]] Leg& leg(std::size_t i)
noexcept {
792 return i < n_sources_ ? sources_[i] : hedges_[i - n_sources_];
794 [[nodiscard]]
const Leg& leg(std::size_t i)
const noexcept {
795 return i < n_sources_ ? sources_[i] : hedges_[i - n_sources_];
797 [[nodiscard]]
int hedge_index(
InstrumentId id)
const noexcept {
798 for (std::size_t i = 0; i < n_hedges_; ++i) {
799 if (hedges_[i].
id ==
id)
return static_cast<int>(i);
804 void clear_state() noexcept {
806 for (Leg& l : sources_) l.down = 0;
807 for (Leg& l : hedges_) l.down = 0;
809 for (std::size_t i = 0; i <
kMaxHedges; ++i) {
816 unavailable_ =
false;
821 derisk_open_ =
false;
824 derisk_stuck_ =
false;
828 maker_cancel_ =
false;
840 [[nodiscard]]
static RiskHeadroom headroom(
const Ctx& ctx,
InstrumentId id)
noexcept {
841 if constexpr (
requires { ctx.risk_headroom(
id); }) {
842 return ctx.risk_headroom(
id);
844 return RiskHeadroom{};
851 [[nodiscard]] std::optional<NewOrderRequest> order_on(
const Ctx& ctx,
854 bool* cut)
const noexcept {
855 const Leg& h = hedges_[i];
856 const auto& b = ctx.book(h.id);
857 const Instrument& inst = ctx.instrument(h.id);
858 std::optional<NewOrderRequest> r =
859 cfg_.
maker && !cross_
866 inst,
residual, b.best_bid().price, b.best_ask().price, h.tolerance, cfg_.tag);
867 if (r) *cut =
fit_risk(headroom(ctx, h.id), inst, *r);
876 const auto& b = ctx.book(
id);
877 const int k = hedge_index(
id);
879 const Ticker& t = ticker_[
static_cast<std::size_t
>(k)];
880 const Timestamp u = b.last_update();
881 if (t.ns > 0 && (!u.valid() || t.ns >= u.ns)) {
883 const Qty shown = side ==
Side::Buy ? t.bid_qty : t.ask_qty;
885 if constexpr (works_orders<Ctx>) {
887 const Order* o = ctx.order(maker_id_);
888 if (o !=
nullptr && o->instrument ==
id && o->side == side && o->price == px)
889 ours = o->leaves_qty();
892 if (px.is_positive() && (!ours.is_positive() || shown > ours))
return px;
895 if constexpr (
requires { ctx.best_ex_self(
id, side); }) {
896 return ctx.best_ex_self(
id, side).price;
898 return side ==
Side::Buy ? b.best_bid().price : b.best_ask().price;
904 void forget_in_ticker(std::size_t k,
const Order& o)
noexcept {
907 Qty& shown = o.side ==
Side::Buy ? t.bid_qty : t.ask_qty;
908 if (t.ns == 0 || px != o.price)
return;
909 shown = shown - o.leaves_qty();
910 if (!shown.is_positive()) px =
Price{};
918 const auto& b = ctx.book(
id);
925 [[nodiscard]]
bool inside_flat(
const Ctx& ctx,
Qty residual)
const noexcept {
932 bool keep_maker(Ctx& ctx, std::int64_t now)
noexcept {
933 if constexpr (!works_orders<Ctx>) {
937 return keep_maker_on(ctx, now);
941 bool keep_maker_on(Ctx& ctx, std::int64_t now)
noexcept {
942 const Order* o = ctx.order(maker_id_);
945 maker_cancel_ =
false;
949 if (maker_cancel_ ||
is_pending(o->state))
return true;
958 if (!stop && want == o->side) {
963 }
else if (now - maker_move_ns_ >= cfg_.
repeg.
ns) {
964 const Price px = touch(ctx,
id, want);
965 if (px.is_positive() && px != o->price) move =
true;
968 const Qty off = (o->leaves_qty() - need).abs();
969 if (inst.notional(o->price, off) > cfg_.
resize) move =
true;
977 if (stop) working_ =
false;
978 if (ctx.cancel(maker_id_)) {
979 maker_cancel_ =
true;
980 maker_move_ns_ = now;
989 void adopt(
const Ctx& ctx)
noexcept {
990 if constexpr (
requires { ctx.oms(); }) {
991 ctx.oms().for_each_open_order([&](Handle<Order>,
const Order& o) {
992 if (maker_open_ || o.user_tag != cfg_.
tag || hedge_index(o.instrument) < 0)
return;
994 maker_cancel_ =
false;
995 maker_id_ = o.cl_ord_id;
996 maker_k_ =
static_cast<std::size_t
>(hedge_index(o.instrument));
997 maker_sent_ns_ = ctx.now().ns;
1003 template <
class Ctx>
1004 [[nodiscard]]
static bool covered(
const Ctx& ctx,
const NewOrderRequest& r)
noexcept {
1005 return ctx.balance_room(r.instrument, r.side, r.price) >= r.qty;
1008 template <
class Ctx>
1009 [[nodiscard]]
bool in_flight(
const Ctx& ctx)
const noexcept {
1010 if (derisk_open_)
return true;
1011 for (std::size_t i = 0; i < n_hedges_; ++i) {
1013 if (ctx.open_qty(
id,
Side::Buy).is_positive() || ctx.open_qty(
id,
Side::Sell).is_positive())
1019 void hold_uncertain(
const Order& o, std::int64_t now)
noexcept {
1022 if (until > next_ns_) next_ns_ = until;
1024 "{}: order {} ended without the venue saying how; next order in {} ms at the earliest",
1030 template <
class Ctx>
1031 void switch_to(
const Ctx& ctx, std::size_t k, std::int64_t now)
noexcept {
1032 const std::size_t from = active_;
1039 FASTMM_LOG_WARN(
"{}: hedging moves from {}:{} to {}:{}: the first is {}",
1041 ctx.venue_name(ctx.instrument(a).venue),
1042 ctx.instrument(a).symbol.view(),
1043 ctx.venue_name(ctx.instrument(b).venue),
1044 ctx.instrument(b).symbol.view(),
1049 ctx.venue_name(ctx.instrument(b).venue),
1050 ctx.instrument(b).symbol.view(),
1051 ctx.venue_name(ctx.instrument(a).venue),
1052 ctx.instrument(a).symbol.view());
1056 template <
class Ctx>
1057 void failed(Ctx& ctx, std::size_t k, std::int64_t now)
noexcept {
1059 next_ns_ = now + cfg_.
retry.
ns;
1060 const auto n =
static_cast<std::size_t
>(cfg_.
max_failures);
1061 std::size_t& count = failures_[k];
1064 if (halted_ || count < n)
return;
1065 const std::int64_t oldest = fail_ns_[k][(count - n) %
kMaxFailures];
1068 for (std::size_t i = 0; i < n_hedges_; ++i) {
1069 if (i != k && now >= bench_until_[i]) other =
true;
1071 const Instrument& inst = ctx.instrument(hedges_[k].
id);
1072 const std::string_view venue = ctx.venue_name(inst.venue);
1073 const std::string_view sym = inst.symbol.view();
1075 bench_until_[k] = now + cfg_.
bench.
ns;
1078 FASTMM_LOG_WARN(
"{}: {} hedges on {}:{} filled nothing within {} ms: it sits out {} ms",
1090 "{}: {} hedges on {}:{} filled nothing within {} ms: hedging stopped with {} unhedged; "
1091 "restart to resume",
1103 template <
class Ctx>
1104 void blocked(Ctx& ctx,
Qty residual, std::int64_t now)
noexcept {
1113 derisk_stuck_ =
false;
1116 "{}: no hedge instrument has taken {} for {} ms: de-risking the sources in steps of at "
1123 if (now < next_derisk_ns_)
return;
1128 void unblock(std::string_view
why)
noexcept {
1129 unavailable_ =
false;
1131 if (!derisking_)
return;
1136 template <
class Ctx>
1137 void derisk_step(Ctx& ctx,
Qty residual, std::int64_t now)
noexcept {
1139 for (std::size_t i = 0; i < n_sources_; ++i) {
1140 const Leg& s = sources_[i];
1141 const Instrument& inst = ctx.instrument(s.id);
1142 const Qty pos =
to_base(inst, ctx.position(s.id).qty);
1145 if (s.down != 0 || ctx.venue_killed(inst.venue))
continue;
1146 const auto& b = ctx.book(s.id);
1147 if (!b.is_valid())
continue;
1149 if (pos.abs() < base) base = pos.
abs();
1151 std::optional<NewOrderRequest> r =
hedge_order(inst,
1155 cfg_.derisk_tolerance,
1159 if (!inst.is_derivative() && ctx.balance_room(s.id, r->side, r->price) < r->qty)
continue;
1160 NewOrderRequest req = *r;
1161 req.reduce_only =
true;
1162 static_cast<void>(
fit_risk(headroom(ctx, s.id), inst, req));
1164 const auto sent = ctx.send(req);
1168 ctx.venue_name(inst.venue),
1176 derisk_open_ =
true;
1178 derisk_stuck_ =
false;
1182 if (!derisk_stuck_) {
1183 derisk_stuck_ =
true;
1189 std::array<Leg, kMaxSources> sources_{};
1190 std::array<Leg, kMaxHedges> hedges_{};
1191 std::array<VenueId, kMaxSources + kMaxHedges> venue_of_{};
1192 std::size_t n_sources_ = 0;
1193 std::size_t n_hedges_ = 0;
1195 std::int64_t next_ns_ = 0;
1196 std::array<std::size_t, kMaxHedges> failures_{};
1197 std::array<std::array<std::int64_t, kMaxFailures>,
kMaxHedges> fail_ns_{};
1198 std::array<std::int64_t, kMaxHedges> bench_until_{};
1199 std::array<Ticker, kMaxHedges> ticker_{};
1200 std::size_t active_ = 0;
1203 bool ready_ =
false;
1204 bool halted_ =
false;
1206 bool unavailable_ =
false;
1207 bool split_ =
false;
1208 bool working_ =
false;
1210 bool maker_open_ =
false;
1211 bool maker_cancel_ =
false;
1212 bool cross_ =
false;
1214 std::size_t maker_k_ = 0;
1215 std::int64_t maker_sent_ns_ = 0;
1216 std::int64_t maker_move_ns_ = 0;
1218 bool blocked_ =
false;
1219 bool derisking_ =
false;
1220 bool derisk_open_ =
false;
1221 bool derisk_stuck_ =
false;
1222 std::int64_t blocked_ns_ = 0;
1223 std::int64_t next_derisk_ns_ = 0;
Definition config.hpp:502
bool add_hedge(InstrumentId id, Ratio tolerance) noexcept
Definition hedge_executor.hpp:197
std::size_t active() const noexcept
Definition hedge_executor.hpp:526
bool involves(VenueId v) const noexcept
A venue of any leg.
Definition hedge_executor.hpp:539
static constexpr bool fit_risk(const RiskHeadroom &h, const Instrument &inst, NewOrderRequest &r) noexcept
Cuts r to risk_room() when that is less and still an order the instrument takes (the lot,...
Definition hedge_executor.hpp:705
State state() const noexcept
Definition hedge_executor.hpp:522
void update(Ctx &ctx) noexcept
Looks at the positions and sends what they call for; the hooks above end here.
Definition hedge_executor.hpp:365
Qty residual(const Ctx &ctx) const noexcept
Sources plus hedges minus the target, base units.
Definition hedge_executor.hpp:548
bool is_leg(InstrumentId id) const noexcept
Definition hedge_executor.hpp:535
bool on_fill(Ctx &ctx, const Fill &fill) noexcept
A fill of any leg: the residual moved. False for another instrument.
Definition hedge_executor.hpp:271
static constexpr std::size_t kMaxSources
Definition hedge_executor.hpp:102
void on_timer(Ctx &ctx) noexcept
Definition hedge_executor.hpp:333
void restart() noexcept
Clears a halt, the benches and the failure counts; hedging resumes from the positions.
Definition hedge_executor.hpp:259
InstrumentId hedge_id(std::size_t i) const noexcept
Definition hedge_executor.hpp:525
bool ready() const noexcept
Definition hedge_executor.hpp:517
static constexpr bool works_orders
Maker mode looks its order up and cancels it (StrategyContext::order, cancel).
Definition hedge_executor.hpp:107
void set_config(const Config &cfg) noexcept
Parameters may change while running (on_params); the legs may not.
Definition hedge_executor.hpp:252
Status status(const Ctx &ctx) const noexcept
Definition hedge_executor.hpp:601
bool on_order_update(Ctx &ctx, const OmsUpdate &u) noexcept
The end of a hedge or de-risk order: counts a failure or holds, then looks again.
Definition hedge_executor.hpp:280
bool can_hedge(const Ctx &ctx) const noexcept
Some hedge instrument can take a hedge now, and hedging is not halted.
Definition hedge_executor.hpp:591
bool start(Ctx &ctx, const Config &cfg) noexcept
Checks the legs against the instrument table: each is configured, linear, used once,...
Definition hedge_executor.hpp:205
bool under_minimum(const Ctx &ctx, Qty residual) const noexcept
The residual rounds under the lot or min_qty of every hedge instrument: no hedge order can take it,...
Definition hedge_executor.hpp:560
static constexpr Qty to_base(const Instrument &inst, Qty contracts) noexcept
Contracts to base units and back (linear: qty * multiplier).
Definition hedge_executor.hpp:616
Qty target() const noexcept
Definition hedge_executor.hpp:527
static constexpr Qty risk_room(const RiskHeadroom &h, const Instrument &inst, Side side, Price price) noexcept
The largest order of side on inst at price that [risk] admits now: max_order_qty, max_order_notional ...
Definition hedge_executor.hpp:688
static constexpr std::string_view to_string(Reason r) noexcept
Definition hedge_executor.hpp:728
const Stats & stats() const noexcept
Definition hedge_executor.hpp:523
bool is_source(InstrumentId id) const noexcept
Definition hedge_executor.hpp:529
bool held() const noexcept
The last hedge the positions asked for fits no usable instrument's balance.
Definition hedge_executor.hpp:520
static constexpr std::size_t kMaxHedges
Definition hedge_executor.hpp:103
static bool uncertain(const OmsUpdate &u) noexcept
A terminal update whose outcome the venue did not report: the ack timeout cancelled an order the venu...
Definition hedge_executor.hpp:721
void reset() noexcept
Forgets the legs and every state but the stats. Call before adding legs in on_start.
Definition hedge_executor.hpp:185
std::size_t hedge_count() const noexcept
Definition hedge_executor.hpp:524
void set_target(Qty base) noexcept
The net base position the strategy wants to hold; the residual is measured from it.
Definition hedge_executor.hpp:257
bool on_balance(Ctx &ctx, const BalanceMsg &m) noexcept
A balance report of a leg's venue: a held hedge may fit now. False for another venue.
Definition hedge_executor.hpp:357
bool halted() const noexcept
Definition hedge_executor.hpp:518
Reason why(const Ctx &ctx, std::size_t i, std::int64_t now) const noexcept
Why hedge instrument i cannot take a hedge now (Reason::Usable: it can).
Definition hedge_executor.hpp:571
void on_connection(Ctx &, const ConnectionStateMsg &m) noexcept
Order channels (channel != 0) of the legs' venues.
Definition hedge_executor.hpp:341
bool on_book_ticker(Ctx &ctx, InstrumentId id, const BookTickerMsg &m) noexcept
A hedge instrument's book ticker: its touch, newer than the depth book's until the book updates.
Definition hedge_executor.hpp:322
bool derisking() const noexcept
Definition hedge_executor.hpp:521
State
What the last evaluation found.
Definition hedge_executor.hpp:155
@ Resting
a maker hedge rests at the touch
@ Holding
retry backoff or uncertain hold
@ Reconciling
a venue reconciles
@ Unavailable
no hedge instrument is usable
@ Waiting
the residual rounds under the hedge instrument's minimum
@ Halted
every hedge instrument failed max_failures times; restart() clears it
@ Flat
the residual is zero
@ InFlight
a hedge or de-risk order is open
@ Inside
the residual is inside Config::band
@ Idle
not started, or nothing evaluated yet
@ Held
no usable instrument's balance or margin covers the hedge
static constexpr std::size_t kMaxFailures
Definition hedge_executor.hpp:104
bool is_hedge(InstrumentId id) const noexcept
Definition hedge_executor.hpp:528
bool add_source(InstrumentId id) noexcept
Definition hedge_executor.hpp:192
static std::optional< NewOrderRequest > hedge_order(const Instrument &inst, Qty residual, Price best_bid, Price best_ask, Ratio tolerance, std::uint32_t tag) noexcept
The order on inst that brings residual (base units) back towards zero: an IOC of -residual in contrac...
Definition hedge_executor.hpp:629
Reason
Why a hedge instrument is not usable now.
Definition hedge_executor.hpp:170
void on_book(Ctx &ctx, InstrumentId id) noexcept
A hedge instrument's book: a new touch to price against.
Definition hedge_executor.hpp:315
static constexpr Qty to_contracts(const Instrument &inst, Qty base) noexcept
Definition hedge_executor.hpp:619
static constexpr Qty qty_within(const Instrument &inst, Price price, Notional cap) noexcept
The largest quantity of inst whose notional at price is at most cap, rounded down to the lot (zero wh...
Definition hedge_executor.hpp:673
void set_tolerance(std::size_t hedge, Ratio tolerance) noexcept
Definition hedge_executor.hpp:253
static std::optional< NewOrderRequest > maker_order(const Instrument &inst, Qty residual, Price bid, Price ask, std::uint32_t tag) noexcept
The maker hedge of residual on inst: a post-only limit of -residual in contracts, rounded down to the...
Definition hedge_executor.hpp:657
Fixed<Tag>: int64 fixed point with a global 1e-8 scale (ADR-0001).
Strategy hooks (ADR-0012): the hook table, the compile-time checker and the Fill view.
Instrument: the single definition of a tradable contract (5.3 / 5.15).
Asynchronous logging (5.10).
#define FASTMM_LOG_WARN(fmt_str,...)
Definition log.hpp:393
#define FASTMM_LOG_INFO(fmt_str,...)
Definition log.hpp:391
#define FASTMM_LOG_ERROR(fmt_str,...)
Definition log.hpp:395
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
constexpr std::int64_t kFixedScale
1e-8
Definition fixed_point.hpp:23
@ VenueReject
Venue-originated.
Side
Definition enums.hpp:12
Fixed< struct QtyTag > Qty
Definition fixed_point.hpp:333
Fixed< struct NotionalTag > Notional
Definition fixed_point.hpp:334
StrongId< struct InstrumentTag, std::uint32_t > InstrumentId
Dense index into the engine's instrument table.
Definition strong_id.hpp:38
FixedString< 16 > encode_cl_ord_id(ClientOrderId id) noexcept
Definition strong_id.hpp:77
Fixed< struct PriceTag > Price
Definition fixed_point.hpp:332
constexpr std::int64_t mul_raw(Fixed< A > a, Fixed< B > b) noexcept
Generic scaled multiply for fixed*fixed in one domain (e.g. qty * multiplier).
Definition fixed_point.hpp:367
Fixed< RatioTag > Ratio
A dimensionless factor: 1.0 == 1e8 raw, 1 bp == 10'000 raw, so 0.0001 bp is the smallest step.
Definition fixed_point.hpp:336
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...
Order: the OMS's 128-byte record for one order across its lifetime.
RiskLimits: the pre-trade limits a session runs with ([risk] in the configuration),...
The account's holding of one asset on one venue (EventType::Balance), as the venue reports it at its ...
Definition messages.hpp:412
Definition messages.hpp:229
Definition messages.hpp:651
std::int64_t ns
Definition time.hpp:23
constexpr std::int64_t millis() const noexcept
Definition time.hpp:29
One execution as a strategy sees it (on_fill).
Definition hooks.hpp:56
constexpr bool is_negative() const noexcept
Definition fixed_point.hpp:303
static constexpr Fixed max() noexcept
Definition fixed_point.hpp:165
constexpr Fixed abs() const noexcept
Definition fixed_point.hpp:304
static constexpr Fixed from_raw(std::int64_t r) noexcept
Definition fixed_point.hpp:155
constexpr bool is_zero() const noexcept
Definition fixed_point.hpp:301
std::int64_t raw
Definition fixed_point.hpp:152
constexpr bool is_positive() const noexcept
Definition fixed_point.hpp:302
static constexpr Fixed from_int(std::int64_t units) noexcept
Whole units, e.g. from_int(100) == 100.00000000.
Definition fixed_point.hpp:161
Duration stale
a hedge book older than this is unusable; 0: never
Definition hedge_executor.hpp:120
Duration failure_window
Definition hedge_executor.hpp:118
Notional band
Bands, quote currency at the hedge instrument's mid: hedge once the residual is worth band,...
Definition hedge_executor.hpp:128
Duration taker_after
Definition hedge_executor.hpp:136
Duration uncertain_hold
Definition hedge_executor.hpp:119
bool maker
Maker mode: a post-only order at the hedge's own touch instead of an IOC.
Definition hedge_executor.hpp:133
Qty derisk_step
base units per de-risk order; 0: the whole residual
Definition hedge_executor.hpp:123
Notional flat
Definition hedge_executor.hpp:129
int max_failures
within failure_window: bench, or halt on the last one
Definition hedge_executor.hpp:117
std::uint32_t tag
user_tag of hedge orders
Definition hedge_executor.hpp:114
Duration bench
how long a failing instrument sits out
Definition hedge_executor.hpp:121
std::uint32_t derisk_tag
user_tag of de-risk orders
Definition hedge_executor.hpp:115
Duration derisk_interval
Definition hedge_executor.hpp:124
Duration derisk_after
0: never de-risk
Definition hedge_executor.hpp:122
Notional resize
Definition hedge_executor.hpp:135
Duration repeg
Definition hedge_executor.hpp:134
Ratio derisk_tolerance
Definition hedge_executor.hpp:125
Duration retry
after a hedge that filled nothing
Definition hedge_executor.hpp:116
std::string_view name
log prefix
Definition hedge_executor.hpp:113
Definition hedge_executor.hpp:139
std::uint64_t failovers
hedging moved to a later instrument in the list
Definition hedge_executor.hpp:145
std::uint64_t hedges_sent
Definition hedge_executor.hpp:140
std::uint64_t benches
instruments benched after max_failures
Definition hedge_executor.hpp:146
std::uint64_t derisk_orders
Definition hedge_executor.hpp:148
std::uint64_t maker_moves
maker hedges cancelled to follow the touch or re-size
Definition hedge_executor.hpp:149
std::uint64_t halts
Definition hedge_executor.hpp:143
std::uint64_t hedges_held
episodes of a hedge no balance or margin could cover
Definition hedge_executor.hpp:144
std::uint64_t uncertain_ends
ended without the venue saying how
Definition hedge_executor.hpp:142
std::uint64_t maker_refused
maker hedges refused as crossing (post-only)
Definition hedge_executor.hpp:151
std::uint64_t derisk_episodes
Definition hedge_executor.hpp:147
std::uint64_t maker_crossed
maker hedges cancelled at taker_after for an IOC
Definition hedge_executor.hpp:150
std::uint64_t hedge_failures
ended with nothing filled, or refused by the engine
Definition hedge_executor.hpp:141
Definition hedge_executor.hpp:172
Timestamp hold_until
end of the last retry or uncertain hold
Definition hedge_executor.hpp:180
bool held
Definition hedge_executor.hpp:177
std::uint8_t active
index of the hedge instrument hedges go to
Definition hedge_executor.hpp:175
bool halted
Definition hedge_executor.hpp:178
bool in_flight
Definition hedge_executor.hpp:176
bool derisking
Definition hedge_executor.hpp:179
Qty residual
Definition hedge_executor.hpp:174
State state
Definition hedge_executor.hpp:173
Definition instrument.hpp:29
constexpr Qty round_qty(Qty q) const noexcept
Definition instrument.hpp:76
Qty min_qty
8
Definition instrument.hpp:49
VenueId venue
1
Definition instrument.hpp:43
Symbol symbol
21
Definition instrument.hpp:56
constexpr LimitOrder tag(std::uint32_t user_tag) const noexcept
user_tag: echoed in the order record.
Definition order.hpp:154
constexpr LimitOrder ioc() const noexcept
Definition order.hpp:147
constexpr LimitOrder post_only() const noexcept
Definition order.hpp:136
Side side
Definition order.hpp:112
Price price
Definition order.hpp:117
static constexpr LimitOrder limit(InstrumentId instrument, Side side, Price price, Qty qty) noexcept
A GTC limit order, refined with the builder's methods: ctx.send(NewOrderRequest::limit(id,...
Definition order.hpp:174
InstrumentId instrument
Definition order.hpp:106
Qty qty
Definition order.hpp:118
OrderState state
1
Definition order.hpp:36
TimeInForce tif
1 -> 64
Definition order.hpp:35
VenueOrderId venue_order_id
41 -> 49
Definition order.hpp:28
ClientOrderId cl_ord_id
8
Definition order.hpp:27
InstrumentId instrument
4 -> 60
Definition order.hpp:31
RejectReason reject_reason
1
Definition order.hpp:37
Qty cum_qty
8 -> 96
Definition order.hpp:43
How much more each limit admits for one instrument now: the largest order (or number of orders) that ...
Definition risk_limits.hpp:75
std::int64_t ns
Definition time.hpp:46
constexpr bool valid() const noexcept
Definition time.hpp:55
VenueHealth: per venue, how late its market data arrives and how long our orders take to be acknowled...