algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::backtest::Backtester< Strategy, CapsType > Class Template Reference

Replays bars (and quotes) through one strategy, with a ledger and a fill model. More...

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

Public Types

using Tag = BacktestTag
 The order-reference tag.
using Executor = QueueingExecutor<Tag>
 The executor type.
using Runner = runner::Runner<Strategy, CapsType, Tag, Executor>
 The runner::Runner the replay drives, the same type a live session uses.

Public Member Functions

 Backtester (Strategy strategy={}, ReplayConfig config={}, NextBarFillModel fills=NextBarFillModel{})
 Constructs a replay.
const Strategy & strategy () const noexcept
 Returns the strategy, to read the state it kept.
const ReplayLedger & ledger () const noexcept
 Returns the ledger: positions, cash and realized profit.
const NextBarFillModel & fills () const noexcept
 Returns the fill model, for summarize.
const ReplayResult & result () const noexcept
 Returns what the replay has produced so far.
void feed (const strategy::BarEvent &event, std::optional< bool > trading_day=true)
 Replays one bar.
void feed (const strategy::QuoteEvent &event, std::optional< bool > trading_day=true)
 Replays one quote: moves the price and the clock to it, re-derives the phase (calling on_phase on a change), calls on_quote, and publishes order updates.
void finish ()
 Ends the replay: calls on_stop (with the current portfolio, including orders from the last bar), settles what it asked for, and counts the orders still queued.

Detailed Description

template<typename Strategy, typename CapsType = strategy::NoCaps>
class algotradelib::backtest::Backtester< Strategy, CapsType >

Replays bars (and quotes) through one strategy, with a ledger and a fill model.

Feed bars with feed, in timestamp order across instruments, then call finish and summarize the result().

backtest::Backtester<MyStrategy> replay(MyStrategy{}, config);
for (const auto& bar : bars) {
replay.feed(strategy::make_bar_event(id, BarInterval::ONE_DAY, BarStamp::CLOSE, bar));
}
replay.finish();
const auto report = backtest::summarize(replay.result(), replay.fills());
Replays bars (and quotes) through one strategy, with a ledger and a fill model.
Definition engine.hpp:437
void finish()
Ends the replay: calls on_stop (with the current portfolio, including orders from the last bar),...
Definition engine.hpp:725
Report summarize(const ReplayResult &result, const NextBarFillModel &fills, std::optional< math::Scalar > periods_per_year=std::nullopt, math::Scalar risk_free=math::Scalar{0})
Summarizes a replay into a Report.
Definition report.hpp:326
BarEvent make_bar_event(InstrumentId instrument, BarInterval interval, BarStamp stamp, Bar bar)
Makes a bar event, computing its period_end_ with bar_period_end.
Definition event.hpp:273
@ CLOSE
The timestamp is the instant the period ended.
Template Parameters
StrategyThe strategy type.
CapsTypeThe broker capabilities the strategy sees as Ctx::caps.

Definition at line 437 of file engine.hpp.

Member Typedef Documentation

◆ Executor

template<typename Strategy, typename CapsType = strategy::NoCaps>
using algotradelib::backtest::Backtester< Strategy, CapsType >::Executor = QueueingExecutor<Tag>

The executor type.

Definition at line 442 of file engine.hpp.

◆ Runner

template<typename Strategy, typename CapsType = strategy::NoCaps>
using algotradelib::backtest::Backtester< Strategy, CapsType >::Runner = runner::Runner<Strategy, CapsType, Tag, Executor>

The runner::Runner the replay drives, the same type a live session uses.

Definition at line 444 of file engine.hpp.

◆ Tag

template<typename Strategy, typename CapsType = strategy::NoCaps>
using algotradelib::backtest::Backtester< Strategy, CapsType >::Tag = BacktestTag

The order-reference tag.

Definition at line 440 of file engine.hpp.

Constructor & Destructor Documentation

◆ Backtester()

template<typename Strategy, typename CapsType = strategy::NoCaps>
algotradelib::backtest::Backtester< Strategy, CapsType >::Backtester ( Strategy strategy = {},
ReplayConfig config = {},
NextBarFillModel fills = NextBarFillModel{} )
inline

Constructs a replay.

Parameters
strategyThe strategy.
configThe settings.
fillsThe fill model (commission, slippage, limit pricing, sale tax).

Definition at line 452 of file engine.hpp.

Member Function Documentation

◆ feed() [1/2]

