algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::backtest::ReplayClock Class Reference

A backtest's clock: the current instant and market phase, moved by the replayed events. More...

#include <algotradelib/src/backtest/clock.hpp>

Public Member Functions

 ReplayClock (strategy::SessionWindow session=strategy::SessionWindow::krx())
 Constructs a clock.
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.
strategy::MarketPhase phase () const noexcept
 Returns the market phase at the current instant.
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 quote).

Static Public Member Functions

static bool contiguous (const strategy::BarEvent &previous, const strategy::BarEvent &event)
 Tests whether a bar follows the previous one with no gap.

Detailed Description

A backtest's clock: the current instant and market phase, moved by the replayed events.

It never reads the wall clock. "Now" is the period end of the last bar (the earliest moment it could be known), or the instant of a quote.

Definition at line 78 of file clock.hpp.

Constructor & Destructor Documentation

◆ ReplayClock()

algotradelib::backtest::ReplayClock::ReplayClock ( strategy::SessionWindow session = strategy::SessionWindow::krx())
inlineexplicit

Constructs a clock.

Parameters
sessionThe session window phases are derived from.

Definition at line 84 of file clock.hpp.

Member Function Documentation

◆ advance()

bool algotradelib::backtest::ReplayClock::advance ( const strategy::BarEvent & event,
std::optional< bool > trading_day )
inline

Moves the clock to a bar's period end and recomputes the phase.

Parameters
eventThe bar.
trading_dayWhether the bar's date trades, as the calendar said; empty gives UNKNOWN, in which the default risk limits refuse to open positions.
Returns
true if the phase changed (so the caller fires on_phase).

Definition at line 113 of file clock.hpp.

◆ advance_to()

bool algotradelib::backtest::ReplayClock::advance_to ( std::chrono::system_clock::time_point at,
std::optional< bool > trading_day )
inline

Moves the clock to an instant and recomputes the phase, for an event that is not a bar (such as a quote).

The clock goes wherever it is told, even backwards; feeding events in time order is the caller's job (see Backtester::feed).

Parameters
atThe instant.
trading_dayWhether that date trades; empty gives UNKNOWN.
Returns
true if the phase changed.

Definition at line 128 of file clock.hpp.

Referenced by advance().

◆ contiguous()

bool algotradelib::backtest::ReplayClock::contiguous ( const strategy::BarEvent & previous,
const strategy::BarEvent & event )
inlinestatic

Tests whether a bar follows the previous one with no gap.

Parameters
previousThe earlier bar.
eventThe later bar.
Returns
false if both are intraday bars of one interval, more than one interval apart (such as across sessions); true otherwise, including for daily and longer bars and for two different intervals, where gaps are not decided here.

Definition at line 148 of file clock.hpp.

Referenced by algotradelib::backtest::Backtester< Strategy, CapsType >::feed().

◆ now()

std::chrono::system_clock::time_point algotradelib::backtest::ReplayClock::now ( ) const
inlinenoexcept

Returns the instant the replay has reached.

Returns
The current instant; the epoch before the first event.

Definition at line 97 of file clock.hpp.

◆ phase()

strategy::MarketPhase algotradelib::backtest::ReplayClock::phase ( ) const
inlinenoexcept

Returns the market phase at the current instant.

Returns
The phase; UNKNOWN before the first event.

Definition at line 103 of file clock.hpp.

◆ session()

const strategy::SessionWindow & algotradelib::backtest::ReplayClock::session ( ) const
inlinenoexcept

Returns the session window.

Returns
The window phases are derived from.

Definition at line 91 of file clock.hpp.

Referenced by ReplayClock().


The documentation for this class was generated from the following file: