![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
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. | |
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().
| Strategy | The strategy type. |
| CapsType | The broker capabilities the strategy sees as Ctx::caps. |
Definition at line 437 of file engine.hpp.
| using algotradelib::backtest::Backtester< Strategy, CapsType >::Executor = QueueingExecutor<Tag> |
The executor type.
Definition at line 442 of file engine.hpp.
| 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.
| using algotradelib::backtest::Backtester< Strategy, CapsType >::Tag = BacktestTag |
The order-reference tag.
Definition at line 440 of file engine.hpp.
|
inline |
Constructs a replay.
| strategy | The strategy. |
| config | The settings. |
| fills | The fill model (commission, slippage, limit pricing, sale tax). |
Definition at line 452 of file engine.hpp.
|
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.
| event | The bar; feed bars in timestamp order across instruments. Within one instrument and interval each bar's timestamp must be later than the last. |
| trading_day | Whether the bar's date trades, for the phase; empty gives UNKNOWN. |
| 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.
|
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.
| event | The quote; feed it in timestamp order with the bars, since the clock follows whatever instant it is given, even backwards. |
| trading_day | Whether the quote's date trades; empty gives UNKNOWN. |
| std::invalid_argument | for 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.
|
inlinenoexcept |
Returns the fill model, for summarize.
Definition at line 486 of file engine.hpp.
|
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.
|
inlinenoexcept |
Returns the ledger: positions, cash and realized profit.
Definition at line 480 of file engine.hpp.
|
inlinenoexcept |
Returns what the replay has produced so far.
Definition at line 492 of file engine.hpp.
|
inlinenoexcept |
Returns the strategy, to read the state it kept.
Definition at line 474 of file engine.hpp.