algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
returns.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "./numeric.hpp"
4
5#include <cstddef>
6
7namespace algotradelib {
8namespace analytics {
9
18
28 const auto rows = prices.size();
29 math::Vector result = math::unobserved(static_cast<std::size_t>(rows));
30 if (rows < 2) {
31 return result;
32 }
33 // One vectorized division over the whole series, then the guards. An unobserved price on either
34 // side makes the quotient NaN by itself, which is already the answer; a zero prior price would
35 // make it an infinity and a negative one reverses its sign, so those rows are replaced explicitly.
36 const auto current = prices.tail(rows - 1).array();
37 const auto previous = prices.head(rows - 1).array();
38 result.tail(rows - 1) =
39 (previous <= Scalar{0}).select(missing(), current / previous - Scalar{1});
40 return result;
41}
42
54 const auto rows = prices.size();
55 math::Vector result = math::unobserved(static_cast<std::size_t>(rows));
56 if (rows < 2) {
57 return result;
58 }
59 const auto current = prices.tail(rows - 1).array();
60 const auto previous = prices.head(rows - 1).array();
61 // A non-positive price on either side is refused before the logarithm rather than left to
62 // produce a bare NaN with nothing to say which row or which side caused it.
63 const auto usable = (previous > Scalar{0}) && (current > Scalar{0});
64 result.tail(rows - 1) = usable.select((current / previous).log(), missing());
65 return result;
66}
67
77 math::Vector result = math::unobserved(static_cast<std::size_t>(period_returns.size()));
78 Scalar growth{1};
79 bool started = false;
80 for (math::Index row = 0; row < period_returns.size(); ++row) {
81 const Scalar value = period_returns[row];
82 if (observed(value)) {
83 growth *= Scalar{1} + value;
84 started = true;
85 }
86 if (started) {
87 result[row] = growth - Scalar{1};
88 }
89 }
90 return result;
91}
92
93} // namespace analytics
94} // namespace algotradelib
Returns, rolling statistics, indicators and performance metrics over numeric series.
math::Vector cumulative_returns(math::SeriesView period_returns)
Compounds simple period returns into the cumulative return, prod(1 + r[0..i]) - 1.
Definition returns.hpp:76
bool observed(Scalar value)
Tests whether a value is an observation; see math::observed.
Definition scalar.hpp:36
math::Vector log_returns(math::SeriesView prices)
Computes log (continuously compounded) returns, log(p[i] / p[i - 1]).
Definition returns.hpp:53
Scalar missing()
Returns the value that stands for "no observation"; see math::missing.
Definition scalar.hpp:28
math::Vector returns(math::SeriesView prices)
Computes simple period returns, p[i] / p[i - 1] - 1.
Definition returns.hpp:27
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition scalar.hpp:18
Eigen::Index Index
The signed index type of rows and columns.
Definition eigen.hpp:65
Vector unobserved(std::size_t count)
Makes a vector filled with missing().
Definition eigen.hpp:73
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Definition eigen.hpp:48
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
Definition eigen.hpp:56
The library: broker-neutral types (core), and the modules below.
Re-exports math::Scalar, math::missing and math::observed into analytics.