Skip to content

Commit ce06b81

Browse files
ENH: Add columns SL and TP to trade_df stats (kernc#1039)
* Added TP and SL into stats file. * Update column order and add test
1 parent e2122b9 commit ce06b81

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

backtesting/_stats.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ def compute_stats(
6464
'ExitBar': [t.exit_bar for t in trades],
6565
'EntryPrice': [t.entry_price for t in trades],
6666
'ExitPrice': [t.exit_price for t in trades],
67+
'SL': [t.sl for t in trades],
68+
'TP': [t.tp for t in trades],
6769
'PnL': [t.pl for t in trades],
6870
'ReturnPct': [t.pl_pct for t in trades],
6971
'EntryTime': [t.entry_time for t in trades],
7072
'ExitTime': [t.exit_time for t in trades],
71-
'Tag': [t.tag for t in trades],
7273
})
7374
trades_df['Duration'] = trades_df['ExitTime'] - trades_df['EntryTime']
75+
trades_df['Tag'] = [t.tag for t in trades]
7476
commissions = sum(t._commissions for t in trades)
7577
del trades
7678

backtesting/test/_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def almost_equal(a, b):
335335

336336
self.assertSequenceEqual(
337337
sorted(stats['_trades'].columns),
338-
sorted(['Size', 'EntryBar', 'ExitBar', 'EntryPrice', 'ExitPrice',
338+
sorted(['Size', 'EntryBar', 'ExitBar', 'EntryPrice', 'ExitPrice', 'SL', 'TP',
339339
'PnL', 'ReturnPct', 'EntryTime', 'ExitTime', 'Duration', 'Tag']))
340340

341341
def test_compute_stats_bordercase(self):

0 commit comments

Comments
 (0)