algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
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
11
namespace
algotradelib
{
12
namespace
analytics
{
13
20
class
TimeIndex
{
21
public
:
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
131
private
:
132
std::vector<Instant> stamps_;
133
};
134
135
}
// namespace analytics
136
}
// namespace algotradelib
algotradelib::analytics::TimeIndex::TimeIndex
TimeIndex()=default
Constructs an empty index.
algotradelib::analytics::TimeIndex::find
std::optional< std::size_t > find(const Instant &when) const
Finds the row of an exact instant.
Definition
time_index.hpp:86
algotradelib::analytics::TimeIndex::operator==
friend bool operator==(const TimeIndex &lhs, const TimeIndex &rhs)
Compares two indices instant by instant.
Definition
time_index.hpp:117
algotradelib::analytics::TimeIndex::Instant
std::chrono::system_clock::time_point Instant
The instant type of the index.
Definition
time_index.hpp:23
algotradelib::analytics::TimeIndex::operator!=
friend bool operator!=(const TimeIndex &lhs, const TimeIndex &rhs)
Compares two indices for inequality.
Definition
time_index.hpp:127
algotradelib::analytics::TimeIndex::size
std::size_t size() const noexcept
Returns the number of instants.
Definition
time_index.hpp:46
algotradelib::analytics::TimeIndex::asof
std::optional< std::size_t > asof(const Instant &when) const
Finds the latest row at or before an instant ("as of" lookup).
Definition
time_index.hpp:103
algotradelib::analytics::TimeIndex::stamps
const std::vector< Instant > & stamps() const noexcept
Returns all instants.
Definition
time_index.hpp:76
algotradelib::analytics::TimeIndex::empty
bool empty() const noexcept
Tests whether the index is empty.
Definition
time_index.hpp:54
algotradelib::analytics::TimeIndex::TimeIndex
TimeIndex(std::vector< Instant > stamps)
Constructs an index over a sequence of instants.
Definition
time_index.hpp:34
algotradelib::analytics::TimeIndex::at
const Instant & at(std::size_t position) const
Returns the instant of one row.
Definition
time_index.hpp:65
algotradelib::analytics
Series buffers and frames, the conversions to and from exact decimals, and performance metrics.
Definition
conversion.hpp:14
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib
src
analytics
time_index.hpp
Generated by
1.18.0