algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::strategy::RiskGuard Class Reference

Checks planned orders against RiskLimits, holds trading stopped, and counts orders sent. More...

#include <algotradelib/src/strategy/risk.hpp>

Public Member Functions

 RiskGuard (RiskLimits limits={})
 Constructs a guard.
const RiskLimits & limits () const noexcept
 Returns the limits.
bool permits_phase (MarketPhase phase) const noexcept
 Tests whether a phase permits opening or increasing a position.
void halt () noexcept
 Takes the operator's hold (HaltReason::OPERATOR): no order or cancellation is sent until resume().
void halt (HaltReason reason, std::string detail)
 Takes a hold, with a description.
void halt (HaltReason reason)
 Takes a hold, with no description.
void resume () noexcept
 Releases the operator's hold; other holds, if any, still stop trading.
void resume (HaltReason reason) noexcept
 Releases a hold; releasing one that is not held does nothing.
bool halted () const noexcept
 Tests whether any hold is in force.
bool halted (HaltReason reason) const noexcept
 Tests whether one hold is in force.
std::optional< HaltReason > blocking_reason () const noexcept
 Returns the hold that stops placements, the most fundamental first.
std::optional< HaltReason > blocking_reason_for (InstructionClass klass) const noexcept
 Returns the hold that stops an instruction class, the most fundamental first.
std::vector< HaltReason > holds () const
 Returns every hold in force.
void note_sent (std::chrono::system_clock::time_point when)
 Records that a request was sent, for the rate limit.
std::size_t total_sent () const noexcept
 Returns how many orders have been recorded with note_sent over the guard's life.
std::size_t recent_orders (std::chrono::system_clock::time_point now) const
 Returns how many orders were sent within RiskLimits::rate_window_ before an instant.
void forget_before (std::chrono::system_clock::time_point cutoff)
 Forgets sends recorded before an instant, to bound memory.
template<typename Broker>
std::optional< GuardRejection > check (const PlannedOrder &order, const Portfolio< Broker > &portfolio, const GuardInputs &inputs) const
 Checks one planned order against the limits and holds.
template<typename Broker>
GuardDecision evaluate (const PlannedOrder &order, const Portfolio< Broker > &portfolio, const GuardInputs &inputs) const
 Checks one planned order, and reports whether only the closing exception admitted it.

Detailed Description

Checks planned orders against RiskLimits, holds trading stopped, and counts orders sent.

The rate limit counts what was recorded with note_sent. The runner records every placement or cancellation that was sent or may have been sent: accepted, rejected by the broker, or of unknown outcome. A request refused before sending (by this guard, the binding or the adapter) is not recorded, so it uses no budget.

Note
Not thread-safe; one guard belongs to one runner.

Definition at line 706 of file risk.hpp.

Constructor & Destructor Documentation

◆ RiskGuard()

algotradelib::strategy::RiskGuard::RiskGuard ( RiskLimits limits = {})
inlineexplicit

Constructs a guard.

Parameters
limitsThe limits to enforce.

Definition at line 712 of file risk.hpp.

Member Function Documentation

◆ blocking_reason()

std::optional< HaltReason > algotradelib::strategy::RiskGuard::blocking_reason ( ) const
inlinenoexcept

Returns the hold that stops placements, the most fundamental first.

Returns
The first held reason in HaltReason order, or std::nullopt if none.

Definition at line 804 of file risk.hpp.

Referenced by evaluate().

◆ blocking_reason_for()

std::optional< HaltReason > algotradelib::strategy::RiskGuard::blocking_reason_for ( InstructionClass klass) const
inlinenoexcept

Returns the hold that stops an instruction class, the most fundamental first.

Parameters
klassThe instruction class; see halt_blocks.
Returns
The first held reason that stops klass, or std::nullopt if none.

Definition at line 813 of file risk.hpp.

Referenced by blocking_reason().

◆ check()

template<typename Broker>
std::optional< GuardRejection > algotradelib::strategy::RiskGuard::check ( const PlannedOrder & order,
const Portfolio< Broker > & portfolio,
const GuardInputs & inputs ) const
inline

Checks one planned order against the limits and holds.

Template Parameters
BrokerThe portfolio's broker tag.
Parameters
orderThe order to check.
portfolioThe current positions and working orders.
inputsThe instant, phase, prices and currencies the checks need.
Returns
Why the order is refused, or std::nullopt if it is permitted.
See also
evaluate, which also reports whether the order was admitted by the closing exception.

Definition at line 905 of file risk.hpp.

◆ evaluate()

template<typename Broker>
GuardDecision algotradelib::strategy::RiskGuard::evaluate ( const PlannedOrder & order,
const Portfolio< Broker > & portfolio,
const GuardInputs & inputs ) const
inline

Checks one planned order, and reports whether only the closing exception admitted it.

