algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
clock.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include "../math/scalar.hpp"
6
7#include <algotradelib/core>
8
9#include <chrono>
10#include <optional>
11#include <utility>
12
13namespace algotradelib {
14namespace backtest {
15
23
30inline std::optional<std::chrono::seconds> interval_duration(BarInterval interval) {
31 return bar_interval_length(interval);
32}
33
47inline std::optional<math::Scalar> periods_per_year(
48 BarInterval interval, const strategy::SessionWindow& session, math::Scalar trading_days
49) {
50 if (!(trading_days > math::Scalar{0})) {
51 return std::nullopt;
52 }
53 if (interval == BarInterval::ONE_DAY) {
54 return trading_days;
55 }
56 const auto duration = interval_duration(interval);
57 if (!duration) {
58 return std::nullopt;
59 }
60 const auto session_seconds = std::chrono::duration_cast<std::chrono::seconds>(session.close_ - session.open_).count();
61 if (session_seconds <= 0 || duration->count() <= 0) {
62 return std::nullopt;
63 }
64 // Rounded up: the session's last bar may be shorter than the others.
65 const auto per_day = (session_seconds + duration->count() - 1) / duration->count();
66 return static_cast<math::Scalar>(per_day) * trading_days;
67}
68
76public:
83
88 const strategy::SessionWindow& session() const noexcept {
89 return session_;
90 }
91
96 std::chrono::system_clock::time_point now() const noexcept {
97 return now_;
98 }
99
104 strategy::MarketPhase phase() const noexcept {
105 return phase_;
106 }
107
116 bool advance(const strategy::BarEvent& event, std::optional<bool> trading_day) {
117 return advance_to(event.period_end_, trading_day);
118 }
119
131 bool advance_to(std::chrono::system_clock::time_point at, std::optional<bool> trading_day) {
132 now_ = at;
133 const auto next = session_.phase(now_, trading_day);
134 const bool changed = next != phase_;
135 phase_ = next;
136 return changed;
137 }
138
148 static bool contiguous(const strategy::BarEvent& previous, const strategy::BarEvent& event) {
149 return contiguous(previous.interval_, previous.period_end_, event);
150 }
151
159 static bool contiguous(
160 BarInterval previous_interval, std::chrono::system_clock::time_point previous_end, const strategy::BarEvent& event
161 ) {
162 const auto duration = interval_duration(event.interval_);
163 if (!duration || previous_interval != event.interval_) {
164 // Undecidable for intervals with no fixed length; reported as contiguous.
165 return true;
166 }
167 return event.period_end_ - previous_end <= *duration;
168 }
169
170private:
172 std::chrono::system_clock::time_point now_{};
174};
175
176} // namespace backtest
177} // namespace algotradelib
static bool contiguous(const strategy::BarEvent &previous, const strategy::BarEvent &event)
Tests whether a bar follows the previous one with no gap.
Definition clock.hpp:148
strategy::MarketPhase phase() const noexcept
Returns the market phase at the current instant.
Definition clock.hpp:104
const strategy::SessionWindow & session() const noexcept
Returns the session window.
Definition clock.hpp:88
std::chrono::system_clock::time_point now() const noexcept
Returns the instant the replay has reached.
Definition clock.hpp:96
ReplayClock(strategy::SessionWindow session=strategy::SessionWindow::krx())
Constructs a clock.
Definition clock.hpp:81
static bool contiguous(BarInterval previous_interval, std::chrono::system_clock::time_point previous_end, const strategy::BarEvent &event)
As contiguous(previous, event), given only what it reads of the previous bar.
Definition clock.hpp:159
bool advance(const strategy::BarEvent &event, std::optional< bool > trading_day)
Moves the clock to a bar's period end and recomputes the phase.
Definition clock.hpp:116
bool advance_to(std::chrono::system_clock::time_point at, std::optional< bool > trading_day)
Moves the clock to an instant and recomputes the phase, for an event that is not a bar (such as a quo...
Definition clock.hpp:131
The events a runner delivers to a strategy's handlers, and the market phase.
Replays a strategy over stored bars: fill and cost models, the replay ledger, and the report.
Definition bootstrap.hpp:16
std::optional< math::Scalar > periods_per_year(BarInterval interval, const strategy::SessionWindow &session, math::Scalar trading_days)
Returns how many bars of an interval a year contains, for analytics::annualize_ratio.
Definition clock.hpp:47
std::optional< std::chrono::seconds > interval_duration(BarInterval interval)
Returns the fixed duration of an interval (bar_interval_length).
Definition clock.hpp:30
double Scalar
The floating-point type the analytics and math layers compute in (double).
Definition scalar.hpp:18
MarketPhase
Where the clock stands relative to the trading session.
Definition event.hpp:33
@ UNKNOWN
Nothing is known about this instant: the calendar does not cover its date, or the filled session hour...
Definition event.hpp:38
The library: broker-neutral types (core), and the modules below.
constexpr std::optional< std::chrono::seconds > bar_interval_length(BarInterval interval) noexcept
Returns an interval's fixed length, for an intraday interval.
BarInterval
The period one bar covers.
One bar, delivered to on_bar.
Definition event.hpp:313
std::chrono::system_clock::time_point period_end_
When the bar's period ended: the earliest moment the bar could be known.
Definition event.hpp:329
BarInterval interval_
The period length the bar covers.
Definition event.hpp:317
The intraday session of a market, as the operator states it.
Definition event.hpp:122
std::chrono::minutes open_
The session open, in minutes from local midnight.
Definition event.hpp:124
std::chrono::minutes close_
The session close, in minutes from local midnight.
Definition event.hpp:126
static SessionWindow krx()
Returns the KRX regular session, 09:00-15:30 KST.
Definition event.hpp:136