algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
series_buffer.hpp
1#pragma once
2
3#include "./conversion.hpp"
4#include "./frame.hpp"
5#include "./time_index.hpp"
6
7#include "../math/matrix.hpp"
8
9#include <chrono>
10#include <cstddef>
11#include <stdexcept>
12#include <vector>
13
14namespace algotradelib {
15namespace analytics {
16
30public:
33
41 SeriesBuffer(std::size_t capacity, std::size_t fields = bar_field_count) : capacity_(capacity), fields_(fields) {
42 if (capacity == 0 || fields == 0) {
43 throw std::invalid_argument("a series buffer needs a capacity and at least one field");
44 }
45 // Twice the capacity bounds compaction to one row move per push, amortized.
46 values_ = math::unobserved(capacity * 2, fields);
47 stamps_.resize(capacity * 2);
48 }
49
54 std::size_t capacity() const noexcept {
55 return capacity_;
56 }
57
62 std::size_t fields() const noexcept {
63 return fields_;
64 }
65
70 std::size_t size() const noexcept {
71 return count_;
72 }
73
78 bool empty() const noexcept {
79 return count_ == 0;
80 }
81
86 bool full() const noexcept {
87 return count_ == capacity_;
88 }
89
94 std::size_t compactions() const noexcept {
95 return compactions_;
96 }
97
106 void push(Instant when, math::SeriesView row) {
107 if (static_cast<std::size_t>(row.size()) != fields_) {
108 throw std::invalid_argument("a series buffer observation must carry every field");
109 }
110 if (count_ > 0 && !(newest_instant() < when)) {
111 throw std::invalid_argument("a series buffer observation must be later than the last");
112 }
113
114 if (count_ == capacity_) {
115 ++begin_;
116 --count_;
117 }
118 if (begin_ + count_ == static_cast<std::size_t>(values_.rows())) {
119 compact();
120 }
121
122 const auto row_index = static_cast<math::Index>(begin_ + count_);
123 for (std::size_t field = 0; field < fields_; ++field) {
124 values_(row_index, static_cast<math::Index>(field)) = row(static_cast<math::Index>(field));
125 }
126 stamps_[begin_ + count_] = when;
127 ++count_;
128 }
129
138 void push(Instant when, const std::vector<Scalar>& row) {
139 if (row.size() != fields_) {
140 throw std::invalid_argument("a series buffer observation must carry every field");
141 }
142 const math::Vector values = math::Vector::Map(row.data(), static_cast<math::Index>(row.size()));
143 push(when, values);
144 }
145
156 void push(const Bar& bar) {
157 if (fields_ != bar_field_count) {
158 throw std::invalid_argument("pushing a bar needs a buffer over every bar field");
159 }
160 // On the stack: a replay pushes one row for every bar it feeds.
161 Eigen::Matrix<math::Scalar, static_cast<int>(bar_field_count), 1> row;
162 row.setConstant(math::missing());
163 if (bar.open_) {
164 row(static_cast<math::Index>(bar_field_index(BarField::OPEN))) = to_scalar(*bar.open_);
165 }
166 row(static_cast<math::Index>(bar_field_index(BarField::HIGH))) = to_scalar(bar.high_);
167 row(static_cast<math::Index>(bar_field_index(BarField::LOW))) = to_scalar(bar.low_);
168 row(static_cast<math::Index>(bar_field_index(BarField::CLOSE))) = to_scalar(bar.close_);
169 row(static_cast<math::Index>(bar_field_index(BarField::VOLUME))) = to_scalar(bar.volume_);
170 if (bar.traded_value_) {
171 row(static_cast<math::Index>(bar_field_index(BarField::TRADED_VALUE))) = to_scalar(*bar.traded_value_);
172 }
173 push(bar.timestamp_, row);
174 }
175
184 math::SeriesView column(std::size_t field) const {
185 if (field >= fields_) {
186 throw std::out_of_range("series buffer field is past the end");
187 }
188 return values_.col(static_cast<math::Index>(field))
189 .segment(static_cast<math::Index>(begin_), static_cast<math::Index>(count_));
190 }
191
200 return column(bar_field_index(field));
201 }
202
211 Scalar at(std::size_t position, std::size_t field) const {
212 if (position >= count_ || field >= fields_) {
213 throw std::out_of_range("series buffer position is past the end");
214 }
215 return values_(static_cast<math::Index>(begin_ + position), static_cast<math::Index>(field));
216 }
217
225 Instant instant(std::size_t position) const {
226 if (position >= count_) {
227 throw std::out_of_range("series buffer position is past the end");
228 }
229 return stamps_[begin_ + position];
230 }
231
238 if (count_ == 0) {
239 throw std::out_of_range("an empty series buffer has no newest observation");
240 }
241 return stamps_[begin_ + count_ - 1];
242 }
243
245 void clear() noexcept {
246 begin_ = 0;
247 count_ = 0;
248 }
249
250private:
251 // Moves the held observations to the front. Source and destination never overlap: this runs
252 // only when the held run reaches the end of storage, so begin_ = 2 * capacity - count_ >= count_.
253 void compact() {
254 const auto rows = static_cast<math::Index>(count_);
255 const auto from = static_cast<math::Index>(begin_);
256 values_.topRows(rows) = values_.middleRows(from, rows);
257 for (std::size_t position = 0; position < count_; ++position) {
258 stamps_[position] = stamps_[begin_ + position];
259 }
260 begin_ = 0;
261 ++compactions_;
262 }
263
264 std::size_t capacity_{0};
265 std::size_t fields_{0};
266 std::size_t begin_{0};
267 std::size_t count_{0};
268 std::size_t compactions_{0};
269 math::Matrix values_;
270 std::vector<Instant> stamps_;
271};
272
273} // namespace analytics
274} // namespace algotradelib
bool full() const noexcept
Tests whether the buffer holds capacity() observations.
Instant instant(std::size_t position) const
Returns the instant of one observation.
std::size_t size() const noexcept
Returns the number of observations held.
std::size_t compactions() const noexcept
Returns how many times the buffer moved its contents to the front of its storage.
math::SeriesView column(std::size_t field) const
Returns one field's values, oldest first.
void push(Instant when, const std::vector< Scalar > &row)
Appends one observation, dropping the oldest if the buffer is full.
std::size_t fields() const noexcept
Returns the number of values per observation.
math::SeriesView column(BarField field) const
Returns one bar field's values, oldest first.
bool empty() const noexcept
Tests whether the buffer is empty.
void push(Instant when, math::SeriesView row)
Appends one observation, dropping the oldest if the buffer is full.
SeriesBuffer(std::size_t capacity, std::size_t fields=bar_field_count)
Constructs an empty buffer.
void clear() noexcept
Discards every observation; the storage is kept.
TimeIndex::Instant Instant
The instant type observations are stamped with.
std::size_t capacity() const noexcept
Returns the most observations the buffer holds.
void push(const Bar &bar)
Appends one bar, dropping the oldest observation if the buffer is full.
Scalar at(std::size_t position, std::size_t field) const
Reads one value.
Instant newest_instant() const
Returns the instant of the newest observation.
std::chrono::system_clock::time_point Instant
The instant type of the index.
Conversions between exact Decimal64 values and the analytics Scalar.
The linear algebra types of the math module: Matrix, Vector, SeriesView, MutableSeriesView,...
Series buffers and frames, the conversions to and from exact decimals, and performance metrics.
BarField
A bar field, used as a column of a SeriesFrame.
Definition frame.hpp:21
constexpr std::size_t bar_field_count
The number of BarField values, and so of columns in a SeriesFrame.
Definition frame.hpp:41
constexpr std::size_t bar_field_index(BarField field) noexcept
Returns the column position of a field in a SeriesFrame.
Definition frame.hpp:48
Scalar to_scalar(const Decimal64 &value)
Converts an exact decimal to the nearest Scalar.
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition scalar.hpp:18
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
A dense, column-major matrix of Scalar.
Definition eigen.hpp:27
Eigen::Index Index
The signed index type of rows and columns.
Definition eigen.hpp:47
Vector unobserved(std::size_t count)
Makes a vector filled with missing().
Definition eigen.hpp:55
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
A dense column vector of Scalar.
Definition eigen.hpp:30
Scalar missing()
Returns the value that stands for "no observation" (a quiet NaN).
Definition scalar.hpp:28
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
Definition eigen.hpp:38
The library: broker-neutral types (core), and the modules below.
One bar: the prices and volume of one period, as exact decimals.