FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
fastmm::HedgeExecutor Class Reference

#include <fastmm/strategies/hedge_executor.hpp>

Classes

struct  Config
 
struct  Stats
 
struct  Status
 

Public Types

enum class  State : std::uint8_t {
  Idle , Flat , Waiting , InFlight ,
  Reconciling , Holding , Held , Unavailable ,
  Halted
}
 What the last evaluation found. More...
 
enum class  Reason : std::uint8_t {
  Usable , Down , Killed , NoBook ,
  Stale , Gated , Benched
}
 Why a hedge instrument is not usable now. More...
 

Public Member Functions

void reset () noexcept
 Forgets the legs and every state but the stats. Call before adding legs in on_start.
 
bool add_source (InstrumentId id) noexcept
 
bool add_hedge (InstrumentId id, Ratio tolerance) noexcept
 
template<class Ctx >
bool start (Ctx &ctx, const Config &cfg) noexcept
 Checks the legs against the instrument table: each is configured, linear, used once, and there is at least one of each.
 
void set_config (const Config &cfg) noexcept
 Parameters may change while running (on_params); the legs may not.
 
void set_tolerance (std::size_t hedge, Ratio tolerance) noexcept
 
void set_target (Qty base) noexcept
 The net base position the strategy wants to hold; the residual is measured from it.
 
void restart () noexcept
 Clears a halt, the benches and the failure counts; hedging resumes from the positions.
 
template<class Ctx >
bool on_fill (Ctx &ctx, const Fill &fill) noexcept
 A fill of any leg: the residual moved. False for another instrument.
 
template<class Ctx >
bool on_order_update (Ctx &ctx, const OmsUpdate &u) noexcept
 The end of a hedge or de-risk order: counts a failure or holds, then looks again.
 
template<class Ctx >
void on_book (Ctx &ctx, InstrumentId id) noexcept
 A hedge instrument's book: a new touch to price against.
 
template<class Ctx >
void on_timer (Ctx &ctx) noexcept
 
template<class Ctx >
void on_connection (Ctx &, const ConnectionStateMsg &m) noexcept
 Order channels (channel != 0) of the legs' venues.
 
template<class Ctx >
bool on_balance (Ctx &ctx, const BalanceMsg &m) noexcept
 A balance report of a leg's venue: a held hedge may fit now. False for another venue.
 
template<class Ctx >
void update (Ctx &ctx) noexcept
 Looks at the positions and sends what they call for; the hooks above end here.
 
bool ready () const noexcept
 
bool halted () const noexcept
 
bool held () const noexcept
 The last hedge the positions asked for fits no usable instrument's balance.
 
bool derisking () const noexcept
 
State state () const noexcept
 
const Stats & stats () const noexcept
 
std::size_t hedge_count () const noexcept
 
InstrumentId hedge_id (std::size_t i) const noexcept
 
std::size_t active () const noexcept
 
Qty target () const noexcept
 
bool is_hedge (InstrumentId id) const noexcept
 
bool is_source (InstrumentId id) const noexcept
 
bool is_leg (InstrumentId id) const noexcept
 
bool involves (VenueId v) const noexcept
 A venue of any leg.
 
template<class Ctx >
Qty residual (const Ctx &ctx) const noexcept
 Sources plus hedges minus the target, base units.
 
template<class Ctx >
Reason why (const Ctx &ctx, std::size_t i, std::int64_t now) const noexcept
 Why hedge instrument i cannot take a hedge now (Reason::Usable: it can).
 
template<class Ctx >
bool can_hedge (const Ctx &ctx) const noexcept
 Some hedge instrument can take a hedge now, and hedging is not halted.
 
template<class Ctx >
Status status (const Ctx &ctx) const noexcept
 

Static Public Member Functions

static constexpr Qty to_base (const Instrument &inst, Qty contracts) noexcept
 Contracts to base units and back (linear: qty * multiplier).
 
static constexpr Qty to_contracts (const Instrument &inst, Qty base) noexcept
 
