diff --git a/README.md b/README.md index d9b511b0..4ea87037 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ Avg. Drawdown [%] -5.58 Max. Drawdown Duration 688 days 00:00:00 Avg. Drawdown Duration 41 days 00:00:00 # Trades 93 +# Long Trades 93 +# Short Trades 0 Win Rate [%] 53.76 Best Trade [%] 57.12 Worst Trade [%] -16.63 diff --git a/backtesting/_stats.py b/backtesting/_stats.py index 3888192b..4ef41705 100644 --- a/backtesting/_stats.py +++ b/backtesting/_stats.py @@ -169,6 +169,8 @@ def _round_timedelta(value, _period=_data_period(index)): s['Max. Drawdown Duration'] = _round_timedelta(dd_dur.max()) s['Avg. Drawdown Duration'] = _round_timedelta(dd_dur.mean()) s['# Trades'] = n_trades = len(trades_df) + s['# Long Trades'] = int((trades_df['Size'] > 0).sum()) if n_trades else 0 + s['# Short Trades'] = int((trades_df['Size'] < 0).sum()) if n_trades else 0 win_rate = np.nan if not n_trades else (pl > 0).mean() s['Win Rate [%]'] = win_rate * 100 s['Best Trade [%]'] = returns.max() * 100 diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index d356b211..db589980 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -1296,6 +1296,8 @@ def run(self, **kwargs) -> pd.Series: Max. Drawdown Duration 584 days 00:00:00 Avg. Drawdown Duration 41 days 00:00:00 # Trades 66 + # Long Trades 33 + # Short Trades 33 Win Rate [%] 46.9697 Best Trade [%] 53.59595 Worst Trade [%] -18.39887 diff --git a/backtesting/test/_test.py b/backtesting/test/_test.py index d74fde9f..eb6bd541 100644 --- a/backtesting/test/_test.py +++ b/backtesting/test/_test.py @@ -330,6 +330,8 @@ def test_compute_stats(self): expected = pd.Series({ # NOTE: These values are also used on the website! # noqa: E126 '# Trades': 66, + '# Long Trades': 33, + '# Short Trades': 33, 'Avg. Drawdown Duration': pd.Timedelta('41 days 00:00:00'), 'Avg. Drawdown [%]': -5.925851581948801, 'Avg. Trade Duration': pd.Timedelta('46 days 00:00:00'), @@ -378,6 +380,9 @@ def almost_equal(a, b): sorted(['Equity', 'DrawdownPct', 'DrawdownDuration'])) self.assertEqual(len(stats['_trades']), 66) + self.assertEqual(stats['# Long Trades'], 33) + self.assertEqual(stats['# Short Trades'], 33) + self.assertEqual(stats['# Long Trades'] + stats['# Short Trades'], stats['# Trades']) indicator_columns = [ f'{entry}_SMA(C,{n})'