From f77c581eac5a4084753fe02cf356bd479a260f50 Mon Sep 17 00:00:00 2001 From: wavebyrd Date: Mon, 16 Mar 2026 09:10:13 -0400 Subject: [PATCH] Add # Long Trades and # Short Trades to stats output Add two new statistics to the backtest results: - # Long Trades: count of trades with positive size (buy positions) - # Short Trades: count of trades with negative size (sell positions) This helps users understand the balance between bullish and bearish positions in their strategies without needing to manually filter the trades dataframe. Fixes #1309 Co-Authored-By: Claude Opus 4.5 --- backtesting/_stats.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backtesting/_stats.py b/backtesting/_stats.py index 1f01c5a3..fbd8b102 100644 --- a/backtesting/_stats.py +++ b/backtesting/_stats.py @@ -168,6 +168,8 @@ def _round_timedelta(value, _period=_data_period(index)): s.loc['Max. Drawdown Duration'] = _round_timedelta(dd_dur.max()) s.loc['Avg. Drawdown Duration'] = _round_timedelta(dd_dur.mean()) s.loc['# Trades'] = n_trades = len(trades_df) + s.loc['# Long Trades'] = (trades_df['Size'] > 0).sum() if n_trades else 0 + s.loc['# Short Trades'] = (trades_df['Size'] < 0).sum() if n_trades else 0 win_rate = np.nan if not n_trades else (pl > 0).mean() s.loc['Win Rate [%]'] = win_rate * 100 s.loc['Best Trade [%]'] = returns.max() * 100