8#include <algotradelib/analytics>
9#include <algotradelib/core>
61 std::string
tag_{
"ma-crossover"};
88 const auto* series = ctx.bars(bar.instrument_, bar.interval_);
89 if (series ==
nullptr) {
92 const auto closes = series->column(analytics::BarField::CLOSE);
93 if (
static_cast<std::size_t
>(closes.size()) <
warmup_bars()) {
99 const auto fast_pair =
last_two(fast);
100 const auto slow_pair =
last_two(slow);
101 if (!fast_pair || !slow_pair) {
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 sma(math::SeriesView values, std::size_t window, std::size_t min_observed)
Computes the simple moving average; identical to rolling_mean.
Eigen::Index Index
The signed index type of rows and columns.
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
The strategies this library ships (buy and hold, crossovers, mean reversion, sizing,...
math::Vector exponential_line(math::SeriesView closes, std::size_t span)
Computes the exponential moving average MovingAverageCrossover reads.
std::optional< Crossing > last_two(const math::Vector &line)
Returns the last two values of a line.
AverageKind
Which moving average MovingAverageCrossover uses.
@ EXPONENTIAL
An exponential average with alpha = 2 / (span + 1), over every cached bar.
@ SIMPLE
The simple mean of the window.
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 warmup_bars() const
Returns how many bars to load before the first live bar.
void on_bar(Ctx &ctx, const BarEvent &bar)
Calls the general crossover's on_bar.
EmaCrossover(MovingAverageCrossover::Params params={})
Constructs the strategy.
MovingAverageCrossover inner_
The general crossover, with kind_ set to EXPONENTIAL.
std::size_t slow_
The slow span.
InstrumentId instrument_
The instrument to trade; bars for any other instrument are ignored.
std::string tag_
The tag on the targets.
std::size_t fast_
The fast span; must be nonzero and less than slow_, or the strategy does nothing.
Decimal64 quantity_
The shares to hold while the fast average is above the slow one.
AverageKind kind_
Which average to use; see AverageKind.
Holds quantity_ shares while the fast moving average is above the slow one, and is flat otherwise.
Params params_
The settings.
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 target position for the instrument on each of its bars.
SmaCrossover(MovingAverageCrossover::Params params={})
Constructs the strategy.
std::size_t warmup_bars() const
Returns how many bars to load before the first live bar.
MovingAverageCrossover inner_
The general crossover, with kind_ set to SIMPLE.
void on_bar(Ctx &ctx, const BarEvent &bar)
Calls the general crossover's on_bar.