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 <utility>
11#include <optional>
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,
49 const strategy::SessionWindow& session,
50 math::Scalar trading_days
51) {
52 if (!(trading_days > math::Scalar{0})) {
53 return std::nullopt;
54 }
55 if (interval == BarInterval::ONE_DAY) {
56 return trading_days;
57 }
58 const auto duration = interval_duration(interval);
59 if (!duration) {
60 return std::nullopt;
61 }
62 const auto session_seconds =
63 std::chrono::duration_cast<std::chrono::seconds>(session.close_ - session.open_).count();
64 if (session_seconds <= 0 || duration->count() <= 0) {
65 return std::nullopt;
66 }
67 // Rounded up: the session's last bar may be shorter than the others.
68 const auto per_day = (session_seconds + duration->count() - 1) / duration->count();
69 return static_cast<math::Scalar>(per_day) * trading_days;
70}
71
79public:
86
91 const strategy::SessionWindow& session() const noexcept { return session_; }
92
97 std::chrono::system_clock::time_point now() const noexcept { return now_; }
98
103 strategy::MarketPhase phase() const noexcept { return phase_; }
104
113 bool advance(const strategy::BarEvent& event, std::optional<bool> trading_day) {
114 return advance_to(event.period_end_, trading_day);
115 }
116
129 std::chrono::system_clock::time_point at,
130 std::optional<bool> trading_day
131 ) {
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(
149 const strategy::BarEvent& previous,
150 const strategy::BarEvent& event
151 ) {
152 const auto duration = interval_duration(event.interval_);
153 if (!duration || previous.interval_ != event.interval_) {
154 // Undecidable for intervals with no fixed length; reported as contiguous.
155 return true;
156 }
157 return event.period_end_ - previous.period_end_ <= *duration;
158 }
159
160private:
162 std::chrono::system_clock::time_point now_{};
164};
165
166} // namespace backtest
167} // 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:103
const strategy::SessionWindow & session() const noexcept
Returns the session window.
Definition clock.hpp:91
std::chrono::system_clock::time_point now() const noexcept
Returns the instant the replay has reached.
Definition clock.hpp:97
ReplayClock(strategy::SessionWindow session=strategy::SessionWindow::krx())
Constructs a clock.
Definition clock.hpp:84
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:113
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:128
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 clock.hpp:14
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:30
@ UNKNOWN
The calendar does not cover this date; nothing is known about it.
Definition event.hpp:32
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:202
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:218
BarInterval interval_
The period length the bar covers.
Definition event.hpp:206
The intraday session of a market, as the operator states it.
Definition event.hpp:106
std::chrono::minutes open_
The session open, in minutes from local midnight.
Definition event.hpp:108
std::chrono::minutes close_
The session close, in minutes from local midnight.
Definition event.hpp:110
static SessionWindow krx()
Returns the KRX regular session, 09:00-15:30 KST.
Definition event.hpp:120