algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
panel.hpp
1#pragma once
2
3#include "./frame.hpp"
4#include "./time_index.hpp"
5
6#include "../math/matrix.hpp"
7
8#include <algorithm>
9#include <cstddef>
10#include <iterator>
11#include <optional>
12#include <stdexcept>
13#include <string_view>
14#include <utility>
15#include <vector>
16
17namespace algotradelib {
18namespace analytics {
19
49
55inline constexpr std::string_view alignment_name(Alignment alignment) noexcept {
56 switch (alignment) {
58 return "union_unobserved";
60 return "intersection";
62 return "forward_fill";
63 }
64 return "unknown";
65}
66
74class Panel {
75public:
77 Panel() = default;
78
83 std::size_t rows() const noexcept {
84 return index_.size();
85 }
86
91 std::size_t columns() const noexcept {
92 return instruments_.size();
93 }
94
99 const TimeIndex& time() const noexcept {
100 return index_;
101 }
102
107 const std::vector<InstrumentId>& instruments() const noexcept {
108 return instruments_;
109 }
110
115 BarField field() const noexcept {
116 return field_;
117 }
118
123 Alignment alignment() const noexcept {
124 return alignment_;
125 }
126
132 std::optional<std::size_t> find(const InstrumentId& instrument) const {
133 for (std::size_t column = 0; column < instruments_.size(); ++column) {
134 if (instruments_[column] == instrument) {
135 return column;
136 }
137 }
138 return std::nullopt;
139 }
140
147 math::SeriesView column(std::size_t position) const {
148 if (position >= instruments_.size()) {
149 throw std::out_of_range("panel column is past the end");
150 }
151 return values_.col(static_cast<math::Index>(position));
152 }
153
161 Scalar at(std::size_t row, std::size_t column) const {
162 if (row >= rows() || column >= columns()) {
163 throw std::out_of_range("panel position is past the end");
164 }
165 return values_(static_cast<math::Index>(row), static_cast<math::Index>(column));
166 }
167
173 return values_;
174 }
175
185 static Panel from_bars(const std::vector<BarSeries>& series, BarField field, Alignment alignment) {
186 Panel panel;
187 panel.field_ = field;
188 panel.alignment_ = alignment;
189 if (series.empty()) {
190 return panel;
191 }
192
193 std::vector<SeriesFrame> frames;
194 frames.reserve(series.size());
195 for (const auto& one : series) {
196 frames.push_back(analytics::from_bars(one));
197 panel.instruments_.push_back(one.instrument_);
198 }
199
200 auto stamps = alignment == Alignment::INTERSECTION ? shared_instants(frames) : combined_instants(frames);
201 panel.index_ = TimeIndex(std::move(stamps));
202
203 panel.values_ = math::Matrix::Constant(
204 static_cast<math::Index>(panel.index_.size()), static_cast<math::Index>(frames.size()), missing()
205 );
206
207 for (std::size_t column = 0; column < frames.size(); ++column) {
208 fill_column(panel, column, frames[column], field, alignment);
209 }
210 return panel;
211 }
212
213private:
214 // Every instant any frame reported, ascending and deduplicated.
215 static std::vector<TimeIndex::Instant> combined_instants(const std::vector<SeriesFrame>& frames) {
216 std::size_t total = 0;
217 for (const auto& frame : frames) {
218 total += frame.time().size();
219 }
220 std::vector<TimeIndex::Instant> all;
221 all.reserve(total);
222 for (const auto& frame : frames) {
223 const auto& stamps = frame.time().stamps();
224 all.insert(all.end(), stamps.begin(), stamps.end());
225 }
226 std::sort(all.begin(), all.end());
227 all.erase(std::unique(all.begin(), all.end()), all.end());
228 return all;
229 }
230
231 // Only the instants every frame reported.
232 static std::vector<TimeIndex::Instant> shared_instants(const std::vector<SeriesFrame>& frames) {
233 std::vector<TimeIndex::Instant> shared = frames.front().time().stamps();
234 std::vector<TimeIndex::Instant> narrowed;
235 narrowed.reserve(shared.size());
236 for (std::size_t position = 1; position < frames.size(); ++position) {
237 const auto& other = frames[position].time().stamps();
238 narrowed.clear();
239 std::set_intersection(shared.begin(), shared.end(), other.begin(), other.end(), std::back_inserter(narrowed));
240 shared.swap(narrowed);
241 }
242 return shared;
243 }
244
245 // Writes one instrument's column under the chosen alignment, walking both ascending indices
246 // together in one pass.
247 static void fill_column(
248 Panel& panel, std::size_t column, const SeriesFrame& frame, BarField field, Alignment alignment
249 ) {
250 const auto& target = panel.index_.stamps();
251 const auto& source = frame.time().stamps();
252 if (target.empty() || source.empty()) {
253 return;
254 }
255 const auto series = frame.column(field);
256 const bool carry = alignment == Alignment::FORWARD_FILL;
257
258 std::size_t at = 0;
259 for (std::size_t row = 0; row < target.size(); ++row) {
260 const auto& when = target[row];
261 while (at + 1 < source.size() && !(when < source[at + 1])) {
262 ++at;
263 }
264 // `source[at]` is now the latest instant at or before `when`, unless the source
265 // begins after it — in which case there is nothing observed yet, and carrying
266 // backwards would be inventing a price the instrument never had.
267 if (when < source[at]) {
268 continue;
269 }
270 if (!carry && source[at] != when) {
271 continue;
272 }
273 panel.values_(static_cast<math::Index>(row), static_cast<math::Index>(column)) =
274 series(static_cast<math::Index>(at));
275 }
276 }
277
278 std::vector<InstrumentId> instruments_;
279 TimeIndex index_;
280 math::Matrix values_;
281 BarField field_{BarField::CLOSE};
283};
284
285} // namespace analytics
286} // namespace algotradelib
math::SeriesView column(std::size_t position) const
Returns one instrument's column.
Definition panel.hpp:147
std::size_t columns() const noexcept
Returns the number of columns.
Definition panel.hpp:91
Scalar at(std::size_t row, std::size_t column) const
Reads one value.
Definition panel.hpp:161
Panel()=default
Constructs an empty panel.
const TimeIndex & time() const noexcept
Returns the instants of the rows.
Definition panel.hpp:99
std::optional< std::size_t > find(const InstrumentId &instrument) const
Finds an instrument's column.
Definition panel.hpp:132
static Panel from_bars(const std::vector< BarSeries > &series, BarField field, Alignment alignment)
Builds a panel of one bar field from several bar series.
Definition panel.hpp:185
math::MatrixView values() const
Returns all values.
Definition panel.hpp:172
Alignment alignment() const noexcept
Returns how the panel lined up its instruments.
Definition panel.hpp:123
const std::vector< InstrumentId > & instruments() const noexcept
Returns the instruments of the columns.
Definition panel.hpp:107
BarField field() const noexcept
Returns the bar field the panel holds.
Definition panel.hpp:115
std::size_t rows() const noexcept
Returns the number of rows.
Definition panel.hpp:83
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.
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::string_view alignment_name(Alignment alignment) noexcept
Returns a lowercase display name for an alignment, such as "intersection".
Definition panel.hpp:55
Scalar missing()
Returns the value that stands for "no observation"; see math::missing.
Definition scalar.hpp:28
SeriesFrame from_bars(const BarSeries &series)
Converts a bar series into a frame of Scalar columns.
Definition frame.hpp:247
Alignment
How Panel::from_bars lines up instruments whose bars fall on different instants.
Definition panel.hpp:25
@ FORWARD_FILL
Every instant any instrument reported; a gap repeats the instrument's last value.
Definition panel.hpp:47
@ INTERSECTION
Only the instants every instrument reported.
Definition panel.hpp:38
@ UNION_UNOBSERVED
Every instant any instrument reported; an instrument with no bar there is missing().
Definition panel.hpp:31
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.
Identifies an instrument: its country, venue and symbol.
Definition types.hpp:85