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 {
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
123
private
:
124
std::vector<Instant> stamps_;
125
};
126
127
}
// namespace analytics
128
}
// 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:80
algotradelib::analytics::TimeIndex::operator==
friend bool operator==(const TimeIndex &lhs, const TimeIndex &rhs)
Compares two indices instant by instant.
Definition
time_index.hpp:111
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:121
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:97
algotradelib::analytics::TimeIndex::stamps
const std::vector< Instant > & stamps() const noexcept
Returns all instants.
Definition
time_index.hpp:72
algotradelib::analytics::TimeIndex::empty
bool empty() const noexcept
Tests whether the index is empty.
Definition
time_index.hpp:52
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:61
algotradelib::analytics
Returns, rolling statistics, indicators and performance metrics over numeric series.
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