5#include "fastmm/core/account_pool.hpp"
6#include "fastmm/core/balance_book.hpp"
9#include "fastmm/core/config_macros.hpp"
12#include "fastmm/core/fill_cluster.hpp"
14#include "fastmm/core/hazard.hpp"
18#include "fastmm/core/order_budget.hpp"
19#include "fastmm/core/perp_book.hpp"
20#include "fastmm/core/pool_homes.hpp"
31#include <initializer_list>
38template <
class Engine>
39class StrategyContext {
41 using Book =
typename Engine::Book;
47 [[nodiscard]]
Timestamp now() const noexcept {
return e_->now(); }
49 return e_->instrument(
id);
53 return e_->instruments().contains(
id);
66 return e_->own_qty(
id, side, px);
69 return e_->own_qty(
id, side, px, at);
74 return e_->best_ex_self(
id, side);
80 return e_->position(
id);
83 [[nodiscard]]
Portfolio portfolio() const noexcept {
return e_->positions().portfolio(); }
91 return e_->set_quotes(
id, q);
118 std::string_view reason,
120 e_->note_quote(
id, side, reason, budget);
124 void metric(std::string_view name,
double value)
noexcept { e_->metric(name, value); }
134 e_->quote_context(
id, side, std::span<const double>(values.begin(), values.size()));
137 e_->quote_context(
id, side, values);
141 const std::span<const double> v(values.begin(), values.size());
147 return e_->quote_context(
id, side);
152 e_->quote_context_names(std::span<const std::string_view>(names.begin(), names.size()));
155 e_->quote_context_names(names);
159 e_->pull_all_quotes();
164 std::uint32_t level = 0) const noexcept {
166 const Handle<Order> h = e_->quote_manager().slot_handle(
id, side, level);
167 const Oms&
oms = e_->oms();
179 return e_->send_order(req);
183 return e_->cancel_order(
id);
187 return e_->replace_order(
id, px, qty);
191 const Oms&
oms = e_->oms();
201 return e_->queue_ahead(
id);
212 return e_->oms().open_qty(
id, side);
214 [[nodiscard]]
const Oms&
oms() const noexcept {
return e_->oms(); }
219 return e_->add_timer(period,
true, tag);
222 return e_->add_timer(delay,
false, tag);
228 [[nodiscard]]
bool quoting_enabled() const noexcept {
return e_->quoting_enabled(); }
232 [[nodiscard]]
bool reconciling() const noexcept {
return e_->reconciling(); }
233 [[nodiscard]]
bool killed() const noexcept {
return e_->risk().killed(); }
239 [[nodiscard]]
bool venue_killed(
VenueId v)
const noexcept {
return e_->risk().venue_killed(v); }
241 [[nodiscard]] std::string_view
venue_name(
VenueId v)
const noexcept {
return e_->venue_name(v); }
266 return e_->risk_headroom(
id);
271 return e_->venue_health(v);
276 [[nodiscard]] FillClusterView
fill_cluster() const noexcept {
return e_->fill_cluster(); }
283 [[nodiscard]] std::uint32_t
hazard_groups() const noexcept {
return e_->hazard_groups(); }
285 return e_->hazard_group(
id);
287 [[nodiscard]] HazardView
hazard(std::uint32_t group)
const noexcept {
return e_->hazard(group); }
288 void hazard_add(std::uint32_t group,
Side side,
double amount)
noexcept {
289 e_->hazard_add(group, side, amount);
294 [[nodiscard]] OrderBudget
order_budget(
VenueId v)
const noexcept {
return e_->order_budget(v); }
305 return e_->balance(v, asset);
309 [[nodiscard]] Margin
margin(
VenueId v)
const noexcept {
return e_->margin(v); }
311 [[nodiscard]]
bool balances_live() const noexcept {
return e_->balances_live(); }
316 return e_->balance_room(
id, side, px);
323 return e_->balance_room(
id, side, px, account);
332 [[nodiscard]] PoolMembers
pool(
VenueId primary)
const noexcept {
return e_->pool(primary); }
348 return e_->placement(primary);
353 return e_->holding(
id, account);
356 [[nodiscard]] std::uint32_t
pieces(
InstrumentId id)
const noexcept {
return e_->pieces(
id); }
361 [[nodiscard]]
bool sweeping(
VenueId primary)
const noexcept {
return e_->sweeping(primary); }
367 return e_->set_home(
id, account);
374 e_->set_funding_need(
id, quote);
377 return e_->funding_need(
id);
385 [[nodiscard]] RefPrice
mark(
InstrumentId id)
const noexcept {
return e_->mark(
id); }
386 [[nodiscard]] RefPrice
index(
InstrumentId id)
const noexcept {
return e_->index(
id); }
389 [[nodiscard]] FundingView
funding(
InstrumentId id)
const noexcept {
return e_->funding(
id); }
392 return e_->perps().row(
id);
397 [[nodiscard]] Xoshiro256ss&
rng() noexcept {
return e_->rng(); }
The capacity price ([capacity]): every action that uses an order slot is a purchase.
Dense-by-id table built at startup; symbol lookup is a sorted FlatMap keyed by (venue,...
Definition instrument.hpp:147
const OrderTimes & times(Handle< Order > h) const noexcept
Send and ack times of an open order.
Definition oms.hpp:631
bool is_live(Handle< Order > h) const noexcept
Definition oms.hpp:629
Handle< Order > find(ClientOrderId id) const noexcept
Definition oms.hpp:644
const Order & get(Handle< Order > h) const noexcept
Definition oms.hpp:627
static constexpr bool is_quote_tag(std::uint32_t tag) noexcept
Definition quote_manager.hpp:266
Definition strategy_context.hpp:41
void request_stop() noexcept
Definition strategy_context.hpp:235
RiskHeadroom risk_headroom(InstrumentId id) const noexcept
What each [risk] limit still admits on the instrument now: the next check uses the same inputs,...
Definition strategy_context.hpp:257
void hazard_add(std::uint32_t group, Side side, double amount) noexcept
Definition strategy_context.hpp:280
void quote_context(InstrumentId id, Side side, std::initializer_list< double > values) noexcept
What the strategy knows about its quotes on a side, for the record of their fills: up to kQuoteContex...
Definition strategy_context.hpp:129
Placement placement(VenueId primary) const noexcept
The pool's placement: Placement::Home sends adding orders to the home and reducing orders to the acco...
Definition strategy_context.hpp:336
TimerId every(Duration period, std::uint64_t tag=0) noexcept
Definition strategy_context.hpp:212
void pull_quotes(InstrumentId id) noexcept
Definition strategy_context.hpp:91
const Position & position(InstrumentId id) const noexcept
Definition strategy_context.hpp:78
void quote_context_names(std::initializer_list< std::string_view > names) noexcept
Names for the values, in slot order (at most 23 bytes each), for the store and the tools that show th...
Definition strategy_context.hpp:147
Xoshiro256ss & rng() noexcept
Definition strategy_context.hpp:383
bool killed() const noexcept
Definition strategy_context.hpp:226
Level best_ex_self(InstrumentId id, Side side) const noexcept
The book's best level on one side with own_qty taken out; a level that was only ours is skipped.
Definition strategy_context.hpp:73
std::uint32_t hazard_group(InstrumentId id) const noexcept
Definition strategy_context.hpp:276
const FeeRates & fees(InstrumentId id) const noexcept
Maker and taker rates of the instrument, in cbps of the notional (positive: a fee).
Definition strategy_context.hpp:253
Qty own_qty(InstrumentId id, Side side, Price px) const noexcept
Our resting quantity that the venue's feed shows at (id, side, px): the book and the tickers of a liv...
Definition strategy_context.hpp:65
Qty balance_room(InstrumentId id, Side side, Price px) const noexcept
Largest quantity of id on side at px the balance covers (a spot buy's quote with the taker fee,...
Definition strategy_context.hpp:306
bool balances_live() const noexcept
A venue has reported balances (before that, balance() is unknown everywhere).
Definition strategy_context.hpp:302
Result< ClientOrderId, RejectReason > send(const NewOrderRequest &req) noexcept
RejectReason::InvalidTag: user_tag lies in the QuoteManager's tag range.
Definition strategy_context.hpp:169
void set_funding_need(InstrumentId id, Notional quote) noexcept
The quote the instrument's bids want resting (a spot instrument's quote asset, a derivative's settlem...
Definition strategy_context.hpp:362
const Order * order(ClientOrderId id) const noexcept
An open order by id, or nullptr once it is terminal. Valid until the next context call.
Definition strategy_context.hpp:185
bool set_home(InstrumentId id, VenueId account) noexcept
Moves the instrument's home to account (journaled).
Definition strategy_context.hpp:354
Balance balance(VenueId v, std::string_view asset) const noexcept
One asset of a venue's account: the venue's last report (on_balance) less what this engine's orders h...
Definition strategy_context.hpp:295
bool sweeping(VenueId primary) const noexcept
The pool sweeps now ([venues.
Definition strategy_context.hpp:350
VenueId pool_primary(VenueId v) const noexcept
The venue whose instruments v takes orders for: its primary, or v itself.
Definition strategy_context.hpp:324
Notional funding_need(InstrumentId id) const noexcept
Definition strategy_context.hpp:365
bool reduce_only(InstrumentId id) const noexcept
Reduce-only mode holds the instrument (fastmm-ctl reduce-only, [risk] reduce_only): an order or repla...
Definition strategy_context.hpp:230
CapacityQuote capacity(VenueId v) const noexcept
What an order slot on account v costs now ([capacity], core/capacity_price.hpp): a new order or a mov...
Definition strategy_context.hpp:289
typename Engine::Book Book
Definition strategy_context.hpp:43
Result< void, RejectReason > replace(ClientOrderId id, Price px, Qty qty) noexcept
Definition strategy_context.hpp:180
void note_quote(InstrumentId id, Side side, std::string_view reason, Notional budget={}) noexcept
Why the strategy asks what it asks on a side, for the operator: a short reason ("inventory_cap",...
Definition strategy_context.hpp:112
std::optional< Qty > queue_ahead(ClientOrderId id) const noexcept
Estimated quantity resting ahead of an open order at its price: the l2_queue fill model's queue model...
Definition strategy_context.hpp:195
const Order * working_quote(InstrumentId id, Side side, std::uint32_t level=0) const noexcept
The open order in a quote slot (level 0 is closest to the mid), or nullptr.
Definition strategy_context.hpp:158
std::uint32_t hazard_groups() const noexcept
The hazard groups ([[risk.hazard]], core/hazard.hpp), numbered in the order of the configuration.
Definition strategy_context.hpp:275
OrderBudget order_budget(VenueId v) const noexcept
What the [risk] rate limit and the venue's own limits still admit (core/order_budget....
Definition strategy_context.hpp:286
bool account_usable(VenueId v) const noexcept
The automatic routing would send to v now: its order link is live and its kill switch is not engaged ...
Definition strategy_context.hpp:327
Qty open_qty(InstrumentId id, Side side) const noexcept
Unfilled quantity of our open orders (quotes included) on one side.
Definition strategy_context.hpp:206
std::uint32_t pieces(InstrumentId id) const noexcept
Accounts other than the home that hold a tradable amount of the instrument.
Definition strategy_context.hpp:345
bool set_quotes(InstrumentId id, const DesiredQuotes &q) noexcept
Returns false when the quotes were ignored: quoting is disabled (control pull, kill switch,...
Definition strategy_context.hpp:87
void pull_all_quotes() noexcept
Definition strategy_context.hpp:153
bool contains(InstrumentId id) const noexcept
Definition strategy_context.hpp:53
const Book & book(InstrumentId id) const noexcept
Definition strategy_context.hpp:58
bool quoting_enabled() const noexcept
Definition strategy_context.hpp:221
const InstrumentTable & instruments() const noexcept
Definition strategy_context.hpp:52
Portfolio portfolio() const noexcept
PnL totals over every instrument (a loop over all positions; not for every event).
Definition strategy_context.hpp:82
VenueHealthView venue_health(VenueId v) const noexcept
Feed lag and order round trip of a venue (core/venue_health.hpp), and whether the feed-lag gate ([ris...
Definition strategy_context.hpp:262
void request_checkpoint() noexcept
Asks for a checkpoint in the journal once the events waiting now are handled: state(),...
Definition strategy_context.hpp:244
FundingView funding(InstrumentId id) const noexcept
The venue's funding of a perpetual: the rate it will apply at next, per interval, and over(d),...
Definition strategy_context.hpp:376
PoolMembers pool(VenueId primary) const noexcept
The accounts that take orders for a venue's instruments ([venues.
Definition strategy_context.hpp:322
VenueId home(InstrumentId id) const noexcept
The account the instrument's position lives on; invalid when its pool has no [venues.
Definition strategy_context.hpp:332
bool venue_killed(VenueId v) const noexcept
One venue's kill switch: new orders to it are refused and set_quotes ignores its instruments.
Definition strategy_context.hpp:232
std::string_view venue_name(VenueId v) const noexcept
The venue's [venues.<name>], for log lines ("venue <id>" when the session names none).
Definition strategy_context.hpp:234
void metric(std::string_view name, double value) noexcept
A number the strategy publishes by name, for the status file (a budget split, a model's state): the f...
Definition strategy_context.hpp:120
void quiet_book(InstrumentId id, Price lo, Price hi) noexcept
on_book of id is called only once its mid is at or beyond lo or hi, or the book is not valid: a strat...
Definition strategy_context.hpp:106
FillClusterView fill_cluster() const noexcept
The fill-cluster guard ([risk] fill_cluster_names, core/fill_cluster.hpp): which sides it holds now,...
Definition strategy_context.hpp:268
Qty own_qty(InstrumentId id, Side side, Price px, Timestamp at) const noexcept
Definition strategy_context.hpp:68
RefPrice index(InstrumentId id) const noexcept
Definition strategy_context.hpp:373
Qty holding(InstrumentId id, VenueId account) const noexcept
What one account of the pool holds of the instrument: a spot instrument's base balance there,...
Definition strategy_context.hpp:341
const Instrument & instrument(InstrumentId id) const noexcept
Definition strategy_context.hpp:49
void trip_kill(KillReason reason) noexcept
Trips the global kill switch: quoting stops, quotes are pulled and every working order is cancelled.
Definition strategy_context.hpp:247
Timestamp now() const noexcept
Definition strategy_context.hpp:48
void quote_context(InstrumentId id, std::initializer_list< double > values) noexcept
Both sides at once.
Definition strategy_context.hpp:136
HazardView hazard(std::uint32_t group) const noexcept
Definition strategy_context.hpp:279
void request_batch_end() noexcept
Asks for one on_batch_end call once the engine has handled every event that is waiting now (the feed ...
Definition strategy_context.hpp:240
bool reconciling() const noexcept
A venue's orders and position are being reconciled, or have not been yet since the session started (a...
Definition strategy_context.hpp:225
RefPrice mark(InstrumentId id) const noexcept
The venue's mark and index price of a derivative (on_perp_state), with the time each arrived.
Definition strategy_context.hpp:372
const OrderTimes * order_times(ClientOrderId id) const noexcept
Send and ack times of an open order, or nullptr (OmsUpdate::times has them in on_order_update and,...
Definition strategy_context.hpp:200
TimerId once(Duration delay, std::uint64_t tag=0) noexcept
Definition strategy_context.hpp:215
void watch(InstrumentId id, bool on) noexcept
market-data hooks only for what the strategy watches, and needs no more: a multiverse hands an engine...
Definition strategy_context.hpp:102
const PerpRow & perp_state(InstrumentId id) const noexcept
Every field as last reported, open interest included, without the staleness applied.
Definition strategy_context.hpp:378
const Oms & oms() const noexcept
Definition strategy_context.hpp:209
bool cancel_timer(TimerId id) noexcept
Definition strategy_context.hpp:218
Margin margin(VenueId v) const noexcept
The venue's margin: its account-wide margin where it reports one, else the settlement asset of its fi...
Definition strategy_context.hpp:300
Result< void, RejectReason > cancel(ClientOrderId id) noexcept
Definition strategy_context.hpp:176
Minimal expected<T, E>: the hot path never throws, it returns Result.
Fee rates per instrument, in centi-basis-points (integer) of the fill notional.
Fixed<Tag>: int64 fixed point with a global 1e-8 scale (ADR-0001).
Instrument: the single definition of a tradable contract (5.3 / 5.15).
L2Book<kMaxDepth>: price-aggregated book for crypto feeds (5.4).
Definition strategy.hpp:23
KillReason
Why a kill switch (global or one venue's) was tripped.
Definition enums.hpp:603
@ Balance
the account's holding of one asset on one venue (BalanceMsg)
@ InvalidTag
a direct order used a user_tag in the QuoteManager's range
Side
Definition enums.hpp:12
Fixed< struct QtyTag > Qty
Definition fixed_point.hpp:333
constexpr Unexpected< std::decay_t< E > > fail(E &&e) noexcept
Definition result.hpp:25
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 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...
Order: the OMS's 128-byte record for one order across its lifetime.
Position tracking, average-cost method (5.8).
QuoteManager (5.6): diffs the strategy's DesiredQuotes against the orders resting at the venue and em...
RiskLimits: the pre-trade limits a session runs with ([risk] in the configuration),...
xoshiro256** - the engine's only source of randomness; seeded from config and written to the journal ...
Strongly typed identifiers.
What an action costs on one account now (ctx.capacity).
Definition capacity_price.hpp:109
Definition quote_manager.hpp:83
One maker/taker pair. 1 cbps == 0.01 bps == 1e-6 of the notional.
Definition fees.hpp:26
Definition strong_id.hpp:54
constexpr bool valid() const noexcept
Definition strong_id.hpp:58
Definition instrument.hpp:29
One price level. Shared by book messages and the L2 book.
Definition messages.hpp:31
When the order's working leg (the order, or its latest replacement once acknowledged) was sent and ac...
Definition order.hpp:60
PnL totals over every instrument (StrategyContext::portfolio()), in the reporting currency when the t...
Definition position.hpp:71
Definition position.hpp:20
How much more each limit admits for one instrument now: the largest order (or number of orders) that ...
Definition risk_limits.hpp:75
What StrategyContext::venue_health returns. Durations are zero until the first sample.
Definition venue_health.hpp:29
VenueHealth: per venue, how late its market data arrives and how long our orders take to be acknowled...