quantlab.runs.backtest_stats.round_trip_stats#

quantlab.runs.backtest_stats.round_trip_stats(trips, *, bar_interval)[source]#

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

The rows, in this order, are what vectorbt’s Portfolio.stats reports in the position trade view, and on round_trips of a vectorbt run’s own fills they are equal to the bit: Total Trades, Total Closed Trades and Total Open Trades (counts), Open Trade PnL (the summed PnL of the open round trips, 0 without one), then over the closed round trips only: Win Rate [%] (PnL above zero), Best Trade [%] and Worst Trade [%] (returns), Avg Winning Trade [%] and Avg Losing Trade [%], Avg Winning Trade Duration and Avg Losing Trade Duration (mean bars times bar_interval), Profit Factor (summed winning PnL over the absolute summed losing PnL) and Expectancy (win rate x mean win - (1 - win rate) x |mean loss|). A row with nothing to average is NaN (NaT for a duration).

Parameters:
tripsxarray.Dataset

round_trips’s output, or any dataset with its status, bars, pnl and return on a trade dimension.

bar_interval

One bar, anything pd.Timedelta accepts.

Returns:
dict

The thirteen rows.

Examples

>>> trips = xr.Dataset({
...     "status": ("trade", ["Closed", "Closed", "Closed", "Open"]),
...     "bars": ("trade", [2, 4, 3, 1]),
...     "pnl": ("trade", [30.0, -10.0, 20.0, 5.0]),
...     "return": ("trade", [0.3, -0.1, 0.2, 0.05]),
... })
>>> stats = round_trip_stats(trips, bar_interval="1D")
>>> stats["Total Trades"], stats["Open Trade PnL"], round(stats["Win Rate [%]"], 4)
(4, 5.0, 66.6667)
>>> stats["Avg Winning Trade Duration"], stats["Profit Factor"]
(Timedelta('2 days 12:00:00'), 5.0)