static std::optional< NewOrderRequest > hedge_order (const Instrument &inst, Qty residual, Price best_bid, Price best_ask, Ratio tolerance, std::uint32_t tag) noexcept
 The order on inst that brings residual (base units) back towards zero: an IOC of -residual in contracts, rounded down to the lot and capped at max_qty, priced tolerance through the touch and rounded towards it.
 
static constexpr Qty qty_within (const Instrument &inst, Price price, Notional cap) noexcept
 The largest quantity of inst whose notional at price is at most cap, rounded down to the lot (zero when not even one lot fits, or price is not positive).
 
static constexpr Qty risk_room (const RiskHeadroom &h, const Instrument &inst, Side side, Price price) noexcept
 The largest order of side on inst at price that [risk] admits now: max_order_qty, max_order_notional at that price, that side's room of max_position and of [risk.underlying] max_net, and what max_gross_notional and max_net_notional leave for it at that price.
 
static constexpr bool fit_risk (const RiskHeadroom &h, const Instrument &inst, NewOrderRequest &r) noexcept
 Cuts r to risk_room() when that is less and still an order the instrument takes (the lot, min_qty, min_notional).
 
static bool uncertain (const OmsUpdate &u) noexcept
 A terminal update whose outcome the venue did not report: the ack timeout cancelled an order the venue never acknowledged, reconciliation found it gone with quantity unaccounted for, or a connector turned a request whose outcome it does not know into a generic reject.
 
static constexpr std::string_view to_string (Reason r) noexcept
 
static constexpr std::string_view to_string (State s) noexcept
 

Static Public Attributes

static constexpr std::size_t kMaxSources = 4
 
static constexpr std::size_t kMaxHedges = 4
 
static constexpr std::size_t kMaxFailures = 16
 

Member Enumeration Documentation

◆ State

enum class fastmm::HedgeExecutor::State : std::uint8_t
strong

What the last evaluation found.

Enumerator
Idle 

not started, or nothing evaluated yet

Flat 

the residual is zero

Waiting 

the residual rounds under the hedge instrument's minimum

InFlight 

a hedge or de-risk order is open

Reconciling 

a venue reconciles

Holding 

retry backoff or uncertain hold

Held 

no usable instrument's balance or margin covers the hedge

Unavailable 

no hedge instrument is usable

Halted 

every hedge instrument failed max_failures times; restart() clears it

◆ Reason

enum class fastmm::HedgeExecutor::Reason : std::uint8_t
strong

Why a hedge instrument is not usable now.

Enumerator
Usable 
Down 
Killed 
NoBook 
Stale 
Gated 
Benched 

Member Function Documentation

◆ reset()

void fastmm::HedgeExecutor::reset ( )
inlinenoexcept

Forgets the legs and every state but the stats. Call before adding legs in on_start.

◆ add_source()

bool fastmm::HedgeExecutor::add_source ( InstrumentId  id)
inlinenoexcept

◆ add_hedge()

bool fastmm::HedgeExecutor::add_hedge ( InstrumentId  id,
Ratio  tolerance 
)
inlinenoexcept

◆ start()

template<class Ctx >
bool fastmm::HedgeExecutor::start ( Ctx &  ctx,
const Config &  cfg 
)
inlinenoexcept

Checks the legs against the instrument table: each is configured, linear, used once, and there is at least one of each.

Logs the reason and stays idle when not.

◆ set_config()

void fastmm::HedgeExecutor::set_config ( const Config &  cfg)
inlinenoexcept

Parameters may change while running (on_params); the legs may not.

◆ set_tolerance()

void fastmm::HedgeExecutor::set_tolerance ( std::size_t  hedge,
Ratio  tolerance 
)
inlinenoexcept

◆ set_target()

void fastmm::HedgeExecutor::set_target ( Qty  base)
inlinenoexcept

The net base position the strategy wants to hold; the residual is measured from it.

◆ restart()

void fastmm::HedgeExecutor::restart ( )
inlinenoexcept

