algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
time_index.hpp
1#pragma once
2
3#include <algorithm>
4#include <chrono>
5#include <cstddef>
6#include <optional>
7#include <stdexcept>
8#include <utility>
9#include <vector>
10
11namespace algotradelib {
12namespace analytics {
13
20class TimeIndex {
21public:
23 using Instant = std::chrono::system_clock::time_point;
24
26 TimeIndex() = default;
27
34 explicit TimeIndex(std::vector<Instant> stamps) : stamps_(std::move(stamps)) {
35 for (std::size_t position = 1; position < stamps_.size(); ++position) {
36 if (!(stamps_[position - 1] < stamps_[position])) {
37 throw std::invalid_argument("time index must be strictly ascending");
38 }
39 }
40 }
41
46 std::size_t size() const noexcept {
47 return stamps_.size();
48 }
49
54 bool empty() const noexcept {
55 return stamps_.empty();
56 }
57
65 const Instant& at(std::size_t position) const {
66 if (position >= stamps_.size()) {
67 throw std::out_of_range("time index position is past the end");
68 }
69 return stamps_[position];
70 }
71
76 const std::vector<Instant>& stamps() const noexcept {
77 return stamps_;
78 }
79
86 std::optional<std::size_t> find(const Instant& when) const {
87 const auto found = std::lower_bound(stamps_.begin(), stamps_.end(), when);
88 if (found == stamps_.end() || *found != when) {
89 return std::nullopt;
90 }
91 return static_cast<std::size_t>(found - stamps_.begin());
92 }
93
103 std::optional<std::size_t> asof(const Instant& when) const {
104 const auto above = std::upper_bound(stamps_.begin(), stamps_.end(), when);
105 if (above == stamps_.begin()) {
106 return std::nullopt;
107 }
108 return static_cast<std::size_t>((above - 1) - stamps_.begin());
109 }
110
117 friend bool operator==(const TimeIndex& lhs, const TimeIndex& rhs) {
118 return lhs.stamps_ == rhs.stamps_;
119 }
120
127 friend bool operator!=(const TimeIndex& lhs, const TimeIndex& rhs) {
128 return !(lhs == rhs);
129 }
130
131private:
132 std::vector<Instant> stamps_;
133};
134
135} // namespace analytics
136} // namespace algotradelib
TimeIndex()=default
Constructs an empty index.
std::optional< std::size_t > find(const Instant &when) const
Finds the row of an exact instant.
friend bool operator==(const TimeIndex &lhs, const TimeIndex &rhs)
Compares two indices instant by instant.
std::chrono::system_clock::time_point Instant
The instant type of the index.
friend bool operator!=(const TimeIndex &lhs, const TimeIndex &rhs)
Compares two indices for inequality.
std::size_t size() const noexcept
Returns the number of instants.
std::optional< std::size_t > asof(const Instant &when) const
Finds the latest row at or before an instant ("as of" lookup).
const std::vector< Instant > & stamps() const noexcept
Returns all instants.
bool empty() const noexcept
Tests whether the index is empty.
TimeIndex(std::vector< Instant > stamps)
Constructs an index over a sequence of instants.
const Instant & at(std::size_t position) const
Returns the instant of one row.
Series buffers and frames, the conversions to and from exact decimals, and performance metrics.
The library: broker-neutral types (core), and the modules below.