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

Replays a strategy over stored bars: fill and cost models, the replay ledger, and the report. More...

Classes

struct  AppliedRate
 The commission rate that charged an execution, as the backtest report lists it. More...
class  Backtester
 Replays bars (and quotes) through one strategy, with a ledger and a fill model. More...
struct  BacktestTag
 The broker tag of a replay's order references (they reach no broker). More...
struct  CommissionCharge
 What one execution was charged: the broker's commission and the institution fee. More...
struct  CommissionModel
 A flat commission: a rate on traded value, with an optional per-execution minimum. More...
class  CommissionPolicy
 How a backtest charges commission: one stated model, or a schedule resolved per execution. More...
struct  EquityPoint
 One point on a replay's equity curve, recorded per bar. More...
struct  Execution
 One execution a backtest produced, and what it cost. More...
struct  FillDecision
 What NextBarFillModel::decide concluded about one order and one bar. More...
class  NextBarFillModel
 The fill model: market orders at the next bar's open, limit orders when the next bar's range reaches them, stop-limit orders once released. More...
struct  QueuedOrder
 One order a replay accepted and has not yet filled or withdrawn. More...
class  QueueingExecutor
 The replay's executor: accepts orders and holds them until they fill or are withdrawn. More...
class  ReplayClock
 A backtest's clock: the current instant and market phase, moved by the replayed events. More...
struct  ReplayConfig
 A replay's settings, besides the strategy and the bars. More...
class  ReplayLedger
 Positions, cash and realized profit, updated by each execution of a replay. More...
struct  ReplayResult
 Everything a replay produced; summarize turns it into a Report. More...
struct  Report
 The summary of a replay; read halted_ and caveats_ before the figures. More...
struct  ReportCaveats
 What a replay's figures assumed and did not account for. More...
struct  SellTax
 A tax on sales, stated by the caller; see NextBarFillModel::with_sell_tax. More...
struct  SlippageModel
 Slippage: how far a fill price moves against the order (a buy pays more, a sell gets less). More...

Enumerations

enum class  LimitFillPrice { AT_LIMIT , AT_OPEN_IF_BETTER }
 Which price a backtest credits a limit order that fills. More...

Functions

std::optional< std::chrono::seconds > interval_duration (BarInterval interval)
 Returns the fixed duration of an interval (bar_interval_length).
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.
Result< CommissionModel > commission_model_from (const CommissionSchedule &schedule, Country country, const Date &date)
 Builds a flat commission model from a schedule's rate for a market on a date.
constexpr std::string_view limit_fill_price_name (LimitFillPrice choice) noexcept
 Returns a lowercase name for a choice, such as "at_limit", for reports.
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.

Detailed Description

Replays a strategy over stored bars: fill and cost models, the replay ledger, and the report.

Enumeration Type Documentation

◆ LimitFillPrice

Which price a backtest credits a limit order that fills.

Enumerator
AT_LIMIT 

The default: fill at the limit, or at the bar's worst traded price when the whole bar was better than the limit.

A bar that gaps through a resting limit is not credited its open. A limit marketable through the whole bar (a buy above the high) is credited the high (a sell below the low, the low), which is better than the limit; ReportCaveats::limit_fills_bounded_ counts those fills.

AT_OPEN_IF_BETTER 

Fill at the bar's open when it opened through the limit (assumes the order was resting before the open).

Definition at line 562 of file cost.hpp.

Function Documentation

◆ commission_model_from()

Result< CommissionModel > algotradelib::backtest::commission_model_from ( const CommissionSchedule & schedule,
Country country,
const Date & date )
inline

Builds a flat commission model from a schedule's rate for a market on a date.

The model has no minimum (none is published) and no tax; it charges that one rate for the whole replay, even if the schedule changes rate later. For per-date rates use CommissionPolicy::scheduled.

Parameters
scheduleThe schedule, published by a broker or stated by the caller.
countryThe market.
dateThe date whose rate to use.
Returns
The model (from_broker_ true only when the schedule's origin_ is BROKER_PUBLISHED), or an INVALID_ARGUMENT error with broker code no_rate_for_date or rate_bounds_unreadable (see CommissionSchedule::RateSelection).

Definition at line 94 of file cost.hpp.

Referenced by commission_model_from().

◆ interval_duration()

std::optional< std::chrono::seconds > algotradelib::backtest::interval_duration ( BarInterval interval)
inline

Returns the fixed duration of an interval (bar_interval_length).

Parameters
intervalThe interval.
Returns
The length of an intraday interval; std::nullopt for a day or longer, which has no fixed trading duration.

Definition at line 30 of file clock.hpp.

Referenced by algotradelib::backtest::ReplayClock::contiguous(), interval_duration(), and periods_per_year().

◆ limit_fill_price_name()

std::string_view algotradelib::backtest::limit_fill_price_name ( LimitFillPrice choice)
inlineconstexprnoexcept

Returns a lowercase name for a choice, such as "at_limit", for reports.

Parameters
choiceThe choice.
Returns
The name.

Definition at line 584 of file cost.hpp.

Referenced by limit_fill_price_name().

◆ periods_per_year()

std::optional< math::Scalar > algotradelib::backtest::periods_per_year ( BarInterval interval,
const strategy::SessionWindow & session,
math::Scalar trading_days )
inline

Returns how many bars of an interval a year contains, for analytics::annualize_ratio.

Daily bars: trading_days. Intraday bars: the bars a session holds, times trading_days. A session that is not a whole number of bars ends with a shorter one, which counts: KRX's 390-minute session holds 390 one-minute bars (95,550 a year over 245 days) and 7 hourly ones, the last of them 30 minutes.

Parameters
intervalThe bar interval.
sessionThe session window (used for intraday bars).
trading_daysTrading days per year, such as about 245 for KRX or 252 for US markets.
Returns
The count, or std::nullopt for weekly, monthly or yearly bars, a non-positive trading_days, or (for intraday bars) a session whose close is not after its open.

Definition at line 47 of file clock.hpp.

Referenced by periods_per_year(), and summarize().

◆ summarize()

Report algotradelib::backtest::summarize ( const ReplayResult & result,
const NextBarFillModel & fills,
std::optional< math::Scalar > periods_per_year = std::nullopt,
math::Scalar risk_free = math::Scalar{0} )
inline

Summarizes a replay into a Report.

Parameters
resultThe replay's result.
fillsThe fill model the replay used, so the report states its costs and assumptions.
periods_per_yearPeriods in a year, for the annualized Sharpe ratio (see backtest::periods_per_year); absent leaves it missing().
risk_freeThe risk-free rate per period, in the units of the returns.
Returns
The report.
Exceptions
std::invalid_argumentif periods_per_year is given, not positive, and the equity curve is not empty.

Definition at line 326 of file report.hpp.

Referenced by summarize().