algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
frame.hpp
1#pragma once
2
3#include "./conversion.hpp"
4#include "./time_index.hpp"
5
6#include "../math/matrix.hpp"
7
8#include <algotradelib/core>
9
10#include <array>
11#include <cstddef>
12#include <stdexcept>
13#include <string_view>
14#include <utility>
15#include <vector>
16
17namespace algotradelib {
18namespace analytics {
19
21enum class BarField {
22 OPEN,
23 HIGH,
24 LOW,
25 CLOSE,
26 VOLUME,
27 TRADED_VALUE,
28};
29
31inline constexpr std::array<BarField, 6> all_bar_fields{
32 BarField::OPEN,
33 BarField::HIGH,
34 BarField::LOW,
35 BarField::CLOSE,
36 BarField::VOLUME,
37 BarField::TRADED_VALUE,
38};
39
41inline constexpr std::size_t bar_field_count = all_bar_fields.size();
42
48inline constexpr std::size_t bar_field_index(BarField field) noexcept {
49 return static_cast<std::size_t>(field);
50}
51
57inline constexpr std::string_view bar_field_name(BarField field) noexcept {
58 switch (field) {
59 case BarField::OPEN:
60 return "open";
61 case BarField::HIGH:
62 return "high";
63 case BarField::LOW:
64 return "low";
65 case BarField::CLOSE:
66 return "close";
67 case BarField::VOLUME:
68 return "volume";
69 case BarField::TRADED_VALUE:
70 return "traded_value";
71 }
72 return "";
73}
74
75static_assert(
78 [](BarField field) {
79 return !bar_field_name(field).empty();
80 }
81 ),
82 "all_bar_fields must list every BarField"
83);
84
95public:
97 SeriesFrame() = default;
98
106 : instrument_(std::move(instrument)), index_(std::move(index)) {
107 values_ = math::Matrix::Constant(
108 static_cast<math::Index>(index_.size()), static_cast<math::Index>(bar_field_count), missing()
109 );
110 }
111
116 std::size_t rows() const noexcept {
117 return index_.size();
118 }
119
124 std::size_t columns() const noexcept {
125 return bar_field_count;
126 }
127
132 const TimeIndex& time() const noexcept {
133 return index_;
134 }
135
140 const InstrumentId& instrument() const noexcept {
141 return instrument_;
142 }
143
150 return values_.col(static_cast<math::Index>(bar_field_index(field)));
151 }
152
158 return column(BarField::CLOSE);
159 }
160
166 return column(BarField::OPEN);
167 }
168
174 return column(BarField::HIGH);
175 }
176
182 return column(BarField::LOW);
183 }
184
190 return column(BarField::VOLUME);
191 }
192
201 Scalar at(std::size_t row, BarField field) const {
202 if (row >= rows()) {
203 throw std::out_of_range("frame row is past the end");
204 }
205 return values_(static_cast<math::Index>(row), static_cast<math::Index>(bar_field_index(field)));
206 }
207
216 void set(std::size_t row, BarField field, Scalar value) {
217 if (row >= rows()) {
218 throw std::out_of_range("frame row is past the end");
219 }
220 values_(static_cast<math::Index>(row), static_cast<math::Index>(bar_field_index(field))) = value;
221 }
222
228 return values_;
229 }
230
231private:
232 InstrumentId instrument_;
233 TimeIndex index_;
234 math::Matrix values_;
235};
236
247inline SeriesFrame from_bars(const BarSeries& series) {
248 std::vector<TimeIndex::Instant> stamps;
249 stamps.reserve(series.bars_.size());
250 for (const auto& bar : series.bars_) {
251 stamps.push_back(bar.timestamp_);
252 }
253
254 SeriesFrame frame(TimeIndex(std::move(stamps)), series.instrument_);
255 for (std::size_t row = 0; row < series.bars_.size(); ++row) {
256 const auto& bar = series.bars_[row];
257 // An absent open is a trading day on which nothing traded. It stays unobserved
258 // rather than becoming zero or the previous close.
259 if (bar.open_) {
260 frame.set(row, BarField::OPEN, to_scalar(*bar.open_));
261 }
262 frame.set(row, BarField::HIGH, to_scalar(bar.high_));
263 frame.set(row, BarField::LOW, to_scalar(bar.low_));
264 frame.set(row, BarField::CLOSE, to_scalar(bar.close_));
265 frame.set(row, BarField::VOLUME, to_scalar(bar.volume_));
266 if (bar.traded_value_) {
267 frame.set(row, BarField::TRADED_VALUE, to_scalar(*bar.traded_value_));
268 }
269 }
270 return frame;
271}
272
273} // namespace analytics
274} // namespace algotradelib
One instrument's bars as numeric columns: one row per bar, one column per BarField.
Definition frame.hpp:94
const TimeIndex & time() const noexcept
Returns the instants of the rows.
Definition frame.hpp:132
std::size_t columns() const noexcept
Returns the number of columns.
Definition frame.hpp:124
const InstrumentId & instrument() const noexcept
Returns the instrument the frame describes.
Definition frame.hpp:140
math::SeriesView close() const
Returns the close column.
Definition frame.hpp:157
math::SeriesView volume() const
Returns the volume column.
Definition frame.hpp:189
SeriesFrame()=default
Constructs an empty frame.
Scalar at(std::size_t row, BarField field) const
Reads one value.
Definition frame.hpp:201
void set(std::size_t row, BarField field, Scalar value)
Writes one value.
Definition frame.hpp:216
math::SeriesView open() const
Returns the open column.
Definition frame.hpp:165
std::size_t rows() const noexcept
Returns the number of rows.
Definition frame.hpp:116
math::SeriesView high() const
Returns the high column.
Definition frame.hpp:173
math::MatrixView values() const
Returns all values.
Definition frame.hpp:227
SeriesFrame(TimeIndex index, InstrumentId instrument={})
Constructs a frame with one row per instant, every value missing().
Definition frame.hpp:105
math::SeriesView low() const
Returns the low column.
Definition frame.hpp:181
math::SeriesView column(BarField field) const
Returns one field's column.
Definition frame.hpp:149
The instants a frame's rows correspond to, strictly ascending.
TimeIndex()=default
Constructs an empty index.
std::size_t size() const noexcept
Returns the number of instants.
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.
constexpr std::array< BarField, 6 > all_bar_fields
Every BarField, in declaration order.
Definition frame.hpp:31
BarField
A bar field, used as a column of a SeriesFrame.
Definition frame.hpp:21
constexpr std::string_view bar_field_name(BarField field) noexcept
Returns a lowercase display name for a field, such as "close".
Definition frame.hpp:57
constexpr std::size_t bar_field_count
The number of BarField values, and so of columns in a SeriesFrame.
Definition frame.hpp:41
Scalar missing()
Returns the value that stands for "no observation"; see math::missing.
Definition scalar.hpp:28
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.
SeriesFrame from_bars(const BarSeries &series)
Converts a bar series into a frame of Scalar columns.
Definition frame.hpp:247
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
Eigen::Ref< const Matrix > MatrixView
A read-only reference to a whole matrix.
Definition eigen.hpp:44
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.
constexpr bool lists_every_value(const std::array< Enum, N > &values, IsEnumerator is_enumerator) noexcept
Tests whether a list holds every value of an enum exactly once, in any order.
The bars of one instrument and interval, oldest first.
InstrumentId instrument_
The instrument.
std::vector< Bar > bars_
The bars, oldest first.
Identifies an instrument: its country, venue and symbol.
Definition types.hpp:85