Clears a halt, the benches and the failure counts; hedging resumes from the positions.

◆ on_fill()

template<class Ctx >
bool fastmm::HedgeExecutor::on_fill ( Ctx &  ctx,
const Fill &  fill 
)
inlinenoexcept

A fill of any leg: the residual moved. False for another instrument.

◆ on_order_update()

template<class Ctx >
bool fastmm::HedgeExecutor::on_order_update ( Ctx &  ctx,
const OmsUpdate &  u 
)
inlinenoexcept

The end of a hedge or de-risk order: counts a failure or holds, then looks again.

False for anything else (another instrument, or an update that is not terminal).

◆ on_book()

template<class Ctx >
void fastmm::HedgeExecutor::on_book ( Ctx &  ctx,
InstrumentId  id 
)
inlinenoexcept

A hedge instrument's book: a new touch to price against.

◆ on_timer()

template<class Ctx >
void fastmm::HedgeExecutor::on_timer ( Ctx &  ctx)
inlinenoexcept

◆ on_connection()

template<class Ctx >
void fastmm::HedgeExecutor::on_connection ( Ctx &  ,
const ConnectionStateMsg &  m 
)
inlinenoexcept

Order channels (channel != 0) of the legs' venues.

Market data is covered by the books' validity: the engine clears a venue's books when its channel drops, and a book resync is a Resyncing followed by snapshots with no Live. Does not send: the next event does.

◆ on_balance()

template<class Ctx >
bool fastmm::HedgeExecutor::on_balance ( Ctx &  ctx,
const BalanceMsg &  m 
)
inlinenoexcept

A balance report of a leg's venue: a held hedge may fit now. False for another venue.

◆ update()

template<class Ctx >
void fastmm::HedgeExecutor::update ( Ctx &  ctx)
inlinenoexcept

Looks at the positions and sends what they call for; the hooks above end here.

The first usable instrument decides the size; a hedge its balance cannot cover goes to the next usable instrument that can take it.

◆ ready()

bool fastmm::HedgeExecutor::ready ( ) const
inlinenoexcept

◆ halted()

bool fastmm::HedgeExecutor::halted ( ) const
inlinenoexcept

◆ held()

bool fastmm::HedgeExecutor::held ( ) const
inlinenoexcept

The last hedge the positions asked for fits no usable instrument's balance.

◆ derisking()

bool fastmm::HedgeExecutor::derisking ( ) const
inlinenoexcept

◆ state()

State fastmm::HedgeExecutor::state ( ) const
inlinenoexcept

◆ stats()

const Stats & fastmm::HedgeExecutor::stats ( ) const
inlinenoexcept

◆ hedge_count()

std::size_t fastmm::HedgeExecutor::hedge_count ( ) const
inlinenoexcept

◆ hedge_id()

InstrumentId fastmm::HedgeExecutor::hedge_id ( std::size_t  i) const
inlinenoexcept

◆ active()

std::size_t fastmm::HedgeExecutor::active ( ) const
inlinenoexcept

◆ target()

Qty fastmm::HedgeExecutor::target ( ) const
inlinenoexcept

◆ is_hedge()

bool fastmm::HedgeExecutor::is_hedge ( InstrumentId  id) const
inlinenoexcept

◆ is_source()

bool fastmm::HedgeExecutor::is_source ( InstrumentId  id) const
inlinenoexcept

◆ is_leg()

bool fastmm::HedgeExecutor::is_leg ( InstrumentId  id) const
inlinenoexcept

◆ involves()

bool fastmm::HedgeExecutor::involves ( VenueId  v) const
inlinenoexcept

A venue of any leg.

◆ residual()

template<class Ctx >
Qty fastmm::HedgeExecutor::residual ( const Ctx &  ctx) const
inlinenoexcept

Sources plus hedges minus the target, base units.

◆ why()

template<class Ctx >
Reason fastmm::HedgeExecutor::why ( const Ctx &  ctx,
std::size_t  i,
std::int64_t  now 
) const
inlinenoexcept

