3#include <algotradelib/analytics>
53 if (line.size() < 2) {
56 const Crossing pair{line[line.size() - 2], line[line.size() - 1]};
84 throw std::invalid_argument(
"an ema smoothing factor must be in (0, 1]");
86 if (min_observed == 0) {
87 throw std::invalid_argument(
"ema must require at least one observation");
93 std::size_t count = 0;
94 for (
math::Index row = 0; row < values.size(); ++row) {
101 mean = count == 0 || keep ==
math::Scalar{0} ? value : mean + (value - mean) / total;
103 if (count >= min_observed) {
124 std::size_t observations = 0;
125 for (
math::Index row = 0; row < closes.size(); ++row) {
128 if (observations < 3 * span) {
146 const auto needed =
static_cast<math::Index>(count) + 1;
147 if (values.size() < needed) {
150 return values.tail(needed);
Scalar alpha_from_span(std::size_t span)
Converts an EMA span to its smoothing factor, 2 / (span + 1).
math::Vector ema(math::SeriesView values, Scalar alpha, std::size_t min_observed=1)
Computes the exponentially weighted moving average of a series.
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.
Eigen::Index Index
The signed index type of rows and columns.
Vector unobserved(std::size_t count)
Makes a vector filled with missing().
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,...
std::optional< math::SeriesView > trailing(math::SeriesView values, std::size_t count)
Returns the last count + 1 rows of a series.
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.
math::Vector adjusted_ema(math::SeriesView values, math::Scalar alpha, std::size_t min_observed=1)
Computes the bias-adjusted exponential moving average.
What a strategy sees and emits: events, intents, the portfolio, reconciliation and risk limits.
The library: broker-neutral types (core), and the modules below.
The last two values of an indicator line, for testing a crossing.
math::Scalar previous_
The value on the previous bar.
math::Scalar current_
The value on the latest bar.
bool rose_above(const Crossing &other) const
Tests whether this line crossed above another on the latest bar.
bool fell_below(const Crossing &other) const
Tests whether this line crossed below another on the latest bar.