Skip to content

Commit 3461dcf

Browse files
committed
Account for multi-dim indicators and/or no trades
1 parent e91103b commit 3461dcf

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

backtesting/_stats.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ def compute_stats(
7575
trades_df['Tag'] = [t.tag for t in trades]
7676

7777
# Add indicator values
78-
for ind in strategy_instance._indicators:
79-
trades_df[f'Entry_{ind.name}'] = ind[trades_df['EntryBar'].values]
80-
trades_df[f'Exit_{ind.name}'] = ind[trades_df['ExitBar'].values]
78+
if len(trades_df):
79+
for ind in strategy_instance._indicators:
80+
ind = np.atleast_2d(ind)
81+
for i, values in enumerate(ind): # multi-d indicators
82+
suffix = f'_{i}' if len(ind) > 1 else ''
83+
trades_df[f'Entry_{ind.name}{suffix}'] = values[trades_df['EntryBar'].values]
84+
trades_df[f'Exit_{ind.name}{suffix}'] = values[trades_df['ExitBar'].values]
8185

8286
commissions = sum(t._commissions for t in trades)
8387
del trades

0 commit comments

Comments
 (0)