algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::strategy::vanilla Namespace Reference

The strategies this library ships (buy and hold, crossovers, mean reversion, sizing, liquidation). More...

Classes

struct  BuyAndHold
 Holds a fixed quantity of one instrument. More...
struct  Crossing
 The last two values of an indicator line, for testing a crossing. More...
struct  DonchianBreakout
 Donchian channel breakout: holds after the close breaks above the channel, exits after it breaks below. More...
struct  EmaCrossover
 MovingAverageCrossover with exponential averages. More...
struct  EqualWeightRebalance
 Holds an equal fraction of portfolio value in each instrument of a universe. More...
struct  MovingAverageCrossover
 Holds quantity_ shares while the fast moving average is above the slow one, and is flat otherwise. More...
struct  RsiThreshold
 Long-only RSI strategy: buys when the index is oversold, sells when it is overbought. More...
struct  SmaCrossover
 MovingAverageCrossover with simple averages. More...
struct  TimedLiquidation
 Flattens every position when the market enters a phase it should not hold through. More...
struct  VolatilityTargetSizing
 Holds a long position sized so its value volatility per bar matches a risk budget. More...
struct  ZScoreReversion
 Long-only mean reversion on the z-score of the close. More...

Enumerations

enum class  AverageKind { SIMPLE , EXPONENTIAL }
 Which moving average MovingAverageCrossover uses. More...

Functions

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.
math::Vector exponential_line (math::SeriesView closes, std::size_t span)
 Computes the exponential moving average MovingAverageCrossover reads.
std::optional< math::SeriesView > trailing (math::SeriesView values, std::size_t count)
 Returns the last count + 1 rows of a series.

Detailed Description

The strategies this library ships (buy and hold, crossovers, mean reversion, sizing, liquidation).

Enumeration Type Documentation

◆ AverageKind

Which moving average MovingAverageCrossover uses.

  • SIMPLE reads the last slow_ + 1 closes.
  • EXPONENTIAL reads every cached close (up to the runner's series_capacity). A line with at least three spans of observed closes uses analytics::ema seeded at the oldest close; a line with fewer uses adjusted_ema, which does not overweight the first close (right for a new listing). The choice is made per line.
Note
With EXPONENTIAL, the signal can change on an unchanged close when one line switches from the adjusted to the plain average. A span above a third of series_capacity (170 at the default 512) always uses the adjusted average. A backtest, whose cache keeps growing, can differ from a live run that loaded fewer bars until the live cache catches up.
Enumerator
SIMPLE 

The simple mean of the window.

EXPONENTIAL 

An exponential average with alpha = 2 / (span + 1), over every cached bar.

Definition at line 33 of file crossover.hpp.

Function Documentation

◆ adjusted_ema()

math::Vector algotradelib::strategy::vanilla::adjusted_ema ( math::SeriesView values,
math::Scalar alpha,
std::size_t min_observed = 1 )
inline

Computes the bias-adjusted exponential moving average.

At each row, the exponentially weighted mean of the observations so far, with weights 1, q, q^2, ... (newest first, q = 1 - alpha) normalized to sum to one. On finite observed rows it equals pandas' ewm(alpha=alpha, adjust=True, ignore_na=True).mean(). Unlike analytics::ema, the first observation never weighs more than its exponential share, so use it on a short series with no history before its first row. A finite constant series gives exactly that constant.

Warning
An infinity counts as an observation: the average becomes infinite, then NaN from the next finite value on, for the rest of the series (except at alpha = 1, which keeps only the newest value).
Parameters
valuesThe series; NaN entries are skipped and their rows left missing().
alphaThe smoothing factor, in (0, 1].
min_observedHow many observations must be seen before a value is reported.
Returns
The averages, one per row.
Exceptions
std::invalid_argumentunless 0 < alpha <= 1, or if min_observed is zero.

Definition at line 82 of file signal.hpp.

Referenced by adjusted_ema(), and exponential_line().

◆ exponential_line()

math::Vector algotradelib::strategy::vanilla::exponential_line ( math::SeriesView closes,
std::size_t span )
inline

Computes the exponential moving average MovingAverageCrossover reads.

With at least 3 * span observed closes, analytics::ema seeded at the oldest; with fewer, adjusted_ema, since the seed would still weigh too much. Reported from the span-th observation.

Parameters
closesThe whole close series.
spanThe EMA span.
Returns
The line, one value per row.
Exceptions
std::invalid_argumentif span is zero.

Definition at line 122 of file signal.hpp.

Referenced by exponential_line().

◆ last_two()

std::optional< Crossing > algotradelib::strategy::vanilla::last_two ( const math::Vector & line)
inline

Returns the last two values of a line.

Parameters
lineThe indicator line.
Returns
The two values, or std::nullopt if the line has fewer than two rows or either value is unobserved (a line still warming up cannot have crossed).

Definition at line 52 of file signal.hpp.

Referenced by last_two(), and algotradelib::strategy::vanilla::MovingAverageCrossover::on_bar().

◆ trailing()

std::optional< math::SeriesView > algotradelib::strategy::vanilla::trailing ( math::SeriesView values,
std::size_t count )
inline

Returns the last count + 1 rows of a series.

Slice before computing a windowed indicator when only its latest values are needed, so the cost per bar does not grow with the cached history. Not for a recursive indicator such as an EMA, whose value depends on every earlier row.

Parameters
valuesThe series.
countThe indicator's window; one more row is kept for the previous value.
Returns
A view of the last count + 1 rows, or std::nullopt if there are fewer.

Definition at line 145 of file signal.hpp.

Referenced by algotradelib::strategy::vanilla::DonchianBreakout::on_bar(), algotradelib::strategy::vanilla::VolatilityTargetSizing::on_bar(), algotradelib::strategy::vanilla::ZScoreReversion::on_bar(), and trailing().