algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
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
7
namespace
algotradelib
{
8
namespace
analytics
{
9
18
27
inline
math::Vector
returns
(
math::SeriesView
prices) {
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
53
inline
math::Vector
log_returns
(
math::SeriesView
prices) {
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
76
inline
math::Vector
cumulative_returns
(
math::SeriesView
period_returns) {
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
algotradelib::analytics
Returns, rolling statistics, indicators and performance metrics over numeric series.
Definition
conversion.hpp:14
algotradelib::analytics::cumulative_returns
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
algotradelib::analytics::observed
bool observed(Scalar value)
Tests whether a value is an observation; see math::observed.
Definition
scalar.hpp:36
algotradelib::analytics::log_returns
math::Vector log_returns(math::SeriesView prices)
Computes log (continuously compounded) returns, log(p[i] / p[i - 1]).
Definition
returns.hpp:53
algotradelib::analytics::missing
Scalar missing()
Returns the value that stands for "no observation"; see math::missing.
Definition
scalar.hpp:28
algotradelib::analytics::returns
math::Vector returns(math::SeriesView prices)
Computes simple period returns, p[i] / p[i - 1] - 1.
Definition
returns.hpp:27
algotradelib::math::Scalar
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition
scalar.hpp:18
algotradelib::math::Index
Eigen::Index Index
The signed index type of rows and columns.
Definition
eigen.hpp:65
algotradelib::math::unobserved
Vector unobserved(std::size_t count)
Makes a vector filled with missing().
Definition
eigen.hpp:73
algotradelib::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Definition
eigen.hpp:48
algotradelib::math::SeriesView
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
Definition
eigen.hpp:56
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
numeric.hpp
Re-exports math::Scalar, math::missing and math::observed into analytics.
algotradelib
src
analytics
returns.hpp
Generated by
1.18.0