5#include "./time_index.hpp"
43 throw std::invalid_argument(
"a series buffer needs a capacity and at least one field");
62 std::size_t
fields() const noexcept {
70 std::size_t
size() const noexcept {
86 bool full() const noexcept {
87 return count_ == capacity_;
107 if (
static_cast<std::size_t
>(row.size()) != fields_) {
108 throw std::invalid_argument(
"a series buffer observation must carry every field");
111 throw std::invalid_argument(
"a series buffer observation must be later than the last");
114 if (count_ == capacity_) {
118 if (begin_ + count_ ==
static_cast<std::size_t
>(values_.rows())) {
122 const auto row_index =
static_cast<math::Index>(begin_ + count_);
123 for (std::size_t field = 0; field < fields_; ++field) {
126 stamps_[begin_ + count_] = when;
139 if (row.size() != fields_) {
140 throw std::invalid_argument(
"a series buffer observation must carry every field");
158 throw std::invalid_argument(
"pushing a bar needs a buffer over every bar field");
170 if (bar.traded_value_) {
173 push(bar.timestamp_, row);
185 if (field >= fields_) {
186 throw std::out_of_range(
"series buffer field is past the end");
188 return values_.col(
static_cast<math::Index>(field))
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");
226 if (position >= count_) {
227 throw std::out_of_range(
"series buffer position is past the end");
229 return stamps_[begin_ + position];
239 throw std::out_of_range(
"an empty series buffer has no newest observation");
241 return stamps_[begin_ + count_ - 1];
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];
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};
270 std::vector<Instant> stamps_;
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.
constexpr std::size_t bar_field_count
The number of BarField values, and so of columns in a SeriesFrame.
constexpr std::size_t bar_field_index(BarField field) noexcept
Returns the column position of a field in a SeriesFrame.
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).
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
A dense, column-major matrix of Scalar.
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.
Scalar missing()
Returns the value that stands for "no observation" (a quiet NaN).
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
The library: broker-neutral types (core), and the modules below.
One bar: the prices and volume of one period, as exact decimals.