Strategy API¶
The C++ interface a strategy implements and the engine context it calls. #include "fastmm/strategy.hpp" brings in everything; the headers are listed in Public API.
Every code block on this page is compiled by tests/docs/strategy_api_doc_test.cpp (ctest docs.strategy_api).
Strategy class¶
A strategy is a default-constructible class with a static name() and a static schema(): the concept StrategyLike. StrategyBase<Params> provides schema() and the parameters.
Member of StrategyBase<Params> |
Meaning |
|---|---|
params() -> const Params& |
the current parameters, read-only |
configure(const ParamMap&) -> std::optional<std::string> |
applies name -> value strings and runs validate(); returns the first error and leaves the parameters unchanged. Startup only |
describe_params() -> std::string |
name=value pairs that parse back to the same values |
verify_strategy<S>() checks every hook against stand-in context and book types, so a wrong hook fails in the strategy header.
Hooks¶
Hooks are public member functions, and every hook is optional.
void on_start(auto& ctx) noexcept {
hit(kStart);
static_cast<void>(ctx.every(params().timer_ms, /*tag=*/7));
}
void on_stop(auto& /*ctx*/) noexcept { hit(kStop); }
void on_book(auto& ctx, InstrumentId id, const auto& book) noexcept {
hit(kBook);
if (!book.is_valid()) return ctx.pull_quotes(id);
DesiredQuotes q;
q.bid(book.best_bid().price, params().quote_qty);
q.ask(book.best_ask().price, params().quote_qty);
static_cast<void>(ctx.set_quotes(id, q));
}
void on_book_ticker(auto& /*ctx*/, InstrumentId /*id*/, const BookTickerMsg& /*m*/) noexcept {
hit(kBookTicker);
}
void on_trade(auto& /*ctx*/, InstrumentId /*id*/, const TradeMsg& /*m*/) noexcept { hit(kTrade); }
void on_option_ticker(auto& /*ctx*/, InstrumentId /*id*/, const OptionTickerMsg& /*m*/) noexcept {
hit(kOptionTicker);
}
void on_fill(auto& /*ctx*/, const Fill& /*fill*/) noexcept { hit(kFill); }
void on_order_update(auto& /*ctx*/, const OmsUpdate& /*u*/) noexcept { hit(kOrderUpdate); }
void on_timer(auto& ctx, TimerId /*id*/, std::uint64_t tag) noexcept {
if (tag != 7) return;
hit(kTimer);
// Once: a price off the tick is refused by the engine's own check (on_risk_reject).
if (count(kTimer) == 1)
static_cast<void>(ctx.send(NewOrderRequest::limit(
InstrumentId{0}, Side::Buy, Price::from_decimal("99.995").value(), params().quote_qty)));
}
void on_connection(auto& /*ctx*/, const ConnectionStateMsg& /*m*/) noexcept { hit(kConnection); }
void on_quoting(auto& /*ctx*/, bool /*enabled*/) noexcept { hit(kQuoting); }
void on_params(auto& /*ctx*/) noexcept { hit(kParams); }
void on_balance(auto& /*ctx*/, const BalanceMsg& /*m*/) noexcept { hit(kBalance); }
void on_perp_state(auto& /*ctx*/, InstrumentId /*id*/, const PerpStateMsg& /*m*/) noexcept {
hit(kPerpState);
}
void on_risk_reject(auto& /*ctx*/, const RiskReject& r) noexcept {
if (r.reason == RejectReason::InvalidTick && !r.replace) hit(kRiskReject);
}
| Hook | Called |
|---|---|
on_start(ctx) |
once, before the first event; ctx.quoting_enabled() is true in a dry run, whose orders stay on paper |
on_stop(ctx) |
once, when the engine finishes; shutdown's cancel-all pulls the quotes |
on_book(ctx, id, book) |
after a snapshot or delta is applied, the position marked and risk updated; the book may be invalid (empty or crossed) |
on_book_ticker(ctx, id, m) |
a top-of-book update without depth |
on_trade(ctx, id, m) |
a public trade |
on_option_ticker(ctx, id, m) |
an option's mark, implied volatilities and greeks (Options) |
on_fill(ctx, fill) |
one of our executions, after position and fees are updated, before that execution's on_order_update |
on_order_update(ctx, u) |
an OMS state change of one of our orders, including the end of a reconciliation |
on_timer(ctx, id, tag) |
a timer from ctx.every or ctx.once fired |
on_connection(ctx, m) |
a venue channel changed state; off Live, the venue's quotes are already pulled and, for market data, its books cleared |
on_quoting(ctx, enabled) |
ctx.quoting_enabled() changed (on_quoting) |
on_params(ctx) |
a parameter update was applied and params() holds the new values (Parameter updates) |
on_balance(ctx, m) |
a venue reported one asset of the account; ctx.balance already holds it (Balances) |
on_perp_state(ctx, id, m) |
a derivative's mark, index or funding arrived; ctx.mark and ctx.funding already hold it (Perpetuals) |
on_risk_reject(ctx, r) |
the engine's own risk check refused a new order or replace, and nothing was sent (on_risk_reject) |
on_batch_end(ctx) |
once, after the events waiting when the strategy called ctx.request_batch_end() (on_batch_end) |
on_instrument_rules(ctx, id, m) |
the venue changed the instrument's tick, lot, limits or trading status; ctx.instrument(id) already holds them and m.changed names the fields (Trading rules) |
One input event runs through the hooks in this order:
- The engine updates the book, the position and the OMS.
- The event's hook runs.
on_risk_rejectruns once per refused order.on_quotingruns ifquoting_enabledchanged.- When the input rings are empty and a batch end was requested,
on_batch_endruns.
| Rule | |
|---|---|
| Signature | hooks return void; auto& ctx and template <class Ctx> void on_x(Ctx& ctx, ...) are the same |
noexcept |
hooks run inside noexcept engine code; declare them noexcept |
| Instrument scope | on_book, on_book_ticker, on_trade, on_option_ticker, on_perp_state and on_fill fire only for instruments in the table, so ctx.book(id) and ctx.instrument(id) are valid in them |
| Every fill | on_fill also fires for late fills and for ids the OMS does not know; fills on instruments outside the table are counted in EngineStats::unknown_instrument_fills |
| Determinism | no blocking, no allocation per event, no input the engine does not see: system clock, std::random_device, files (Determinism) |
on_quoting¶
set_quotes is ignored while quoting is disabled. on_quoting(ctx, enabled) reports each change, so a strategy can requote as soon as quoting is back.
Quoting is disabled by an operator pull (PullQuotes), the kill switch, a reconciliation, or parameters older than max_param_age_ms (Parameter updates). A lost connection does not disable it; on_connection reports that. A dry run quotes, and its orders are acknowledged on paper.
- The engine compares
ctx.quoting_enabled()before and after each event, fired timer andon_start, and calls the hook once all flags are final. - A reconciliation holds the resting quotes where they are; at its end the engine re-applies the last quotes to what the snapshot left (an order that still matches stays) and then fires the hook, so a requote from it moves them.
- It never fires from inside a context call: a kill switch tripped by
set_quotesorsendis reported after the calling hook returns. - It does not fire for the initial state;
on_startreadsctx.quoting_enabled().
on_batch_end¶
ctx.request_batch_end() from any hook asks for one on_batch_end(ctx) once the engine has handled the events waiting in its input rings. That is when the rings are empty or the step's max_events_per_step is used up.
Use it for work a burst would repeat per event. A strategy that requotes on each balance report marks the instruments in on_balance and requotes them once here.
- Several requests before the call make one call. A request from inside
on_batch_endmakes one more call, after the step's timers or at the next step. - The engine journals each call, so a replay calls it after the same event.
- Orders sent from it carry the receive time (T0) of the event that asked last.
[engine] feed_budget_per_ringsets how many events the engine takes from one input ring before the next ring's turn; 1 interleaves an account's order events with market data.
on_risk_reject¶
An order the [risk] limits refuse is not sent. send and replace return the RejectReason; set_quotes returns true and the quote manager keeps the level for the next requote. The engine counts and logs the reject, and on_risk_reject(ctx, r) reports it.
RiskReject field |
Meaning |
|---|---|
instrument, side, type, price, qty |
the refused order |
reason |
the RejectReason |
user_tag |
the order's tag; a quote's tag is the QuoteManager's |
replace, order |
whether it was a replace, and the id it would have changed (invalid for a new order) |
time |
engine time of the reject |
- Rejects are delivered after the calling hook returns, in the order they happened, together with
on_quoting. - An order refused again from inside
on_risk_rejectis reported in a further round, a few rounds at most. The queue holds 64 rejects per event; overflow is counted inEngineStats::risk_reject_notices_dropped. - A strategy without the hook pays nothing: the queue is compiled out.
- An order the venue refused is not a risk reject: it arrives in
on_order_updatewithOrderState::Rejected.
Signature checks¶
The engine checks each hook name at compile time. A member with a hook's name whose call does not compile stops the build, one error per hook:
error: static assertion failed: fastmm: on_trade has the wrong signature or is not public; expected void on_trade(auto& ctx, InstrumentId id, const TradeMsg& m)
| Case | Result |
|---|---|
likely misspelling: on_fills, on_trades, on_order_book, on_tick, on_bbo, on_order, on_execution, on_disconnect, onBook, ... |
warning fastmm: on_fills is not a hook; did you mean on_fill?, an error with FASTMM_WERROR; silence it with static constexpr bool fastmm_allow_near_miss_names = true; |
a final class |
only call-checked: a wrong signature is not called and not reported |
| a hook ambiguous across two bases | reported as a wrong signature |
a hook with a deduced (auto) return type |
must not be checked with verify_strategy |
a private hook, also with friend |
rejected |
| implicit argument conversions | accepted |
Engine::start() logs the implemented hooks: strategy first_mm hooks: start book fill timer connection quoting.
Context¶
ctx is a StrategyContext<Engine>, a non-owning view of the engine.
// time and reference data
static_assert(std::same_as<decltype(lvalue<Ctx>().now()), Timestamp>);
static_assert(std::same_as<decltype(lvalue<Ctx>().instrument(InstrumentId{})), const Instrument&>);
static_assert(std::same_as<decltype(lvalue<Ctx>().instruments()), const InstrumentTable&>);
static_assert(std::same_as<decltype(lvalue<Ctx>().venue_name(VenueId{})), std::string_view>);
static_assert(std::same_as<decltype(lvalue<Ctx>().contains(InstrumentId{})), bool>);
// market data
static_assert(std::same_as<decltype(lvalue<Ctx>().book(InstrumentId{})), const Book&>);
static_assert(
std::same_as<decltype(lvalue<Ctx>().own_qty(InstrumentId{}, Side::Buy, Price{})), Qty>);
static_assert(
std::same_as<decltype(lvalue<Ctx>().own_qty(InstrumentId{}, Side::Buy, Price{}, Timestamp{})),
Qty>);
static_assert(std::same_as<decltype(lvalue<Ctx>().best_ex_self(InstrumentId{}, Side::Buy)), Level>);
// portfolio
static_assert(std::same_as<decltype(lvalue<Ctx>().position(InstrumentId{})), const Position&>);
static_assert(std::same_as<decltype(lvalue<Ctx>().portfolio()), Portfolio>);
// quoting
static_assert(
std::same_as<decltype(lvalue<Ctx>().set_quotes(InstrumentId{}, DesiredQuotes{})), bool>);
static_assert(std::same_as<decltype(lvalue<Ctx>().pull_quotes(InstrumentId{})), void>);
static_assert(std::same_as<decltype(lvalue<Ctx>().pull_all_quotes()), void>);
static_assert(std::same_as<decltype(lvalue<Ctx>().working_quote(InstrumentId{}, Side::Buy, 0)),
const Order*>);
// quote context: kept with each order sent on the side and with its fills
static_assert(
std::same_as<decltype(lvalue<Ctx>().quote_context(InstrumentId{}, Side::Buy, {1.0, 2.0})),
void>);
static_assert(std::same_as<decltype(lvalue<Ctx>().quote_context(InstrumentId{}, Side::Buy)),
const QuoteContext&>);
static_assert(std::same_as<decltype(lvalue<Ctx>().quote_context_names({"fair"})), void>);
// direct orders
static_assert(std::same_as<decltype(lvalue<Ctx>().send(NewOrderRequest{})),
Result<ClientOrderId, RejectReason>>);
static_assert(
std::same_as<decltype(lvalue<Ctx>().cancel(ClientOrderId{})), Result<void, RejectReason>>);
static_assert(std::same_as<decltype(lvalue<Ctx>().replace(ClientOrderId{}, Price{}, Qty{})),
Result<void, RejectReason>>);
static_assert(std::same_as<decltype(lvalue<Ctx>().order(ClientOrderId{})), const Order*>);
static_assert(std::same_as<decltype(lvalue<Ctx>().open_qty(InstrumentId{}, Side::Buy)), Qty>);
static_assert(std::same_as<decltype(lvalue<Ctx>().oms()), const Oms&>);
static_assert(
std::same_as<decltype(lvalue<Ctx>().queue_ahead(ClientOrderId{})), std::optional<Qty>>);
static_assert(
std::same_as<decltype(lvalue<Ctx>().order_times(ClientOrderId{})), const OrderTimes*>);
// timers
static_assert(std::same_as<decltype(lvalue<Ctx>().every(Duration{}, std::uint64_t{})), TimerId>);
static_assert(std::same_as<decltype(lvalue<Ctx>().once(Duration{}, std::uint64_t{})), TimerId>);
static_assert(std::same_as<decltype(lvalue<Ctx>().cancel_timer(TimerId{})), bool>);
// control
static_assert(std::same_as<decltype(lvalue<Ctx>().quoting_enabled()), bool>);
static_assert(std::same_as<decltype(lvalue<Ctx>().killed()), bool>);
static_assert(std::same_as<decltype(lvalue<Ctx>().venue_killed(VenueId{})), bool>);
static_assert(std::same_as<decltype(lvalue<Ctx>().reduce_only(InstrumentId{})), bool>);
static_assert(std::same_as<decltype(lvalue<Ctx>().request_stop()), void>);
static_assert(std::same_as<decltype(lvalue<Ctx>().request_checkpoint()), void>);
static_assert(std::same_as<decltype(lvalue<Ctx>().trip_kill(KillReason::StrategyError)), void>);
// randomness (seeded from the configuration)
static_assert(std::same_as<decltype(lvalue<Ctx>().rng()), Xoshiro256ss&>);
| Method | Notes |
|---|---|
now() |
the engine clock of the current event; virtual time in backtests and replay |
instrument(id), instruments(), contains(id) |
the instrument table; iterate instruments() for all of them |
venue_name(venue) |
the venue's [venues.<name>] name, which log lines print as venue:symbol |
book(id) |
the instrument's L2 book (Book) |
position(id) |
signed qty in base units, average price, realised PnL with funding, and fees, in the settlement currency |
portfolio() |
realized, unrealized, fees and net over all instruments, in the [accounting] reporting currency when set; a loop, not for every event |
set_quotes(id, q) |
the desired ladder; the quote manager sends the difference. False when ignored: quoting disabled or instrument not in the table |
pull_quotes(id), pull_all_quotes() |
cancel the quotes of one or every instrument |
working_quote(id, side, level) |
the open order in a quote slot, level 0 nearest the mid, or nullptr |
send(req) |
a direct order, risk-checked and journaled; fails with the RejectReason of risk, the OMS or the kill switch |
cancel(id), replace(id, px, qty) |
direct order management; cancels are always allowed |
order(id) |
an open order, or nullptr once terminal; valid until the next context call |
open_qty(id, side) |
unfilled quantity of our open orders on one side, quotes included |
oms() |
read-only order management state |
every(period, tag), once(delay, tag) |
timers in engine time, journaled; on_timer gets the id and tag. Invalid id once 1016 timers exist: the wheel keeps 8 slots for the engine |
cancel_timer(id) |
false when the timer no longer exists |
watch(id, on) |
watch(id, false) stops on_book, on_book_ticker, on_trade and on_option_ticker for an instrument; its book stays current |
quiet_book(id, lo, hi), quiet_book(id) |
calls on_book only once the mid reaches lo or hi or the book turns invalid; the one-argument form ends it |
quoting_enabled(), killed(), venue_killed(venue) |
quoting state, the global kill switch, one venue's kill switch |
reduce_only(id) |
reduce-only mode holds the instrument: an order or replace that would leave its position no nearer zero is refused with ReduceOnly, so quote only the side that reduces |
note_quote(id, side, reason, budget = {}) |
why the strategy quotes a side as it does, up to 23 bytes, and its budget, for fastmm-top's quote table (Monitoring) |
metric(name, value) |
publishes a named number in the status file, fastmm-top and fastmm_strategy_metric; first 32 names, 23 bytes each |
request_stop() |
sets the stop flag: a backtest ends after the current step; replay always drains the journal |
request_checkpoint() |
writes a checkpoint to the journal once the events waiting are handled (State across sessions); nothing without a journal |
trip_kill(reason) |
trips the global kill switch: quoting stops and every working order is cancelled |
rng() |
a Xoshiro256ss seeded from [engine] rng_seed, identical in replay |
own_qty, best_ex_self, queue_ahead, order_times |
our own orders as the market sees them (Execution view) |
sendreturnsInvalidTagwhenuser_taglies in the quote manager's range.- A venue whose kill switch is engaged refuses new orders, and
set_quotesignores its instruments. watchandquiet_bookgive the same result and run faster in a sweep (Many runs in one pass).set_quotesapplies hysteresis ([engine] min_requote_ticks,min_requote_interval_ms), skips orders awaiting a venue response, and replaces where the venue supports it.
A direct order returns a Result:
auto id = ctx.send(NewOrderRequest::limit(inst.id, Side::Buy, px, qty).post_only());
if (!id) return FASTMM_LOG_WARN("order refused: {}", id.error()); // e.g. RejectReason::MaxPosition
Quote context¶
The store keeps with every fill what the strategy knew about the quote that filled: up to four numbers per instrument and side, such as the fair value the quote was priced from and its distance from it. The engine copies a side's numbers into each order it sends or moves on that side, and the fill's row holds the copy its order took when it last went out and the side's numbers when the fill arrived, next to when the order was placed and moved and what was in flight (Storage). fastmm-pnl fill-context prints them.
| Method | Notes |
|---|---|
quote_context(id, side, {a, b, ...}) |
the side's numbers from now on, up to four; a slot not given is NaN. Call it before set_quotes or send |
quote_context(id, {a, b, ...}) |
both sides |
quote_context(id, side) |
the side's numbers as they stand (QuoteContext::v) |
quote_context_names({"fair", ...}) |
names for the slots, up to 23 bytes each, recorded once for the session; call it from on_start |
void on_start(auto& ctx) { ctx.quote_context_names({"fair", "bid_bps", "ask_bps"}); }
void requote(auto& ctx, InstrumentId id, Price fair, Price bid, Price ask) {
const double f = fair.to_double();
ctx.quote_context(id, {f, (f - bid.to_double()) / f * 1e4, (ask.to_double() - f) / f * 1e4});
DesiredQuotes q;
q.bid(bid, qty_);
q.ask(ask, qty_);
ctx.set_quotes(id, q);
}
Neither call allocates or leaves the engine thread. The numbers are part of the engine's state, so a replay of the journal records the same ones.
Fees, risk headroom and venue health¶
static_assert(std::same_as<decltype(lvalue<Ctx>().fees(InstrumentId{})), const FeeRates&>);
static_assert(std::same_as<decltype(lvalue<Ctx>().risk_headroom(InstrumentId{})), RiskHeadroom>);
static_assert(std::same_as<decltype(lvalue<Ctx>().venue_health(VenueId{})), VenueHealthView>);
static_assert(std::same_as<decltype(lvalue<Ctx>().order_budget(VenueId{})), OrderBudget>);
static_assert(std::same_as<decltype(lvalue<Ctx>().fill_cluster()), FillClusterView>);
static_assert(std::same_as<decltype(lvalue<Ctx>().hazard_groups()), std::uint32_t>);
static_assert(std::same_as<decltype(lvalue<Ctx>().hazard_group(InstrumentId{})), std::uint32_t>);
static_assert(std::same_as<decltype(lvalue<Ctx>().hazard(0U)), HazardView>);
static_assert(std::same_as<decltype(lvalue<Ctx>().hazard_add(0U, Side::Buy, 1.0)), void>);
| Method | Returns | Journaled |
|---|---|---|
fees(id) |
FeeRates |
yes |
risk_headroom(id) |
RiskHeadroom: what each [risk] limit still admits on the instrument |
yes |
venue_health(venue) |
VenueHealthView: feed lag and order round trip |
yes |
order_budget(venue) |
OrderBudget: the local rate limiter and the venue's own order counts |
only local_tokens |
fill_cluster() |
FillClusterView: the sides the fill-cluster guard holds |
yes |
hazard_groups() |
the number of hazard groups, numbered in the order of [[risk.hazard]] |
yes |
hazard_group(id) |
the group the instrument is a member of, Hazard::kNoGroup for none |
yes |
hazard(group) |
HazardView: the group's score and the sides it holds |
yes |
hazard_add(group, side, amount) |
nothing: adds evidence to one side's score | yes |
fees(id) is the instrument's maker_bps / taker_bps, else its venue's [venues.<x>.fees]. With fetch_fees it is the account's own rates (Binance Spot). A backtest charges these; a live venue reports each fill's commission itself.
FeeRates |
Meaning |
|---|---|
maker_cbps, taker_cbps |
rates in centi-bps (1 cbps = 0.01 bps); positive is a fee, negative a rebate |
maker_bps(), taker_bps() |
the same in bps |
fee(notional, liquidity) |
the fee on a notional |
risk_headroom(id) uses the inputs the next risk check uses: an order of exactly a room passes, one lot more is refused. A limit that is off reads RiskHeadroom::kUnlimited or the type's max(). Notionals are in the reporting currency with [accounting]. fastmm-gateway's account guards are not included.
RiskHeadroom field |
Room left under |
|---|---|
order_tokens |
the rate limiter, orders_per_sec |
open_orders |
max_open_orders on the instrument |
max_order_qty, max_order_notional |
the per-order limits |
buy_qty, sell_qty |
max_position, with that side's open orders counted, rounded down to the lot |
underlying_buy_qty, underlying_sell_qty |
[risk.underlying] max_net over the underlying's instruments, in this instrument's contracts; 0 while an inverse contract of it has no mark |
gross_notional |
the gross notional cap |
net_buy_notional, net_sell_notional |
the net exposure an order may open |
exposure_buy_notional, exposure_sell_notional |
those caps as one order's notional here, in the settlement currency; see below |
loss_budget |
max_loss plus net PnL; the kill switch trips at 0 |
balance_buy_qty, balance_sell_qty |
balance_room at the book's mid (Balances) |
On the side that reduces the position, exposure_*_notional adds the close at the mid. While the currency's rate is unknown that close is all that is left, and the other side reads 0.
VenueHealthView field |
Meaning |
|---|---|
feed_lag |
recv_ts - exch_ts of the venue's latest market-data message with a venue time |
feed_lag_base, feed_lag_excess |
the minimum lag over the last 8 s, and the lag above it |
ack_rtt, ack_rtt_smoothed |
engine time from sending a new order to its first ack; smoothed as srtt += (rtt - srtt) / 8 |
md_updated, ack_updated, sample counts |
when each was last sampled |
gate_engagements, gated |
the feed-lag gate; while it holds a venue, set_quotes on its instruments returns false |
All fields are zero before the first sample. venue_health costs one lookup; risk_headroom costs a risk check.
FillClusterView field |
Meaning |
|---|---|
enabled |
[risk] fill_cluster_names is set |
held[0], held[1] |
the guard holds the bids, the asks: set_quotes places nothing on that side until the hold ends, then the engine puts the last quotes back |
held_until[0], held_until[1] |
engine time the hold ends, unless a fill extends it |
engagements[0], engagements[1] |
times the guard engaged on each side |
hazard_add takes an amount in the units of the group's threshold. Evidence that takes the score to the threshold pulls the side before the call returns, so a set_quotes after it already places nothing there. A negative amount lowers the score, not below zero. A group that does not exist takes nothing.
HazardView field |
Meaning |
|---|---|
enabled |
the group exists |
threshold, log_only |
its threshold, and whether its mode is "log" |
score[0], score[1] |
the score of the bids, the asks, decayed to now |
held[0], held[1] |
the group holds the side: set_quotes places nothing on it on the members until the hold ends. In log mode: it would |
held_since[0], held_since[1] |
engine time the side was pulled |
held_until[0], held_until[1] |
engine time the hold ends, unless evidence restarts it |
engagements[0], engagements[1] |
times the score reached the threshold on a side that was not held |
held_fills[0], held_fills[1] |
quote fills that arrived on a held side |
held_notional[0], held_notional[1] |
their notional |
OrderBudget field |
Meaning |
|---|---|
local_tokens |
orders the [risk] orders_per_sec bucket admits now; kUnlimited with the limit off |
local_wait_ns |
time until the bucket admits the next order, 0 if it admits one now |
wait_ns |
time until every limit admits an order again: 0 while orders_remaining() > 0, else the latest of local_wait_ns, the end of each full window and the end of a pause; kUnlimited where the end is unknown (a 418). A strategy refused RateLimit waits this long |
orders_10s, orders_1m, orders_1d |
the venue's order counts per 10 s, minute and day: its last report plus the orders sent since |
weight |
request weight over the venue's shortest window: 6000 a minute on Binance Spot, 2400 on USDⓈ-M |
venue_paused |
the connector sends nothing: a 429 Retry-After or a 418 |
paused_until_ns |
when a 429's pause ends, on the engine's clock; 0 for a 418 |
venue_known |
the connector has published counts |
orders_remaining() |
the least of the windows |
Each window is a RateWindow: window_ms, used, limit, cap, ends_ns, known(), remaining(). cap is where the connector stops sending, 90 % of limit, and remaining() counts up to it. ends_ns is when the window rolls, on the engine's clock (0 unknown); once it has passed, order_budget reads the window as empty without waiting for the connector's next publication.
The venue fields come from the connector's thread and are not journaled. A backtest, a replay and a strategy attached to fastmm-gateway see venue_known == false and every window unlimited.
Order capacity¶
With [capacity] a new order or a move is sent only when its value covers the price of its order slot (Price order capacity).
static_assert(std::same_as<decltype(lvalue<Ctx>().capacity(VenueId{})), CapacityQuote>);
static_assert(std::same_as<decltype(CapacityQuote::price), double>);
// A level worth `value` placed, and its resting order worth `keep` where it rests:
static_assert(
std::same_as<decltype(lvalue<DesiredQuotes>().bid(Price{}, Qty{}, 0.0F, 0.0F)), bool>);
static_assert(
std::same_as<decltype(lvalue<DesiredQuotes>().set_value(Side::Buy, 0, 0.0F, 0.0F)), void>);
// A direct order worth `value`:
static_assert(
std::same_as<
decltype(NewOrderRequest::limit(InstrumentId{}, Side::Buy, Price{}, Qty{}).value(0.0F)),
LimitOrder>);
| Call | Meaning |
|---|---|
DesiredQuotes::bid(px, qty, value, keep), ask(...), set_value(side, level, value, keep) |
what the level is worth placed, and what its resting order is worth where it rests, in the quote currency; kNoValue (the default): not stated |
NewOrderRequest::limit(...).value(v) |
a direct order worth v; one with no value is not priced |
capacity(account) |
CapacityQuote: what a slot on that account costs now |
CapacityQuote field |
Meaning |
|---|---|
price |
what an action's value must reach: window + weight + day |
floor |
L, the value of an order of the smallest minimum notional |
window, window_share, window_used, window_orders |
the account window's part of the price, its share used, its orders and orders_10s |
weight, day |
the pool's request-weight part; the account's and the strategy's day prices |
on |
[capacity] is on |
A level priced out returns no error: set_quotes keeps its target and the engine applies it again when the window rolls over or a fill gives a slot back. send returns PricedOut. A value of 0 or below is never sent; kNoValue takes [capacity] value, and a direct order with none is not priced. An account outside [capacity] venues reads on == false.
Balances¶
static_assert(std::same_as<decltype(lvalue<Ctx>().balance(VenueId{}, "USDT")), Balance>);
static_assert(std::same_as<decltype(lvalue<Ctx>().margin(VenueId{})), Margin>);
static_assert(
std::same_as<decltype(lvalue<Ctx>().balance_room(InstrumentId{}, Side::Buy, Price{})), Qty>);
static_assert(std::same_as<decltype(Balance::free), Notional>);
static_assert(std::same_as<decltype(Balance::known), bool>);
static_assert(std::same_as<decltype(Margin::available), Notional>);
static_assert(std::same_as<decltype(lvalue<Ctx>().balances_live()), bool>);
| Method | Returns |
|---|---|
balance(venue, asset) |
Balance of one asset, amounts in the asset |
margin(venue) |
Margin: available, initial, maintenance, equity, wallet, asset |
balance_room(id, side, px) |
the largest quantity of id the balance covers at px, rounded down to the lot; Qty::max() until the venue reports that balance |
balances_live() |
a venue has reported balances; before that nothing is estimated or checked |
Balance field |
Meaning |
|---|---|
free, locked, total |
the venue's last report, moved by this engine's orders and fills since (Balance check) |
equity, maintenance |
as reported |
as_of |
the report's venue time |
known |
false until the venue reports the asset, and for an asset no instrument of the venue uses |
marginis the venue's account-wide margin where it reports one:accountis true, usually valued in USD. Otherwise it is the settlement asset of the venue's first derivative.balance_roomcounts a spot buy's quote with the taker fee, a spot sell's base and a derivative's initial margin. This instrument's open orders on that side do not count as room.
Account pools¶
Several accounts of one exchange behind one venue: [venues.<x>] pool_of (Account pools). Instruments, books, tickers and positions are the primary's. Each member has its own balance(), margin() and order_budget() under its own venue id.
static_assert(std::same_as<decltype(lvalue<Ctx>().pool(VenueId{})), PoolMembers>);
static_assert(std::same_as<decltype(lvalue<Ctx>().pool_primary(VenueId{})), VenueId>);
static_assert(std::same_as<decltype(lvalue<Ctx>().account_usable(VenueId{})), bool>);
static_assert(std::same_as<
decltype(lvalue<Ctx>().balance_room(InstrumentId{}, Side::Buy, Price{}, VenueId{})),
Qty>);
static_assert(std::same_as<decltype(lvalue<const PoolMembers>().size()), std::size_t>);
static_assert(std::same_as<decltype(lvalue<const PoolMembers>()[0]), VenueId>);
static_assert(std::same_as<decltype(NewOrderRequest::account), VenueId>);
| Method | Returns |
|---|---|
pool(primary) |
PoolMembers, primary first: size(), operator[], contains(venue), iterable; the venue alone without a pool |
pool_primary(venue) |
the venue whose instruments venue takes orders for: its primary, or itself |
account_usable(venue) |
automatic routing would send to it now: order link live, kill switch not engaged |
balance_room(id, side, px, account) |
balance_room on one account of the pool |
The three-argument balance_room on a pool is what one usable account can hold:
- the largest room over the members, a sell's free base or a buy's free quote at the price,
- plus that member's own open orders on the side,
- less
open_qty(id, side)over every account, never below zero.
fit_to_balance therefore cuts a ladder to what one account covers. A quote resting on one account is not resized by another's balance.
An order goes to one account and stays there. Order::venue, OmsUpdate and Fill (msg->hdr.venue) and the store's rows carry the member's id; cancel and replace follow it.
NewOrderRequest::account, or .account(venue) on the builder, names the account. It must be the primary or a member, else send fails with InvalidAccount. Without one the engine picks:
- Candidates: usable members whose free quote covers a spot buy's notional, or whose free base covers a spot sell. Every usable member for a derivative.
- Skip members whose connector is paused or has an order window at its
cap. - Take the one with most of the 10 s window left (
order_budget(member).orders_10s.remaining()), then of the daily window. - Ties take turns, starting after the account the last automatic choice went to.
-
No member covers the order: it goes to the primary, which refuses it as the venue does.
-
Orders sent to an account since its connector last published count against it, so one burst spreads over the pool.
sendrefuses withRateLimit, without reaching the connector, an order whose account has a window at its cap or a paused connector.order_budget(account).wait_nssays how long until that account admits one again: a strategy that resends a pinned order waits that long rather than retrying on every event.- Cancels count in no order window.
- A member with its order link down or kill switch engaged gets no new orders; a kill of the primary stops the pool.
set_quotesroutes the same way, one order at a time.- A backtest without window limits knows no budget and sends what the balances allow to the primary.
Homes¶
With [venues.<primary>.pool] each instrument of the pool has a home account (Keep each position on one account).
static_assert(std::same_as<decltype(lvalue<Ctx>().home(InstrumentId{})), VenueId>);
static_assert(std::same_as<decltype(lvalue<Ctx>().placement(VenueId{})), Placement>);
static_assert(std::same_as<decltype(lvalue<Ctx>().holding(InstrumentId{}, VenueId{})), Qty>);
static_assert(std::same_as<decltype(lvalue<Ctx>().pieces(InstrumentId{})), std::uint32_t>);
static_assert(std::same_as<decltype(lvalue<Ctx>().set_home(InstrumentId{}, VenueId{})), bool>);
| Method | Returns |
|---|---|
home(id) |
the home's venue id; invalid without the table, or before the instrument has one |
placement(primary) |
Placement::Home or Placement::Balance; a member reads its pool's |
holding(id, account) |
what that account holds: a spot instrument's base balance there, a derivative's position on it; without a pool, the position |
pieces(id) |
the number of accounts other than the home holding at least the instrument's minimum order |
set_home(id, account) |
moves the home (journaled) and cancels the adding orders on other accounts; false when the pool keeps no homes or account is not in it |
With placement = "home" an order without NewOrderRequest::account goes to an account whose own part it takes towards zero, pieces first, and otherwise to the home (Where orders go). balance_room(id, side, px) is what the account the next order on that side goes to holds, a piece's part at most. An order for an unusable home is refused with HomeUnavailable.
A spot instrument's holding is the account's whole balance of the base asset: two instruments on one base asset in a pool (BTCUSDT and BTCFDUSD) share it.
Funding needs¶
A strategy tells a pool treasury with targets = "demand" what quote each instrument's bids want resting; the treasury funds each account for the instruments whose home it is (Fund each account of a pool).
static_assert(
std::same_as<decltype(lvalue<Ctx>().set_funding_need(InstrumentId{}, Notional{})), void>);
static_assert(std::same_as<decltype(lvalue<Ctx>().funding_need(InstrumentId{})), Notional>);
| Method | Does |
|---|---|
set_funding_need(id, quote) |
declares the need in the instrument's quote asset (a derivative's settlement asset); zero or less clears it |
funding_need(id) |
the need last declared |
Declare it when the sizes change, for example at each re-allocation. A need follows the instrument's home when the home moves.
Perpetuals¶
static_assert(std::same_as<decltype(lvalue<Ctx>().mark(InstrumentId{})), RefPrice>);
static_assert(std::same_as<decltype(lvalue<Ctx>().index(InstrumentId{})), RefPrice>);
static_assert(std::same_as<decltype(lvalue<Ctx>().funding(InstrumentId{})), FundingView>);
static_assert(std::same_as<decltype(lvalue<Ctx>().perp_state(InstrumentId{})), const PerpRow&>);
static_assert(std::same_as<decltype(RefPrice::price), Price>);
static_assert(std::same_as<decltype(RefPrice::stale), bool>);
static_assert(std::same_as<decltype(FundingView::rate), double>);
static_assert(std::same_as<decltype(FundingView::interval), Duration>);
static_assert(std::same_as<decltype(lvalue<const FundingView>().over(Duration{})), double>);
| Method | Returns |
|---|---|
mark(id), index(id) |
RefPrice of a derivative (venues) |
funding(id) |
FundingView |
perp_state(id) |
const PerpRow&: every field as last reported, open interest included, with arrival times and no staleness applied |
RefPrice field |
Meaning |
|---|---|
price |
the venue's mark or index price |
at |
engine time it arrived |
stale |
older than [accounting] stale_mark_ms, or never reported |
usable() |
reported and not stale |
FundingView field |
Meaning |
|---|---|
rate |
per interval, a decimal; positive means longs pay shorts |
interval |
the funding period |
next |
venue time of the next payment; zero when funding is continuous |
at, stale, usable() |
as for RefPrice, against stale_funding_ms |
over(d) |
the rate over a holding time d: rate * d / interval |
PerpStateMsg::fieldsnames what a message carries; a venue with separate channels (OKX) sends one message per channel and the table keeps the rest.- With
[accounting] mark = "venue", the default, a position with a fresh mark is valued at it (Valuation);on_bookthen no longer moves its unrealised PnL. fit_to_balance(ctx, id, inst, q)instrategies/quoting.hppcuts aDesiredQuotesladder to what the balance covers. The side's resting orders count as room, and a level under the lot,min_qtyormin_notionalis dropped.
Execution view¶
Our own orders as the venue's feed shows them, derived from journaled order events and market data, so a replay reproduces them.
| Method | Returns |
|---|---|
own_qty(id, side, px), own_qty(id, side, px, at) |
our resting quantity the feed shows at px, as of the book's last update or venue time at; 0 in the simulator |
best_ex_self(id, side) |
the best Level on one side with own_qty taken out; Level{} when none is left |
queue_ahead(order_id) |
std::optional<Qty>: the estimated quantity ahead of an open order at its price; nullopt before its ack and once terminal |
order_times(order_id) |
const OrderTimes* of an open order, nullptr once terminal |
own_qtycounts an order from its ack's venue time to its end's, less its fills. The end is a cancel ack, last fill, expiry or a reconciliation that drops it.- With whole-millisecond venue times, an order counts to the end of that millisecond. A depth update stamped before our cancel still includes it.
atis typically aBookTicker'shdr.exch_ts.best_ex_selfskips a level that was only ours.
OrderTimes field |
Meaning |
|---|---|
sent |
engine clock at send |
venue_ack |
the ack's exch_ts, whole ms on Binance; 0 when the venue gives none |
local_ack |
the ack's recv_ts |
After a replace they describe the replacement from its ack on. OmsUpdate::times carries them in on_order_update, the terminal update included, and Fill::update in on_fill.
Whether a venue's feed shows our orders is own_in_feed(venue):
| Transport | Feed shows our orders |
|---|---|
LiveTransport |
yes |
| replay of a live journal, one with a TSC calibration | yes |
backtest with [backtest] own_orders_in_feed |
yes: the simulated venue sends our quantity as OwnLevels |
| other backtests | no |
Queue position¶
queue_ahead runs the l2_queue fill model's queue model on the market data the strategy sees. Its conservatism is [engine] queue_conservatism, or [backtest] queue_conservatism in a backtest.
| Event | Quantity ahead |
|---|---|
| ack | the displayed quantity at the price less own_qty, less what trades since took from the level |
| ack at the touch of a newer book ticker | the touch's quantity less ours; 0 for a price better than that touch |
| depth delta at the price | a shrink from old to new takes (old - new) * ahead / old * (1 - conservatism), old net of trades since the last update |
| level removed | 0 |
| snapshot | capped at the level |
| trade at the price | consumed first |
| trade through the price | 0, and the order fills the print's quantity |
| book ticker newer than the depth | 0 for an order better than its touch; at most the touch's quantity less ours at the touch |
| replace ack | kept at the same price and no more than the leaves; otherwise as for an ack |
- One print is shared by our orders on its side in price priority, best for the aggressor first. Each gets what the better ones left, after our maker fills at the print's venue time.
- "Newer" compares venue update ids when both messages carry one, else venue times.
- A level showing less than we rest at its price was published before our order arrived; all of it counts as others' quantity.
- Tracking starts at the first
queue_aheadcall. An order already resting then starts at the back of its level. Call it inon_startto cover every order from its ack. - With
fill_model = "l2_queue"and no latency the value equals the fill model's (tests/sim/exec_view_test.cpp). With latency the simulated venue sees the market before the strategy does. - A replay handles events in arrival order,
fastmm-data fill-checkin venue-time order, so the two can differ.
Hedge executor¶
HedgeExecutor (strategies/hedge_executor.hpp) is a member a strategy owns. It hedges the positions of source instruments with IOC orders on hedge instruments, fails over between them and de-risks (Hedge with HedgeExecutor).
static_assert(std::same_as<decltype(lvalue<HedgeExecutor>().add_source(InstrumentId{})), bool>);
static_assert(
std::same_as<decltype(lvalue<HedgeExecutor>().add_hedge(InstrumentId{}, Ratio{})), bool>);
static_assert(
std::same_as<decltype(lvalue<HedgeExecutor>().start(lvalue<Ctx>(), HedgeExecutor::Config{})),
bool>);
static_assert(std::same_as<decltype(lvalue<HedgeExecutor>().on_fill(lvalue<Ctx>(), Fill{})), bool>);
static_assert(
std::same_as<decltype(lvalue<HedgeExecutor>().on_order_update(lvalue<Ctx>(), OmsUpdate{})),
bool>);
static_assert(
std::same_as<decltype(lvalue<const HedgeExecutor>().residual(lvalue<const Ctx>())), Qty>);
static_assert(
std::same_as<decltype(lvalue<const HedgeExecutor>().can_hedge(lvalue<const Ctx>())), bool>);
static_assert(std::same_as<decltype(lvalue<const HedgeExecutor>().status(lvalue<const Ctx>())),
HedgeExecutor::Status>);
| Method | Does |
|---|---|
reset(), add_source(id), add_hedge(id, tolerance) |
name the legs, at most 4 of each, hedges in order of preference; false when full |
start(ctx, config) |
checks the legs (configured, linear, each once) and starts; false and logged otherwise |
set_config(config), set_tolerance(i, tolerance) |
new parameters |
set_target(qty) |
a new net target in base units |
restart() |
clears a halt |
on_fill, on_order_update, on_book, on_timer, on_connection, on_balance, update |
the strategy's hooks forwarded; the bool ones return whether the event concerned a leg or its orders |
residual(ctx) |
sources plus hedges minus the target, base units |
can_hedge(ctx) |
some hedge instrument can take a hedge now and hedging is not halted |
why(ctx, i, now) |
the Reason hedge i is not usable: Down, Killed, NoBook, Stale, Gated, Benched, or Usable |
held(), halted(), derisking(), state(), status(ctx), stats() |
monitoring (Monitor it) |
hedge_order(inst, residual, bid, ask, tolerance, tag) |
static: the IOC that brings residual towards zero on inst, or none under its lot, min_qty or min_notional |
Book¶
on_book receives the engine's L2Book. Any type modelling BookView has the same read API.
static_assert(BookView<Book>); // best_bid(), best_ask(), mid(), spread(), level(side, i), ...
static_assert(std::same_as<decltype(lvalue<const Book>().best_bid()), Level>);
static_assert(std::same_as<decltype(lvalue<const Book>().mid()), Price>);
static_assert(std::same_as<decltype(lvalue<const Book>().is_valid()), bool>);
| Method | Meaning |
|---|---|
best_bid(), best_ask() |
the touch as Level{price, qty}; zero when the side is empty |
mid(), spread() |
(bid + ask) / 2 truncated, ask - bid |
level(side, i), depth(side), top<N>(side) |
levels from the touch outwards |
qty_at_or_better(side, px), price_for_qty(side, qty) |
depth queries |
is_valid() |
both sides present and not crossed |
seq(), last_update() |
venue sequence number and venue time of the last update; receive time without a venue time |
Fill¶
Fill is built on the engine's stack; update and msg are valid only during the call.
static_assert(std::same_as<decltype(Fill::instrument), InstrumentId>);
static_assert(std::same_as<decltype(Fill::side), Side>);
static_assert(std::same_as<decltype(Fill::price), Price>);
static_assert(std::same_as<decltype(Fill::qty), Qty>);
static_assert(std::same_as<decltype(Fill::position_delta), Qty>);
static_assert(std::same_as<decltype(Fill::fee), Notional>);
static_assert(std::same_as<decltype(Fill::fee_converted), bool>);
static_assert(std::same_as<decltype(Fill::liquidity), Liquidity>);
static_assert(std::same_as<decltype(Fill::known), bool>);
static_assert(std::same_as<decltype(Fill::late), bool>);
static_assert(std::same_as<decltype(Fill::order_done), bool>);
static_assert(std::same_as<decltype(Fill::update), const OmsUpdate*>);
static_assert(std::same_as<decltype(Fill::msg), const OrderFillMsg*>);
| Field | Meaning |
|---|---|
instrument, side |
the order's, else the fill message's |
price, qty |
this execution |
position_delta |
signed change of the position; differs from qty when the commission is charged in the base asset |
fee |
fee in the settlement currency as booked |
fee_converted |
false when the commission was in a third asset, such as BNB, and not booked |
liquidity |
Maker, Taker or Unknown |
known |
the id matched an order; update->order holds its snapshot |
late |
the order was already terminal when the fill arrived; its snapshot has id, instrument, side, state and filled quantity |
order_done |
the order reached a terminal state with this fill |
update, msg |
the OMS update, non-null when known, and the raw OrderFillMsg |
Messages¶
Hooks receive fixed-size, trivially copyable structs from core/messages.hpp.
static_assert(std::same_as<decltype(TradeMsg::price), Price>);
static_assert(std::same_as<decltype(TradeMsg::qty), Qty>);
static_assert(std::same_as<decltype(TradeMsg::aggressor), Side>);
static_assert(std::same_as<decltype(TradeMsg::trade_id), std::uint64_t>);
static_assert(
std::same_as<decltype(std::declval<const TradeMsg&>().parties()), const TradeParties*>);
static_assert(std::same_as<decltype(TradeParties::buyer), PartyId>);
static_assert(std::same_as<decltype(TradeParties::seller), PartyId>);
static_assert(std::same_as<decltype(PartyId::kind), PartyIdKind>);
static_assert(std::same_as<decltype(BookTickerMsg::bid_px), Price>);
static_assert(std::same_as<decltype(BookTickerMsg::bid_qty), Qty>);
static_assert(std::same_as<decltype(BookTickerMsg::ask_px), Price>);
static_assert(std::same_as<decltype(BookTickerMsg::ask_qty), Qty>);
static_assert(std::same_as<decltype(OptionTickerMsg::mark_price), Price>);
static_assert(std::same_as<decltype(OptionTickerMsg::underlying_price), Price>);
static_assert(std::same_as<decltype(OptionTickerMsg::mark_iv), double>);
static_assert(std::same_as<decltype(OptionTickerMsg::delta), double>);
static_assert(std::same_as<decltype(ConnectionStateMsg::state), ConnState>);
static_assert(std::same_as<decltype(ConnectionStateMsg::channel), std::uint8_t>);
static_assert(std::same_as<decltype(EventHeader::instrument), InstrumentId>);
static_assert(std::same_as<decltype(EventHeader::venue), VenueId>);
static_assert(std::same_as<decltype(EventHeader::recv_ts), Timestamp>);
static_assert(std::same_as<decltype(EventHeader::exch_ts), Timestamp>);
static_assert(std::same_as<decltype(OmsUpdate::order), Order>);
static_assert(std::same_as<decltype(OmsUpdate::prev), OrderState>);
static_assert(std::same_as<decltype(OmsUpdate::terminal), bool>);
static_assert(std::same_as<decltype(BalanceMsg::free), Notional>);
static_assert(std::same_as<decltype(BalanceMsg::asset), FixedString<8>>);
static_assert(std::same_as<decltype(PerpStateMsg::mark_price), Price>);
static_assert(std::same_as<decltype(PerpStateMsg::funding_rate), double>);
static_assert(std::same_as<decltype(PerpStateMsg::next_funding), Timestamp>);
static_assert(std::same_as<decltype(PerpStateMsg::fields), std::uint8_t>);
| Message | Notes |
|---|---|
EventHeader |
starts every message: type, venue, instrument, exch_ts (venue event time), recv_ts (receive time), seq and flags |
TradeMsg |
aggressor is the taker's side |
TradeMsg::parties() |
who bought and who sold where the venue publishes it, else null; see below |
OptionTickerMsg |
mark_price in the instrument's price unit; underlying_price and index_price in the underlying's quote currency; IVs annualised decimals (0.312 is 31.2 %); the venue's greeks; NaN for a field not sent |
ConnectionStateMsg |
state: Disconnected, Connecting, Live, Stale, Resyncing or Dead; channel 0 market data, 1 order and user stream; reason_code venue-specific |
OmsUpdate |
order (snapshot after the transition), prev (the previous state), and the flags known, changed, terminal |
BalanceMsg |
one asset of a venue account as reported, absolute amounts in asset at exch_ts; see below |
Trade parties:
| Venue | PartyIdKind |
Read with |
|---|---|---|
| Hyperliquid | Address, both wallet addresses |
raw(), 20 bytes |
| Lighter | Account, both account indexes |
account() |
A trade with parties is 192 bytes (hdr.len): the TradeParties follow the 128-byte message. A copied TradeMsg does not include them, so copy *m.parties().
BalanceMsg field |
Spot | Derivative |
|---|---|---|
free |
free | available margin |
locked |
locked | initial margin in use |
total |
total | wallet balance |
equity |
total | wallet plus unrealised PnL |
maintenance |
0 | maintenance margin |
BalanceMsg flag |
Meaning |
|---|---|
kSnapshot |
part of a full snapshot |
kSnapshotEnd |
the snapshot's last message; an asset it did not name holds nothing |
kAccount |
the account-wide margin |
Parameters¶
struct AllHooksParams {
FASTMM_PARAMS(AllHooksParams)
FASTMM_PARAM(int, levels, 1, 1, 8, "quote levels per side")
FASTMM_PARAM(double, gamma, 0.1, 0.0, 10.0, "risk aversion")
FASTMM_PARAM(bool, hedge, false, false, true, "hedge fills")
FASTMM_PARAM(Qty, quote_qty, 0.001_qty, 0_qty, 1000_qty, "quantity per side")
FASTMM_PARAM_BPS(half_spread_bps, 5_bps, 0_bps, 1000_bps, "half spread, bps")
FASTMM_PARAM_MS(timer_ms, milliseconds(100), milliseconds(1), milliseconds(60000), "timer, ms")
// Optional: runs after all keys of a configure() call are applied.
[[nodiscard]] std::optional<std::string> validate() const {
if (hedge && levels > 4) return "hedge supports at most 4 levels";
return std::nullopt;
}
};
| Declaration | Field type | Schema type | Configuration value |
|---|---|---|---|
FASTMM_PARAM(int, ...) |
any integer type | int |
whole number: 3, 3.0, 3e0 |
FASTMM_PARAM(double, ...) |
floating point | double |
any finite number |
FASTMM_PARAM(bool, ...) |
bool |
bool |
true/false, 1/0, yes/no, on/off |
FASTMM_PARAM(Qty, ...) |
Price, Qty, Notional |
decimal |
exact, up to 8 decimals |
FASTMM_PARAM_BPS(name, ...) |
Ratio |
bps |
basis points, exact, up to 4 decimals |
FASTMM_PARAM_MS(name, ...) |
Duration |
ms |
whole milliseconds; bounds are Durations |
- Arguments: field name, default, minimum, maximum, description.
FASTMM_PARAMS(Self)comes first; at most 128 parameters. decimal,bpsandmsvalues are parsed as decimal text, never through a double; exponents are accepted (Fixed point).- Ranges are checked on the typed value:
parameter 'quote_qty': value 1000.5 outside [0, 1000]. std::optional<std::string> check_instruments(const InstrumentTable&) constchecks parameters that name instruments. It runs afterconfigure(), before the engine is built; an error stops the session.lead_mmuses it.- The schema drives configuration checks,
--list-strategies,--param key=valueandfastmm.strategies()in Python.
Parameters per instrument¶
StrategyBase<Params, ParamScope::PerInstrument> keeps a parameter set per instrument besides the shared one, and params(id) reads the instrument's.
class MyMM : public StrategyBase<MyParams, ParamScope::PerInstrument> {
// ... params(id).half_spread_bps where the instrument is known, params() where it is not
};
Shared (default) |
PerInstrument |
|
|---|---|---|
params() |
the configuration plus every update | the configuration plus updates that named no instrument |
params(id) |
same as params() |
params() with the instrument's own values on top, an array index |
updates for one instrument ([strategy.overrides], fastmm-ctl param --instrument, an [ab] arm) |
refused | taken, as the instrument's own values |
An instrument's own value stays when an update for every instrument changes that parameter. A reset (fastmm-ctl param --reset, an instrument leaving an [ab] test) gives it the shared value again. own_params(id) is the mask of the parameters it holds as its own.
- A parameter read only through
params(), or only inon_start, does not vary per instrument. basic_mm,avellaneda_stoikovand every Python strategy arePerInstrument. Per-instrument tests: Run a live A/B by instrument.
Parameter updates¶
A running engine takes new parameter values as an input event. ParamPublisher (fastmm/strategies/param_publisher.hpp) builds the event on another thread and pushes it into a ring of the engine's feed.
ParamPublisher pub(ParamSink::to_ring(ring), strategy.params());
bool sent = pub.publish({{"half_spread_bps", "7.5"}, {"quote_qty", "0.02"}});
In a backtest, sim::ParamSchedule (fastmm/sim/param_schedule.hpp) delivers updates at simulated times through bt::BacktestSession::set_param_schedule.
publish(values, inst)parses each value into a copy of the parameters and runsvalidate(). Nothing is sent if it throwsstd::invalid_argument.- It throws for an unknown name, a name given twice, a value that does not parse or is out of range, a failed
validate()and more than 32 values. - It returns false when the ring is full or after
close(); the engine never waits for the publisher. - The copy starts from the constructor's parameters and follows every update sent. An update applies to all instruments, or with
PerInstrumentmay name one;reset(names, inst)sends a reset. - The engine assigns an update's values at one event, journals it, then calls
on_params(ctx). Parameters it does not name keep their values. [strategy] max_param_age_ms(0, off, by default) disables quoting before the first update and when none arrived for that long in engine time. The quotes are pulled andon_quoting(false)fires; the next update fireson_quoting(true).- Replay applies the journaled updates at the same events, matched to the parameters by name (Journal format).
State across sessions¶
A strategy can keep what it has learnt, such as an online model, in a state file between sessions and in the journal's checkpoints, so a backtest can start from it mid-session. It implements both members or neither:
| Member | Does |
|---|---|
std::string_view state() |
returns the bytes; the engine copies them before the strategy's next hook |
bool restore(std::string_view) |
takes bytes saved before; false when they are not its own, which the engine logs before it runs on with what restore left |
state_version |
optional, static constexpr std::uint32_t or a member function: the layout of the bytes, 0 when not declared |
class LearntSpread : public StrategyBase<AllHooksParams> {
public:
static constexpr std::string_view name() noexcept { return "doc_learnt_spread"; }
static constexpr std::uint32_t state_version = 2; // bump when the layout below changes
template <class Ctx>
void on_start(Ctx& ctx) noexcept {
static_cast<void>(ctx.every(seconds(3600), 0));
}
template <class Ctx>
void on_fill(Ctx&, const Fill& f) noexcept {
edge_[f.side == Side::Buy ? 0 : 1] += 1.0;
++fills_;
}
template <class Ctx>
void on_timer(Ctx& ctx, TimerId, std::uint64_t) noexcept {
ctx.request_checkpoint(); // once an hour, besides [engine] checkpoint_s
}
std::string_view state() noexcept {
StateWriter w(buf_);
w.put(edge_);
w.put(fills_);
return w.view();
}
bool restore(std::string_view bytes) noexcept {
StateReader r(bytes);
std::array<double, 2> edge{};
std::uint64_t fills = 0;
if (!r.get(edge) || !r.get(fills) || !r.done()) return false; // keeps what it has
edge_ = edge;
fills_ = fills;
return true;
}
private:
std::array<double, 2> edge_{};
std::uint64_t fills_ = 0;
std::array<std::byte, 64> buf_{}; // sized once: state() never allocates
};
StateWriter copies trivially copyable values into a buffer the strategy sizes once, and put_array a count and its elements. A write that does not fit sets ok() false and leaves view() empty; the engine then keeps the state file as it was, writes checkpoints without strategy bytes and logs a warning once. StateReader reads the values back in the same order, and done() is true once every byte was read. Read into locals and assign them once all are read, so bytes of another layout leave the strategy as it was. The signatures are checked at registration like the hooks.
| Run | restore |
state() |
|---|---|---|
live session, backtest, with [strategy] state_file |
the file, once, after on_start, before the first event |
to the file every state_interval_s (300 s) of engine time and at the end |
| any session with a journal | into a checkpoint every [engine] checkpoint_s, and once the events waiting are handled after ctx.request_checkpoint() |
|
backtest with [backtest] initial_state = "journal" |
the bytes the session restored at its start, else its first checkpoint, after on_start |
as above |
backtest with [backtest] initial_state = "journal" and start_at |
the last checkpoint at or before start_at, when trading starts there (Replay a live session) |
as above |
warm standby (fastmm-live --standby) |
the file when it takes over, after warming up (Hand over a running session) | as a live session, from takeover |
sweep points, walk-forward folds, [ab] arms |
the file as it was when they began | never |
| replay of a journal | the bytes the session restored, at the same events | never |
- No file is a first start; an unreadable file is a configuration error.
state()is never called from inside another hook. File writes are atomic, a temporary file then a rename, off the engine thread.- The state file and each checkpoint record the
state_version; the file holds a 16-byte head (Journal format) before the bytes. A backtest,fastmm-replayorfastmm-livestops with an error before the first event when that version differs from the strategy's, and never hands those bytes torestore. A file without the head, written before it existed, is version 0. - A checkpoint costs
state()and one copy of its bytes into the journal ring on the engine thread: about 25 µs for 1 MiB and 90 µs for 4 MiB on a Ryzen 7 7800X3D (BM_Journal_Checkpoint). One that would leave less than a quarter of[engine] journal_ring_bytesfree is skipped, logged and counted:checkpoints,checkpoints_skippedandcheckpoint_bytesare in a backtest'ssummary.jsonandengine_stats(). - A backtest's end state is what the next backtest or live session starts from.
Fixed-point helpers¶
static_assert(100.25_px == Price::from_raw(10'025'000'000));
static_assert(0.01_qty == Qty::from_raw(1'000'000));
static_assert(5_bps == Ratio::from_raw(50'000)); // 1 bp = 10'000 raw, 1.0 = 1e8 raw
static_assert(100_px * 5_bps == 0.05_px); // Price * Ratio, one truncation toward zero
static_assert(ratio(1_px, 4_px) == Ratio::from_raw(25'000'000));
static_assert(mid(100.00_px, 100.02_px) == 100.01_px);
static_assert(microprice(Level{100.00_px, 3_qty}, Level{100.04_px, 1_qty}) == 100.03_px);
static_assert(spread_ratio(99_px, 101_px) == 200_bps);
static_assert(away_from(100_px, Side::Buy, 0.5_px) == 99.5_px);
static_assert(inventory_allows(Side::Buy, 0.003_qty, 0.001_qty, 0.004_qty));
static_assert(!inventory_allows(Side::Buy, 0.004_qty, 0.001_qty, 0.004_qty));
static_assert(inventory_allows(Side::Sell, 5_qty, 1_qty, Qty{})); // a zero limit is no limit
static_assert(std::same_as<decltype(lvalue<const Instrument>().ticks(3)), Price>);
static_assert(std::same_as<decltype(lvalue<DesiredQuotes>().bid(Price{}, Qty{})), bool>);
static_assert(std::same_as<decltype(lvalue<DesiredQuotes>().uncross(Price{})), void>);
static_assert(
std::same_as<decltype(keep_passive(lvalue<DesiredQuotes>(), Price{}, Price{}, Price{})), void>);
static_assert(
std::same_as<decltype(NewOrderRequest::limit(InstrumentId{}, Side::Buy, Price{}, Qty{})
.post_only()
.reduce_only()
.ioc()
.tag(7)
.account(VenueId{})),
LimitOrder>);
static_assert(std::convertible_to<LimitOrder, NewOrderRequest>);
NewOrderRequest::limit(id, side, px, qty) builds a GTC limit order. Types, literals, rounding rules, quoting helpers and ranges: Fixed point.
Test harness¶
fastmm::sim::StrategyHarness<S> (fastmm/testing/strategy_harness.hpp) runs a strategy in a real engine against a simulated venue on virtual time. Each call runs the engine until it is idle.
sim::StrategyHarness<AllHooks> h({{"quote_qty", "0.001"}, {"timer_ms", "5"}}); // on_start
h.book("100.00", "100.02"); // on_book; quotes go out
h.advance(milliseconds(10)); // acks (on_order_update), the timer (on_timer)
REQUIRE(h.working_orders().size() == 2);
REQUIRE(h.fill(Side::Buy)); // on_fill, then on_order_update
h.trade(100.02_px, 0.5_qty, Side::Buy); // on_trade
BookTickerMsg ticker{};
init_header(ticker, EventType::BookTicker, h.instrument(), VenueId{0});
ticker.bid_px = 100.00_px;
ticker.ask_px = 100.02_px;
h.push(ticker.hdr); // on_book_ticker
OptionTickerMsg option{};
init_header(option, EventType::OptionTicker, h.instrument(), VenueId{0});
h.push(option.hdr); // on_option_ticker
h.disconnect(); // on_connection (market data lost)
h.reconnect(); // on_connection (live again)
h.pull_quotes(); // on_quoting(false)
h.resume_quotes(); // on_quoting(true)
h.publish({{"half_spread_bps", "7.5"}}); // on_params
BalanceMsg usdt{};
init_header(usdt, EventType::Balance, InstrumentId::invalid(), VenueId{0});
usdt.asset.assign("USDT");
usdt.free = Notional::from_int(1000);
h.push(usdt.hdr); // on_balance
PerpStateMsg perp{};
init_header(perp, EventType::PerpState, h.instrument(), VenueId{0});
perp.mark_price = 100.01_px;
perp.fields = PerpStateMsg::kMark;
h.push(perp.hdr); // on_perp_state
h.engine().finish(); // on_stop
| Call | Effect |
|---|---|
StrategyHarness<S>(params, options) |
configures the strategy and starts the engine; std::invalid_argument on a bad value. HarnessOptions sets instruments, EngineConfig, latency (100 µs each way) and start time |
book(bid, ask[, size, id]) |
a one-level snapshot each side; prices as strings or Price |
trade(px, qty, aggressor[, id]) |
a public trade |
fill(side[, qty, id]) |
a taker fills our best working order on side, all of it when qty is 0, then time advances by the ack latency; false when there is none |
disconnect([channel, venue]), reconnect(...) |
a ConnectionStateMsg; channel 0 is market data |
pull_quotes(), resume_quotes() |
operator control: on_quoting(false), on_quoting(true) |
publish(values) |
a parameter update for all instruments now, then on_params; std::invalid_argument when a publisher would reject it |
push(msg.hdr) |
any other message at the current time |
advance(duration) |
moves virtual time: orders reach the venue, acks and fills return, timers fire |
working_orders([id]) |
our working orders, bids then asks, best first |
engine(), strategy(), now(), instrument(), price(s), quantity(s) |
access and conversions |
The default instrument is BTCUSDT on venue 0, tick 0.01, lot 0.001. The harness allocates and is single-threaded.
Registration¶
void register_strategies(StrategyRegistry& r) {
register_strategy<AllHooks>(r); // Sim + Replay + Live
}
static_assert(std::same_as<decltype(®ister_strategies), StrategyModule>);
register_strategy<S>(r)adds the Sim, Replay and Live factories ofS. Its file includesfastmm/strategies/factories.hpp, which compiles the three engines; without it the link fails naming the factory.- A strategy library exports one such function, a
StrategyModule; apps pass it tofastmm::cli::live,backtestorreplay. Built-in strategies are registered first. - Registering the same function twice does nothing. A name registered by different code throws
StrategyConflict, and the command lines exit with code 3. bt::run_backtest<S>(cfg, source)andStrategyHarness<S>need no registration.
Build setups: Register a strategy.
Logging¶
FASTMM_LOG_TRACE, FASTMM_LOG_DEBUG, FASTMM_LOG_INFO, FASTMM_LOG_WARN and FASTMM_LOG_ERROR take a fmt format string; Price, Qty, Side and the enums format directly.
- Records go through a per-thread ring to a background thread; a full ring drops the record and counts it.
- A line carries wall-clock time live, and simulated time in a backtest or replay.
- Logs are not journaled, so strategy logic must not depend on them.