Why hedge instrument i cannot take a hedge now (Reason::Usable: it can).

◆ can_hedge()

template<class Ctx >
bool fastmm::HedgeExecutor::can_hedge ( const Ctx &  ctx) const
inlinenoexcept

Some hedge instrument can take a hedge now, and hedging is not halted.

A quoting strategy pulls its quotes while this is false: a fill could not be hedged, and a de-risk order may be working.

◆ status()

template<class Ctx >
Status fastmm::HedgeExecutor::status ( const Ctx &  ctx) const
inlinenoexcept

◆ to_base()

static constexpr Qty fastmm::HedgeExecutor::to_base ( const Instrument &  inst,
Qty  contracts 
)
inlinestaticconstexprnoexcept

Contracts to base units and back (linear: qty * multiplier).

◆ to_contracts()

static constexpr Qty fastmm::HedgeExecutor::to_contracts ( const Instrument &  inst,
Qty  base 
)
inlinestaticconstexprnoexcept

◆ hedge_order()

static std::optional< NewOrderRequest > fastmm::HedgeExecutor::hedge_order ( const Instrument &  inst,
Qty  residual,
Price  best_bid,
Price  best_ask,
Ratio  tolerance,
std::uint32_t  tag 
)
inlinestaticnoexcept

The order on inst that brings residual (base units) back towards zero: an IOC of -residual in contracts, rounded down to the lot and capped at max_qty, priced tolerance through the touch and rounded towards it.

None when it rounds under the lot or min_qty, its notional is under min_notional, or the touch it needs is empty: a hedge the venue (or the risk check) must refuse would only count as a failure.

◆ qty_within()

static constexpr Qty fastmm::HedgeExecutor::qty_within ( const Instrument &  inst,
Price  price,
Notional  cap 
)
inlinestaticconstexprnoexcept

The largest quantity of inst whose notional at price is at most cap, rounded down to the lot (zero when not even one lot fits, or price is not positive).

The notional rounds at each step: never over the cap.

◆ risk_room()

static constexpr Qty fastmm::HedgeExecutor::risk_room ( const RiskHeadroom &  h,
const Instrument &  inst,
Side  side,
Price  price 
)
inlinestaticconstexprnoexcept

The largest order of side on inst at price that [risk] admits now: max_order_qty, max_order_notional at that price, that side's room of max_position and of [risk.underlying] max_net, and what max_gross_notional and max_net_notional leave for it at that price.

Qty::max() when none of them is set.

◆ fit_risk()

static constexpr bool fastmm::HedgeExecutor::fit_risk ( const RiskHeadroom &  h,
const Instrument &  inst,
NewOrderRequest &  r 
)
inlinestaticconstexprnoexcept

Cuts r to risk_room() when that is less and still an order the instrument takes (the lot, min_qty, min_notional).

True when it was cut. An order the limits leave no such room for stays whole: the engine's refusal is then what the caller counts.

◆ uncertain()

static bool fastmm::HedgeExecutor::uncertain ( const OmsUpdate &  u)
inlinestaticnoexcept

A terminal update whose outcome the venue did not report: the ack timeout cancelled an order the venue never acknowledged, reconciliation found it gone with quantity unaccounted for, or a connector turned a request whose outcome it does not know into a generic reject.

◆ to_string() [1/2]

static constexpr std::string_view fastmm::HedgeExecutor::to_string ( Reason  r)
inlinestaticconstexprnoexcept

◆ to_string() [2/2]

static constexpr std::string_view fastmm::HedgeExecutor::to_string ( State  s)
inlinestaticconstexprnoexcept

Member Data Documentation

◆ kMaxSources

constexpr std::size_t fastmm::HedgeExecutor::kMaxSources = 4
staticconstexpr

◆ kMaxHedges

constexpr std::size_t fastmm::HedgeExecutor::kMaxHedges = 4
staticconstexpr

◆ kMaxFailures

constexpr std::size_t fastmm::HedgeExecutor::kMaxFailures = 16
staticconstexpr