5#include "fastmm/core/account_pool.hpp"
6#include "fastmm/core/balance_book.hpp"
8#include "fastmm/core/config_macros.hpp"
15#include "fastmm/core/order_budget.hpp"
16#include "fastmm/core/perp_book.hpp"
32template <
class Engine>
33class StrategyContext {
35 using Book =
typename Engine::Book;
41 [[nodiscard]]
Timestamp now() const noexcept {
return e_->now(); }
43 return e_->instrument(
id);
47 return e_->instruments().contains(
id);
60 return e_->own_qty(
id, side, px);
63 return e_->own_qty(
id, side, px, at);
68 return e_->best_ex_self(
id, side);
74 return e_->position(
id);
77 [[nodiscard]]
Portfolio portfolio() const noexcept {
return e_->positions().portfolio(); }
85 return e_->set_quotes(
id, q);
97 std::string_view reason,
99 e_->note_quote(
id, side, reason, budget);
103 void metric(std::string_view name,
double value)
noexcept { e_->metric(name, value); }
106 e_->pull_all_quotes();
111 std::uint32_t level = 0) const noexcept {
113 const Handle<Order> h = e_->quote_manager().slot_handle(
id, side, level);
114 const Oms&
oms = e_->oms();
126 return e_->send_order(req);
130 return e_->cancel_order(
id);
134 return e_->replace_order(
id, px, qty);
138 const Oms&
oms = e_->oms();
148 return e_->queue_ahead(
id);
153 const Oms&
oms = e_->oms();
159 return e_->oms().open_qty(
id, side);
161 [[nodiscard]]
const Oms&
oms() const noexcept {
return e_->oms(); }
166 return e_->add_timer(period,
true, tag);
169 return e_->add_timer(delay,
false, tag);
179 [[nodiscard]]
bool reconciling() const noexcept {
return e_->reconciling(); }
180 [[nodiscard]]
bool killed() const noexcept {
return e_->risk().killed(); }
182 [[nodiscard]]
bool venue_killed(
VenueId v)
const noexcept {
return e_->risk().venue_killed(v); }
203 return e_->risk_headroom(
id);
208 return e_->venue_health(v);
221 return e_->balance(v, asset);
225 [[nodiscard]] Margin
margin(
VenueId v)
const noexcept {
return e_->margin(v); }
227 [[nodiscard]]
bool balances_live() const noexcept {
return e_->balances_live(); }
232 return e_->balance_room(
id, side, px);
238 VenueId account)
const noexcept {
239 return e_->balance_room(
id, side, px, account);
248 [[nodiscard]] PoolMembers
pool(
VenueId primary)
const noexcept {
return e_->pool(primary); }
260 [[nodiscard]] RefPrice
mark(
InstrumentId id)
const noexcept {
return e_->mark(
id); }
264 [[nodiscard]] FundingView
funding(
InstrumentId id)
const noexcept {
return e_->funding(
id); }
267 return e_->perps().row(
id);
272 [[nodiscard]] Xoshiro256ss&
rng() noexcept {
return e_->rng(); }
Dense-by-id table built at startup; symbol lookup is a sorted FlatMap keyed by (venue,...
Definition instrument.hpp:141
const OrderTimes & times(Handle< Order > h) const noexcept
Send and ack times of an open order.
Definition oms.hpp:586
bool is_live(Handle< Order > h) const noexcept
Definition oms.hpp:584
Handle< Order > find(ClientOrderId id) const noexcept
Definition oms.hpp:587
const Order & get(Handle< Order > h) const noexcept
Definition oms.hpp:582
static constexpr bool is_quote_tag(std::uint32_t tag) noexcept
Definition quote_manager.hpp:167
Definition strategy_context.hpp:35
void request_stop() noexcept
Definition strategy_context.hpp:178
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:196
TimerId every(Duration period, std::uint64_t tag=0) noexcept
Definition strategy_context.hpp:161
void pull_quotes(InstrumentId id) noexcept
Definition strategy_context.hpp:85
const Position & position(InstrumentId id) const noexcept
Definition strategy_context.hpp:72
Xoshiro256ss & rng() noexcept
Definition strategy_context.hpp:261
bool killed() const noexcept
Definition strategy_context.hpp:175
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:67
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:192
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:59
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:224
bool balances_live() const noexcept
A venue has reported balances (before that, balance() is unknown everywhere).
Definition strategy_context.hpp:220
Result< ClientOrderId, RejectReason > send(const NewOrderRequest &req) noexcept
RejectReason::InvalidTag: user_tag lies in the QuoteManager's tag range.
Definition strategy_context.hpp:118
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:134
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:213
VenueId pool_primary(VenueId v) const noexcept
The venue whose instruments v takes orders for: its primary, or v itself.
Definition strategy_context.hpp:242
typename Engine::Book Book
Definition strategy_context.hpp:37
Result< void, RejectReason > replace(ClientOrderId id, Price px, Qty qty) noexcept
Definition strategy_context.hpp:129
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:93
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:144
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:107
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:207
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:245
Qty open_qty(InstrumentId id, Side side) const noexcept
Unfilled quantity of our open orders (quotes included) on one side.
Definition strategy_context.hpp:155
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:81
void pull_all_quotes() noexcept
Definition strategy_context.hpp:102
bool contains(InstrumentId id) const noexcept
Definition strategy_context.hpp:47
const Book & book(InstrumentId id) const noexcept
Definition strategy_context.hpp:52
bool quoting_enabled() const noexcept
Definition strategy_context.hpp:170
const InstrumentTable & instruments() const noexcept
Definition strategy_context.hpp:46
Portfolio portfolio() const noexcept
PnL totals over every instrument (a loop over all positions; not for every event).
Definition strategy_context.hpp:76
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:201
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:254
PoolMembers pool(VenueId primary) const noexcept
The accounts that take orders for a venue's instruments ([venues.
Definition strategy_context.hpp:240
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:177
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:101
Qty own_qty(InstrumentId id, Side side, Price px, Timestamp at) const noexcept
Definition strategy_context.hpp:62
RefPrice index(InstrumentId id) const noexcept
Definition strategy_context.hpp:251
const Instrument & instrument(InstrumentId id) const noexcept
Definition strategy_context.hpp:43
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:186
Timestamp now() const noexcept
Definition strategy_context.hpp:42
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:183
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:174
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:250
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:149
TimerId once(Duration delay, std::uint64_t tag=0) noexcept
Definition strategy_context.hpp:164
const PerpRow & perp_state(InstrumentId id) const noexcept
Every field as last reported, open interest included, without the staleness applied.
Definition strategy_context.hpp:256
const Oms & oms() const noexcept
Definition strategy_context.hpp:158
bool cancel_timer(TimerId id) noexcept
Definition strategy_context.hpp:167
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:218
Result< void, RejectReason > cancel(ClientOrderId id) noexcept
Definition strategy_context.hpp:125
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:16
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
KillReason
Why a kill switch (global or one venue's) was tripped.
Definition enums.hpp:523
@ 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
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
constexpr std::size_t kMaxQuoteLevels
Definition quote_manager.hpp:54
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.
Definition quote_manager.hpp:56
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:30
When the order's working leg (the order, or its latest replacement once acknowledged) was sent and ac...
Definition order.hpp:58
PnL totals over every instrument (StrategyContext::portfolio()), in the reporting currency when the t...
Definition position.hpp:39
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:50
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...