4#include "./time_index.hpp"
58 return "union_unobserved";
60 return "intersection";
62 return "forward_fill";
83 std::size_t
rows() const noexcept {
return index_.size(); }
89 std::size_t
columns() const noexcept {
return instruments_.size(); }
101 const std::vector<InstrumentId>&
instruments() const noexcept {
return instruments_; }
122 if (instruments_[
column] == instrument) {
136 if (position >= instruments_.size()) {
137 throw std::out_of_range(
"panel column is past the end");
139 return values_.col(
static_cast<math::Index>(position));
151 throw std::out_of_range(
"panel position is past the end");
172 const std::vector<BarSeries>& series,
177 panel.field_ =
field;
179 if (series.empty()) {
183 std::vector<SeriesFrame> frames;
184 frames.reserve(series.size());
185 for (
const auto& one : series) {
187 panel.instruments_.push_back(one.instrument_);
191 shared_instants(frames) :
192 combined_instants(frames);
193 panel.index_ =
TimeIndex(std::move(stamps));
195 panel.values_ = math::Matrix::Constant(
209 static std::vector<TimeIndex::Instant> combined_instants(
const std::vector<SeriesFrame>& frames) {
210 std::size_t total = 0;
211 for (
const auto& frame : frames) {
212 total += frame.time().size();
214 std::vector<TimeIndex::Instant> all;
216 for (
const auto& frame : frames) {
217 const auto& stamps = frame.time().stamps();
218 all.insert(all.end(), stamps.begin(), stamps.end());
220 std::sort(all.begin(), all.end());
221 all.erase(std::unique(all.begin(), all.end()), all.end());
226 static std::vector<TimeIndex::Instant> shared_instants(
const std::vector<SeriesFrame>& frames) {
227 std::vector<TimeIndex::Instant> shared = frames.front().time().stamps();
228 std::vector<TimeIndex::Instant> narrowed;
229 narrowed.reserve(shared.size());
230 for (std::size_t position = 1; position < frames.size(); ++position) {
231 const auto& other = frames[position].time().stamps();
233 std::set_intersection(
234 shared.begin(), shared.end(), other.begin(), other.end(), std::back_inserter(narrowed)
236 shared.swap(narrowed);
243 static void fill_column(
246 const SeriesFrame& frame,
250 const auto& target = panel.index_.stamps();
251 const auto& source = frame.time().stamps();
252 if (target.empty() || source.empty()) {
255 const auto series = frame.column(
field);
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])) {
267 if (when < source[
at]) {
270 if (!carry && source[
at] != when) {
278 std::vector<InstrumentId> instruments_;
math::SeriesView column(std::size_t position) const
Returns one instrument's column.
std::size_t columns() const noexcept
Returns the number of columns.
Scalar at(std::size_t row, std::size_t column) const
Reads one value.
Panel()=default
Constructs an empty panel.
const TimeIndex & time() const noexcept
Returns the instants of the rows.
std::optional< std::size_t > find(const InstrumentId &instrument) const
Finds an instrument's column.
static Panel from_bars(const std::vector< BarSeries > &series, BarField field, Alignment alignment)
Builds a panel of one bar field from several bar series.
math::MatrixView values() const
Returns all values.
Alignment alignment() const noexcept
Returns how the panel lined up its instruments.
const std::vector< InstrumentId > & instruments() const noexcept
Returns the instruments of the columns.
BarField field() const noexcept
Returns the bar field the panel holds.
std::size_t rows() const noexcept
Returns the number of rows.
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,...
Returns, rolling statistics, indicators and performance metrics over numeric series.
BarField
A bar field, used as a column of a SeriesFrame.
constexpr std::string_view alignment_name(Alignment alignment) noexcept
Returns a lowercase display name for an alignment, such as "intersection".
Scalar missing()
Returns the value that stands for "no observation"; see math::missing.
SeriesFrame from_bars(const BarSeries &series)
Converts a bar series into a frame of Scalar columns.
Alignment
How Panel::from_bars lines up instruments whose bars fall on different instants.
@ FORWARD_FILL
Every instant any instrument reported; a gap repeats the instrument's last value.
@ INTERSECTION
Only the instants every instrument reported.
@ UNION_UNOBSERVED
Every instant any instrument reported; an instrument with no bar there is missing().
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.
Eigen::Ref< const Matrix > MatrixView
A read-only reference to a whole matrix.
Eigen::Ref< const Vector > SeriesView
A read-only reference to one series.
The library: broker-neutral types (core), and the modules below.
Identifies an instrument: its country, venue and symbol.