5#include "../math/scalar.hpp"
7#include <algotradelib/core>
55 if (interval == BarInterval::ONE_DAY) {
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) {
68 const auto per_day = (session_seconds + duration->count() - 1) / duration->count();
69 return static_cast<math::Scalar>(per_day) * trading_days;
85 : session_(std::move(
session)) {}
97 std::chrono::system_clock::time_point
now() const noexcept {
return now_; }
129 std::chrono::system_clock::time_point at,
130 std::optional<bool> trading_day
133 const auto next = session_.phase(now_, trading_day);
134 const bool changed = next != phase_;
157 return event.period_end_ - previous.
period_end_ <= *duration;
162 std::chrono::system_clock::time_point now_{};
static bool contiguous(const strategy::BarEvent &previous, const strategy::BarEvent &event)
Tests whether a bar follows the previous one with no gap.
strategy::MarketPhase phase() const noexcept
Returns the market phase at the current instant.
const strategy::SessionWindow & session() const noexcept
Returns the session window.
std::chrono::system_clock::time_point now() const noexcept
Returns the instant the replay has reached.
ReplayClock(strategy::SessionWindow session=strategy::SessionWindow::krx())
Constructs a clock.
bool advance(const strategy::BarEvent &event, std::optional< bool > trading_day)
Moves the clock to a bar's period end and recomputes the phase.
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...
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.
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.
std::optional< std::chrono::seconds > interval_duration(BarInterval interval)
Returns the fixed duration of an interval (bar_interval_length).
double Scalar
The floating-point type the analytics and math layers compute in (double).
MarketPhase
Where the clock stands relative to the trading session.
@ UNKNOWN
The calendar does not cover this date; nothing is known about it.
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.
std::chrono::system_clock::time_point period_end_
When the bar's period ended: the earliest moment the bar could be known.
BarInterval interval_
The period length the bar covers.
The intraday session of a market, as the operator states it.
std::chrono::minutes open_
The session open, in minutes from local midnight.
std::chrono::minutes close_
The session close, in minutes from local midnight.
static SessionWindow krx()
Returns the KRX regular session, 09:00-15:30 KST.