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 { return stamps_.size(); }
47
52 bool empty() const noexcept { return stamps_.empty(); }
53
61 const Instant& at(std::size_t position) const {
62 if (position >= stamps_.size()) {
63 throw std::out_of_range("time index position is past the end");
64 }
65 return stamps_[position];
66 }
67
72 const std::vector<Instant>& stamps() const noexcept { return stamps_; }
73
80 std::optional<std::size_t> find(const Instant& when) const {
81 const auto found = std::lower_bound(stamps_.begin(), stamps_.end(), when);
82 if (found == stamps_.end() || *found != when) {
83 return std::nullopt;
84 }
85 return static_cast<std::size_t>(found - stamps_.begin());
86 }
87
97 std::optional<std::size_t> asof(const Instant& when) const {
98 const auto above = std::upper_bound(stamps_.begin(), stamps_.end(), when);
99 if (above == stamps_.begin()) {
100 return std::nullopt;
101 }
102 return static_cast<std::size_t>((above - 1) - stamps_.begin());
103 }
104
111 friend bool operator==(const TimeIndex& lhs, const TimeIndex& rhs) {
112 return lhs.stamps_ == rhs.stamps_;
113 }
114
121 friend bool operator!=(const TimeIndex& lhs, const TimeIndex& rhs) { return !(lhs == rhs); }
122
123private:
124 std::vector<Instant> stamps_;
125};
126
127} // namespace analytics
128} // 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.
Returns, rolling statistics, indicators and performance metrics over numeric series.
The library: broker-neutral types (core), and the modules below.