quantlab.runs.backtest_stats#

Return, relative, win-rate, turnover and trade statistics of a backtest, as public functions.

These are the returns-based rows and the turnover rows of a backtest run’s metrics.json: the in_sample, out_of_sample and benchmark return blocks, the relative block, the win rates and the three turnover rows, the last two counted on the decided rebalances a run records (its rebalance record), never inferred from its fills; and the round-trip trade rows and Max Gross Exposure [%] of its whole block, from fill records (round_trips, round_trip_stats, exposure_stats), with the deepest drawdown the report marks (drawdown_span). quantlab.runs.run_metrics assembles a run’s metric blocks from them, for the vectorbt backtester and for a tool that simulates a run elsewhere (an event-driven replay of a quantlab run) alike, so the numbers are comparable.

The module imports only numpy, pandas and xarray: no quantlab model or dataset layer, no vectorbt and no torch, so importing it is cheap and drags in no simulation engine. return_stats reimplements vectorbt’s ReturnsAccessor.stats (vectorbt 1.1) to the bit: every sum and product is accumulated in index order, as vectorbt’s compiled kernels do, rather than with numpy’s pairwise reductions, which round differently in the last bits.

Every function takes per-bar values on a timestamp axis (an xarray.DataArray) and plain numbers; ranges are inclusive pairs of bar labels ("2024-01-02" or "2024-01-02T15:30:00"), compared as exact timestamps, in time order and never overlapping.

Functions

drawdown_span

Return the deepest drawdown of a value curve, from its valley to its recovery.

exposure_stats

Return the Max Gross Exposure [%] row of a run's whole block.

in_ranges

Return a boolean mask of the timestamps inside any of ranges.

rebalance_fill_bars

Return the fill bar of every decided rebalance: the bar after it on timestamps.

relative_stats

Return a strategy's statistics relative to its benchmark over ranges.

return_stats

Return the return statistics of a per-bar return series, keyed by row name.

round_trip_stats

Summarize round trips as the trade rows of a run's whole block.

round_trips

Return the position round trips of a run's fills, one per flat-to-flat holding.

turnover

Return the turnover of every bar that had rebalance fills, on a timestamp axis.

turnover_stats

Summarize the rebalance turnover of a slice as the three percent-valued metric rows.

win_rates

Return the share of holding periods and of months the strategy won, in percent.

year_freq

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