8#include <algotradelib/analytics>
9#include <algotradelib/core>
39 std::string
tag_{
"zscore-reversion"};
68 const auto* series = ctx.bars(bar.instrument_, bar.interval_);
69 if (series ==
nullptr) {
72 const auto closes = series->column(analytics::BarField::CLOSE);
78 const auto score = scores[scores.size() - 1];
91 const bool holding = held->coefficient() != 0;
136 std::string
tag_{
"rsi-threshold"};
162 const auto* series = ctx.bars(bar.instrument_, bar.interval_);
163 if (series ==
nullptr) {
166 const auto closes = series->column(analytics::BarField::CLOSE);
167 if (
static_cast<std::size_t
>(closes.size()) <
warmup_bars()) {
172 const auto level = index[index.size() - 1];
183 const bool holding = held->coefficient() != 0;
216 std::string
tag_{
"donchian-breakout"};
242 const auto* series = ctx.bars(bar.instrument_, bar.interval_);
243 if (series ==
nullptr) {
246 const auto closes = series->column(analytics::BarField::CLOSE);
253 const auto prior = window->head(window->size() - 1);
256 const auto ceiling = highest[highest.size() - 1];
257 const auto floor = lowest[lowest.size() - 1];
258 const auto close = (*window)[window->size() - 1];
266 if (ceiling < close) {
268 }
else if (close < floor) {
An exact signed decimal: an int64 coefficient times 10^-scale.
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
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_min(math::SeriesView values, std::size_t window, std::size_t min_observed)
Computes the trailing minimum of the observed values.
math::Vector rsi(math::SeriesView prices, std::size_t period=14)
Computes Wilder's relative strength index.
math::Vector zscore(math::SeriesView values, std::size_t window, std::size_t min_observed)
Computes how many trailing sample standard deviations each value lies from its trailing mean.
math::Vector rolling_max(math::SeriesView values, std::size_t window, std::size_t min_observed)
Computes the trailing maximum of the observed values.
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.
The library: broker-neutral types (core), and the modules below.
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::size_t lookback_
The number of prior closes the channel spans; zero disables the strategy.
std::string tag_
The tag on the targets.
InstrumentId instrument_
The instrument to trade; bars for any other instrument are ignored.
Decimal64 quantity_
The shares to hold while in a position.
Donchian channel breakout: holds after the close breaks above the channel, exits after it breaks belo...
Params params_
The settings.
void on_bar(Ctx &ctx, const BarEvent &bar)
States a target position for the instrument when its signal enters or exits.
std::size_t warmup_bars() const
Returns how many bars to load before the first live bar.
Decimal64 quantity_
The shares to hold while in a position.
math::Scalar overbought_
Sell at or above this level; must exceed oversold_, or nothing is traded.
std::string tag_
The tag on the targets.
std::size_t period_
The RSI period; zero disables the strategy.
math::Scalar oversold_
Buy at or below this level.
InstrumentId instrument_
The instrument to trade; bars for any other instrument are ignored.
Long-only RSI strategy: buys when the index is oversold, sells when it is overbought.
void on_bar(Ctx &ctx, const BarEvent &bar)
States a target position for the instrument when its signal enters or exits.
std::size_t warmup_bars() const
Returns how many bars to load before the first live bar.
Params params_
The settings.
InstrumentId instrument_
The instrument to trade; bars for any other instrument are ignored.
std::size_t window_
The number of closes the z-score is measured over; below 2, nothing is traded.
math::Scalar exit_
The z-score -exit_ at which to sell; positive and less than entry_, or nothing is traded.
math::Scalar entry_
How many standard deviations below the mean to buy at; positive, or nothing is traded.
std::string tag_
The tag on the targets.
Decimal64 quantity_
The shares to hold while in a position.
Long-only mean reversion on the z-score of the close.
void on_bar(Ctx &ctx, const BarEvent &bar)
States a target position for the instrument when its signal enters or exits.
Params params_
The settings.
std::size_t warmup_bars() const
Returns how many bars to load before the first live bar.