Checks run in this order and the first failure is reported: holds, permitted universe, permitted phases (with the closing-toward-flat exception), order rate, order notional, sellable quantity, buying power, position ceiling, gross exposure ceiling.

Template Parameters
BrokerThe portfolio's broker tag.
Parameters
orderThe order to check.
portfolioThe current positions and working orders.
inputsThe instant, phase, prices and currencies the checks need.
Returns
The decision.

Definition at line 927 of file risk.hpp.

Referenced by check().

◆ forget_before()

void algotradelib::strategy::RiskGuard::forget_before ( std::chrono::system_clock::time_point cutoff)
inline

Forgets sends recorded before an instant, to bound memory.

Parameters
cutoffSends earlier than this are dropped; pass a time at least rate_window_ ago.

Definition at line 888 of file risk.hpp.

◆ halt() [1/3]

void algotradelib::strategy::RiskGuard::halt ( )
inlinenoexcept

Takes the operator's hold (HaltReason::OPERATOR): no order or cancellation is sent until resume().

This only stops sending ("quiesce"): working orders and positions are left as they are. To also cancel working orders or close positions, use runner::StopIntent with runner::PollingLoop, or a strategy such as vanilla::TimedLiquidation. The hold is in memory; runner::FileStopChannel and runner::SignalStop stop a running process from outside.

Definition at line 746 of file risk.hpp.

◆ halt() [2/3]

void algotradelib::strategy::RiskGuard::halt ( HaltReason reason)
inline

Takes a hold, with no description.

Parameters
reasonThe hold.

Definition at line 766 of file risk.hpp.

Referenced by halt().

◆ halt() [3/3]

void algotradelib::strategy::RiskGuard::halt ( HaltReason reason,
std::string detail )
inline

Takes a hold, with a description.

Taking a hold that is already held replaces its detail; one resume(reason) releases it however many times it was taken.

Parameters
reasonThe hold.
detailA description for the operator, returned by hold_detail.

Definition at line 757 of file risk.hpp.

◆ halted() [1/2]

bool algotradelib::strategy::RiskGuard::halted ( ) const
inlinenoexcept

Tests whether any hold is in force.

Returns
true if at least one hold is held.

Definition at line 784 of file risk.hpp.

◆ halted() [2/2]

bool algotradelib::strategy::RiskGuard::halted ( HaltReason reason) const
inlinenoexcept

Tests whether one hold is in force.

Parameters
reasonThe hold.
Returns
true if it is held.

Definition at line 798 of file risk.hpp.

◆ holds()

std::vector< HaltReason > algotradelib::strategy::RiskGuard::holds ( ) const
inline

Returns every hold in force.

Returns
The held reasons, most fundamental first.

Definition at line 836 of file risk.hpp.

◆ limits()

const RiskLimits & algotradelib::strategy::RiskGuard::limits ( ) const
inlinenoexcept

Returns the limits.

Returns
The limits in force.

Definition at line 718 of file risk.hpp.

Referenced by RiskGuard().

◆ note_sent()

void algotradelib::strategy::RiskGuard::note_sent ( std::chrono::system_clock::time_point when)
inline

Records that a request was sent, for the rate limit.

The runner calls it for each placement or cancellation the broker accepted, rejected, or may have received (unknown outcome).

Parameters
whenWhen the order was sent.

Definition at line 854 of file risk.hpp.

◆ permits_phase()

bool algotradelib::strategy::RiskGuard::permits_phase ( MarketPhase phase) const
inlinenoexcept

Tests whether a phase permits opening or increasing a position.

Parameters
phaseThe market phase.
Returns
true if RiskLimits::permitted_phases_ is absent or contains phase.

Definition at line 725 of file risk.hpp.

◆ recent_orders()

std::size_t algotradelib::strategy::RiskGuard::recent_orders ( std::chrono::system_clock::time_point now) const
inline

Returns how many orders were sent within RiskLimits::rate_window_ before an instant.

Parameters
nowThe instant to count back from.
Returns
The count.

Definition at line 874 of file risk.hpp.

Referenced by evaluate().

◆ resume() [1/2]

void algotradelib::strategy::RiskGuard::resume ( )
inlinenoexcept

Releases the operator's hold; other holds, if any, still stop trading.

Definition at line 769 of file risk.hpp.

Referenced by resume().

◆ resume() [2/2]

void algotradelib::strategy::RiskGuard::resume ( HaltReason reason)
inlinenoexcept

Releases a hold; releasing one that is not held does nothing.

Parameters
reasonThe hold.

Definition at line 775 of file risk.hpp.

◆ total_sent()

std::size_t algotradelib::strategy::RiskGuard::total_sent ( ) const
inlinenoexcept

Returns how many orders have been recorded with note_sent over the guard's life.

Unlike recent_orders, it never decreases, so a caller can difference it across a call to learn whether anything was sent in between.

Returns
The total count.

Definition at line 867 of file risk.hpp.


The documentation for this class was generated from the following file: