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
wholeblock.The rows, in this order, are what vectorbt’s
Portfolio.statsreports in the position trade view, and onround_tripsof a vectorbt run’s own fills they are equal to the bit:Total Trades,Total Closed TradesandTotal 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 [%]andWorst Trade [%](returns),Avg Winning Trade [%]andAvg Losing Trade [%],Avg Winning Trade DurationandAvg Losing Trade Duration(meanbarstimesbar_interval),Profit Factor(summed winning PnL over the absolute summed losing PnL) andExpectancy(win rate x mean win - (1 - win rate) x |mean loss|). A row with nothing to average is NaN (NaTfor a duration).- Parameters:
- tripsxarray.Dataset
round_trips’s output, or any dataset with itsstatus,bars,pnlandreturnon atradedimension.- bar_interval
One bar, anything
pd.Timedeltaaccepts.
- 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)