quantlab.backtest.base.MarketSpec#

class quantlab.backtest.base.MarketSpec(fill_price_column, valuation_price_column, trading_days_per_year, session_minutes_per_day)[source]#

Bases: object

Backtest conventions of one market: price columns and annualization.

Column names live only on a market’s spec instance; backtester methods read them from self.MARKET and never spell them out, so supporting a new market means writing a new spec rather than changing the base class.

Parameters:
fill_price_columnstr

Price variable that orders execute at, for example the open.

valuation_price_columnstr

Price variable the portfolio is marked to at the end of each bar, for example the close.

trading_days_per_yearint

Trading days in a year, used to annualize daily statistics.

session_minutes_per_dayint

Length of one trading session in minutes, used to annualize intraday statistics.

Examples

>>> spec = MarketSpec(
...     fill_price_column="open",
...     valuation_price_column="close",
...     trading_days_per_year=252,
...     session_minutes_per_day=390,
... )
>>> spec.year_freq("1D")
Timedelta('252 days 00:00:00')
year_freq(bar_interval)[source]#

Return one year in vectorbt’s convention for bars of bar_interval.

quantlab.runs.backtest_stats.year_freq with this market’s trading days and session minutes; its docstring gives the rule.

Parameters:
bar_interval

Anything pd.Timedelta accepts, such as "1D", "5min" or a numpy.timedelta64.

Returns:
pd.Timedelta

The year length as a pd.Timedelta.

Raises:
ValueError

If bar_interval is not positive.

Examples

>>> spec.year_freq("1min") / pd.Timedelta("1min")
98280.0
>>> spec.year_freq("1D") / pd.Timedelta("1D")
252.0
>>> round(spec.year_freq("7D") / pd.Timedelta("7D"), 2)
52.18