template<typename Strategy, typename CapsType = strategy::NoCaps>
void algotradelib::backtest::Backtester< Strategy, CapsType >::feed ( const strategy::BarEvent & event,
std::optional< bool > trading_day = true )
inline

Replays one bar.

In order: fill orders resting from earlier bars against this bar (and call on_fill), mark positions at the close and record an equity point, advance the clock to the bar's period end (calling on_phase on a change), then call on_bar, whose orders wait for a later bar. After a halt it does nothing.

For a strategy declaring on_bars, the bar is cached instead and held with the rest of its period; the period goes to on_bars (one call per interval) when the first later bar arrives, before that bar settles, or at a quote at or after it, or at finish. Its orders wait for a later period's bar, as on_bar's do. Such a strategy is fed every bar of a period before anything later.

Parameters
eventThe bar; feed bars in timestamp order across instruments. Within one instrument and interval each bar's timestamp must be later than the last.
trading_dayWhether the bar's date trades, for the phase; empty gives UNKNOWN.
Exceptions
std::invalid_argument(with a linear algebra backend) if series_capacity_ is zero, or the bar is not later than the previous bar of its instrument and interval. The exception propagates after the bar was counted and settled; it does not set ReplayResult::halted_.
std::invalid_argument(for a strategy declaring on_bars, with or without a linear algebra backend) if the bar's period was already delivered, it is earlier than bars already fed, or its instrument and interval already have a bar for that period; thrown before anything changes.

Definition at line 517 of file engine.hpp.

◆ feed() [2/2]

template<typename Strategy, typename CapsType = strategy::NoCaps>
void algotradelib::backtest::Backtester< Strategy, CapsType >::feed ( const strategy::QuoteEvent & event,
std::optional< bool > trading_day = true )
inline

Replays one quote: moves the price and the clock to it, re-derives the phase (calling on_phase on a change), calls on_quote, and publishes order updates.

A quote does not mark the ledger, record an equity point or fill anything. An order placed from on_quote fills against a later bar, not the quote (see ReportCaveats::quotes_). Compared with a live loop, on_phase fires before on_quote here (live, after the iteration's polls), and order updates are always published here (live, only when the working-order poll runs). A quote before the first bar is fine; its orders wait for a bar.

Parameters
eventThe quote; feed it in timestamp order with the bars, since the clock follows whatever instant it is given, even backwards.
trading_dayWhether the quote's date trades; empty gives UNKNOWN.
Exceptions
std::invalid_argumentfor a strategy declaring on_bars, if the quote is earlier than bars already fed; a quote at or after a pending period first delivers that period to on_bars.

Definition at line 649 of file engine.hpp.

◆ fills()

template<typename Strategy, typename CapsType = strategy::NoCaps>
const NextBarFillModel & algotradelib::backtest::Backtester< Strategy, CapsType >::fills ( ) const
inlinenoexcept

Returns the fill model, for summarize.

Returns
The fill model.

Definition at line 486 of file engine.hpp.

◆ finish()

template<typename Strategy, typename CapsType = strategy::NoCaps>
void algotradelib::backtest::Backtester< Strategy, CapsType >::finish ( )
inline

Ends the replay: calls on_stop (with the current portfolio, including orders from the last bar), settles what it asked for, and counts the orders still queued.

For a strategy declaring on_bars, the last period's bars are delivered first.

After a halt it does nothing; on_stop is not called.

Definition at line 725 of file engine.hpp.

◆ ledger()

template<typename Strategy, typename CapsType = strategy::NoCaps>
const ReplayLedger & algotradelib::backtest::Backtester< Strategy, CapsType >::ledger ( ) const
inlinenoexcept

Returns the ledger: positions, cash and realized profit.

Returns
The ledger.

Definition at line 480 of file engine.hpp.

◆ result()

template<typename Strategy, typename CapsType = strategy::NoCaps>
const ReplayResult & algotradelib::backtest::Backtester< Strategy, CapsType >::result ( ) const
inlinenoexcept

Returns what the replay has produced so far.

Returns
The result.

Definition at line 492 of file engine.hpp.

◆ strategy()

template<typename Strategy, typename CapsType = strategy::NoCaps>
const Strategy & algotradelib::backtest::Backtester< Strategy, CapsType >::strategy ( ) const
inlinenoexcept

Returns the strategy, to read the state it kept.

Returns
The strategy.

Definition at line 474 of file engine.hpp.


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