5#include "./time_index.hpp"
45 throw std::invalid_argument(
"a series buffer needs a capacity and at least one field");
56 std::size_t
capacity() const noexcept {
return capacity_; }
62 std::size_t
fields() const noexcept {
return fields_; }
68 std::size_t
size() const noexcept {
return count_; }
74 bool empty() const noexcept {
return count_ == 0; }
80 bool full() const noexcept {
return count_ == capacity_; }
86 std::size_t
compactions() const noexcept {
return compactions_; }
97 if (
static_cast<std::size_t
>(row.size()) != fields_) {
98 throw std::invalid_argument(
"a series buffer observation must carry every field");
101 throw std::invalid_argument(
"a series buffer observation must be later than the last");
104 if (count_ == capacity_) {
108 if (begin_ + count_ ==
static_cast<std::size_t
>(values_.rows())) {
112 const auto row_index =
static_cast<math::Index>(begin_ + count_);
113 for (std::size_t field = 0; field < fields_; ++field) {
114 values_(row_index,
static_cast<math::Index>(field)) =
117 stamps_[begin_ + count_] = when;
130 if (row.size() != fields_) {
131 throw std::invalid_argument(
"a series buffer observation must carry every field");
149 throw std::invalid_argument(
"pushing a bar needs a buffer over every bar field");
159 if (bar.traded_value_) {
163 push(bar.timestamp_, row);
175 if (field >= fields_) {
176 throw std::out_of_range(
"series buffer field is past the end");
178 return values_.col(
static_cast<math::Index>(field))
199 Scalar at(std::size_t position, std::size_t field)
const {
200 if (position >= count_ || field >= fields_) {
201 throw std::out_of_range(
"series buffer position is past the end");
216 if (position >= count_) {
217 throw std::out_of_range(
"series buffer position is past the end");
219 return stamps_[begin_ + position];
229 throw std::out_of_range(
"an empty series buffer has no newest observation");
231 return stamps_[begin_ + count_ - 1];
244 const auto rows =
static_cast<math::Index>(count_);
245 const auto from =
static_cast<math::Index>(begin_);
246 values_.topRows(rows) = values_.middleRows(from, rows);
247 for (std::size_t position = 0; position < count_; ++position) {
248 stamps_[position] = stamps_[begin_ + position];
254 std::size_t capacity_{0};
255 std::size_t fields_{0};
256 std::size_t begin_{0};
257 std::size_t count_{0};
258 std::size_t compactions_{0};
260 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,...
Returns, rolling statistics, indicators and performance metrics over numeric series.
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.
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.