8#include <algotradelib/analytics>
9#include <algotradelib/core>
40 std::string
tag_{
"volatility-target"};
66 const auto* series = ctx.bars(bar.instrument_, bar.interval_);
67 if (series ==
nullptr) {
70 const auto closes = series->column(analytics::BarField::CLOSE);
80 const auto deviation = deviations[deviations.size() - 1];
81 const auto close = (*window)[window->size() - 1];
91 if (!value_deviation || value_deviation->coefficient() == 0) {
134 std::string
tag_{
"equal-weight"};
161 if (!share || share->coefficient() == 0) {
An exact signed decimal: an int64 coefficient times 10^-scale.
Decimal64()=default
Constructs zero.
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
static OrderIntent market(InstrumentId instrument, OrderSide side, SizeSpec size, TimeInForce time_in_force=TimeInForce::DAY)
Makes a market order, which carries no price.
static SizeSpec target_weight(Decimal64 fraction)
Makes a size as a fraction of portfolio value to hold.
The events a runner delivers to a strategy's handlers, and the market phase.
What a strategy asks the runner for: orders (OrderIntent), target positions (TargetPosition) and canc...
math::Vector rolling_stdev(math::SeriesView values, std::size_t window, std::size_t min_observed)
Computes the trailing sample standard deviation (divisor count - 1).
math::Vector returns(math::SeriesView prices)
Computes simple period returns, p[i] / p[i - 1] - 1.
std::optional< Decimal64 > to_decimal(Scalar value, std::uint8_t scale)
Converts a Scalar to an exact decimal with a given number of decimal places.
double Scalar
The floating-point type the analytics and math layers compute in (double).
bool observed(Scalar value)
Tests whether a value is an observation.
The strategies this library ships (buy and hold, crossovers, mean reversion, sizing,...
std::optional< math::SeriesView > trailing(math::SeriesView values, std::size_t count)
Returns the last count + 1 rows of a series.
What a strategy sees and emits: events, intents, the portfolio, reconciliation and risk limits.
MarketPhase
Where the clock stands relative to the trading session.
@ OPEN
Inside the session.
The library: broker-neutral types (core), and the modules below.
std::optional< Decimal64 > decimal_divide(const Decimal64 &lhs, const Decimal64 &rhs, std::uint8_t scale)
Divides two decimals, truncating toward zero at a given scale.
bool decimal_less(const Decimal64 &lhs, const Decimal64 &rhs)
Compares two decimals numerically, whatever their scales.
Helpers for the vanilla strategies that compute indicators.
Identifies an instrument: its country, venue and symbol.
One bar, delivered to on_bar.
The position a strategy wants to hold in one instrument.
InstrumentId instrument_
The instrument.
std::string tag_
A label carried to the orders and their fills.
Decimal64 target_quantity_
The position to hold, signed: negative is short, and zero means flat.
std::vector< InstrumentId > universe_
The instruments to hold; an empty universe does nothing.
std::uint8_t weight_scale_
Decimal places of each weight; 4 is a hundredth of a percent.
MarketPhase rebalance_at_
The phase whose start triggers a rebalance.
std::string tag_
The tag on the orders.
Holds an equal fraction of portfolio value in each instrument of a universe.
void on_phase(Ctx &ctx, MarketPhase phase)
Submits an equal-weight buy intent for every instrument when phase is rebalance_at_.
static constexpr std::size_t warmup_bars
No history is needed before the first live bar.
Params params_
The settings.
Decimal64 risk_budget_
The value volatility per bar the position should have, in the instrument's currency.
Decimal64 max_quantity_
The largest position to hold, whatever the arithmetic gives.
std::string tag_
The tag on the targets.
InstrumentId instrument_
The instrument to hold; bars for any other instrument are ignored.
std::size_t window_
The number of returns the volatility is measured over; below 2, nothing is traded.
Holds a long position sized so its value volatility per bar matches a risk budget.
std::size_t warmup_bars() const
Returns how many bars to load before the first live bar.
void on_bar(Ctx &ctx, const BarEvent &bar)
States the volatility-sized target position on each bar of the instrument.
Params params_
The settings.