Skip to content

Commit eb42fee

Browse files
committed
Implementation for issue #1309
1 parent 7677461 commit eb42fee

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

β€Žbacktesting/_stats.pyβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def compute_stats(
6060
# Came straight from Backtest.run()
6161
trades_df = pd.DataFrame({
6262
'Size': [t.size for t in trades],
63+
'IsLong': [t.is_long for t in trades],
64+
'IsShort': [t.is_short for t in trades],
6365
'EntryBar': [t.entry_bar for t in trades],
6466
'ExitBar': [t.exit_bar for t in trades],
6567
'EntryPrice': [t.entry_price for t in trades],
@@ -170,6 +172,15 @@ def _round_timedelta(value, _period=_data_period(index)):
170172
s.loc['# Trades'] = n_trades = len(trades_df)
171173
win_rate = np.nan if not n_trades else (pl > 0).mean()
172174
s.loc['Win Rate [%]'] = win_rate * 100
175+
s.loc['# Long Trades'] = n_long_trades = len(trades_df.loc[trades_df['IsLong']])
176+
long_trades_df = trades_df.loc[trades_df['IsLong']]
177+
win_long_rate = np.nan if not n_long_trades else (long_trades_df['PnL'] > 0).mean()
178+
s.loc['Win Long Rate [%]'] = win_long_rate * 100
179+
s.loc['# Short Trades'] = n_short_trades = len(trades_df.loc[trades_df['IsShort']])
180+
short_trades_df = trades_df.loc[trades_df['IsShort']]
181+
win_short_rate = np.nan if not n_short_trades else (short_trades_df['PnL'] > 0).mean()
182+
s.loc['Win Short Rate [%]'] = win_short_rate * 100
183+
s.loc['Long/Short Ratio'] = np.nan if n_long_trades == 0 or n_short_trades == 0 else (n_long_trades / n_short_trades)
173184
s.loc['Best Trade [%]'] = returns.max() * 100
174185
s.loc['Worst Trade [%]'] = returns.min() * 100
175186
mean_return = geometric_mean(returns)

0 commit comments

Comments
Β (0)