![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
The limits the operator sets for RiskGuard. More...
#include <algotradelib/src/strategy/risk.hpp>
Public Attributes | |
| std::optional< Decimal64 > | max_order_notional_ |
| The largest value one order may have, in its own currency. | |
| std::optional< Decimal64 > | max_position_quantity_ |
| The largest absolute position, in shares, any instrument may reach. | |
| std::optional< Decimal64 > | max_gross_exposure_ |
| The largest gross exposure the portfolio may reach, in the order's currency. | |
| std::optional< std::size_t > | max_orders_per_window_ |
| The most orders that may be sent within rate_window_. | |
| std::chrono::seconds | rate_window_ {60} |
| The window max_orders_per_window_ counts over. | |
| bool | enforce_sellable_quantity_ {true} |
| Whether to refuse a sell larger than the broker's sellable quantity. | |
| bool | enforce_buying_power_ {false} |
| Whether to refuse a buy that costs more than the broker's buying power. | |
| std::optional< std::vector< InstrumentId > > | permitted_universe_ |
| The instruments that may be traded; absent means any. | |
| std::optional< std::vector< MarketPhase > > | permitted_phases_ {std::vector<MarketPhase>{MarketPhase::OPEN}} |
| The market phases in which a position may be opened or increased; absent means any. | |
| std::optional< Money > | run_budget_ |
| The run's starting cash, in one currency: buys spend it, sells return their proceeds, and the run never borrows. | |
| std::optional< RunFees > | run_fees_ |
| The fees a run budget estimates for each order; required with run_budget_, unread without it. | |
The limits the operator sets for RiskGuard.
Every limit is optional and absent means unlimited, except permitted_phases_, which by default permits opening positions only while the market is OPEN, and enforce_sellable_quantity_, which is on by default. Holds (halts) are not limits; they are taken on the guard itself.
| bool algotradelib::strategy::RiskLimits::enforce_buying_power_ {false} |
| bool algotradelib::strategy::RiskLimits::enforce_sellable_quantity_ {true} |
Whether to refuse a sell larger than the broker's sellable quantity.
On by default: a sell is refused if the broker gave no sellable figure (the held quantity is not assumed sellable) or the figure's basis is not established. So an account that cannot sell what it does not hold never sends a sale that would open a short: every shipped adapter places cash orders, and a target below zero is refused here before it is sent.
Turn it off, where the replay or the dry run is configured, to simulate short positions; that models no borrow. Turning it off for a live account borrows nothing either: it only lets the sale reach the broker.
| std::optional<Decimal64> algotradelib::strategy::RiskLimits::max_gross_exposure_ |
The largest gross exposure the portfolio may reach, in the order's currency.
Checked against the exposure if this order and every outstanding order filled, each valued at the highest price that could apply (see projected_gross_exposure). The ceiling applies to each currency separately; there is no account-wide converted cap.
An order that does not raise this projection is permitted at any size, so an exit (a sell up to flat against a long) is allowed even when the account is over the ceiling. A sell that goes through flat into a short raises it and can be refused. To cap one order's size, use max_order_notional_. An order that cannot be valued, or an incomplete GuardInputs::exposure_basis_, is refused with GuardReason::VALUE_NOT_KNOWN.
| std::optional<Decimal64> algotradelib::strategy::RiskLimits::max_order_notional_ |
| std::optional<std::size_t> algotradelib::strategy::RiskLimits::max_orders_per_window_ |
The most orders that may be sent within rate_window_.
| std::optional<Decimal64> algotradelib::strategy::RiskLimits::max_position_quantity_ |
| std::optional<std::vector<MarketPhase> > algotradelib::strategy::RiskLimits::permitted_phases_ {std::vector<MarketPhase>{MarketPhase::OPEN}} |
The market phases in which a position may be opened or increased; absent means any.
The default permits only OPEN, so UNKNOWN (an uncovered calendar date) is refused.
An order that only closes toward flat (it opposes the net held quantity and, with what is already working on the closing side, cannot carry it through flat) is permitted in any phase, so a flatten after the close can still be sent. The later checks (sellable, buying power, position and exposure ceilings) still apply to it, and the broker will likely reject it outside its session; max_orders_per_window_ limits how often such an order is resent, and runner::Runner::sends_outside_permitted_phase counts them. Cancellations are never checked against this.
| std::optional<std::vector<InstrumentId> > algotradelib::strategy::RiskLimits::permitted_universe_ |
| std::chrono::seconds algotradelib::strategy::RiskLimits::rate_window_ {60} |
The window max_orders_per_window_ counts over.
| std::optional<Money> algotradelib::strategy::RiskLimits::run_budget_ |
The run's starting cash, in one currency: buys spend it, sells return their proceeds, and the run never borrows.
The run starts flat and owns only the shares it buys. A buy must carry a positive limit price (GuardReason::RUN_BUY_NEEDS_LIMIT). The buying-power check then spends the smaller of the broker's buying power (with enforce_buying_power_) and the run's room: its cash less what its open buys reserve less this buy's estimated fee (run_fees_). A buy whose limit price times its quantity is more is refused, naming the smaller bound: GuardReason::RUN_BUDGET_EXCEEDED when the room is smaller, INSUFFICIENT_BUYING_POWER when the broker's is smaller or equal; equal passes. The sellable check bounds a sell by the smaller of the broker's sellable quantity less resting sells (with enforce_sellable_quantity_) and the run's own shares less its open sells: GuardReason::RUN_HOLDING_EXCEEDED when the run's is smaller, QUANTITY_NOT_SELLABLE when the broker's is smaller or equal. Always name the currency (Currency defaults to KRW); a buy priced in another currency is refused.
runner::Runner keeps the run's cash and shares, and refuses to construct with a budget but no run_fees_, or over an executor that sends to a broker unless the runner keeps its run in the order journal (a serialized runner::JournalingExecutor over a broker that lists working orders). Such a runner opens its run at runner::recover_runner: a new run with this amount by default, once the journal's earlier run is closed, or the journal's run resumed when stated (runner::RunOpening). It does not turn on enforce_buying_power_. See "The run budget" in running-a-strategy-live.md.
| std::optional<RunFees> algotradelib::strategy::RiskLimits::run_fees_ |
The fees a run budget estimates for each order; required with run_budget_, unread without it.