From b4f4b693272a2c3c338ed75aba6f8c17321bb415 Mon Sep 17 00:00:00 2001 From: ali gheshlaghi Date: Mon, 7 Aug 2023 01:49:02 +0330 Subject: [PATCH 1/2] Added TP and SL into stats file. --- backtesting/_stats.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backtesting/_stats.py b/backtesting/_stats.py index f2bb4f7c..64ddf8bf 100644 --- a/backtesting/_stats.py +++ b/backtesting/_stats.py @@ -65,6 +65,8 @@ def compute_stats( 'ReturnPct': [t.pl_pct for t in trades], 'EntryTime': [t.entry_time for t in trades], 'ExitTime': [t.exit_time for t in trades], + 'SL': [t.sl for t in trades], + 'TP': [t.tp for t in trades], 'Tag': [t.tag for t in trades], }) trades_df['Duration'] = trades_df['ExitTime'] - trades_df['EntryTime'] From de0eb2b661a0cd759fd8a3c6e3a2fb4660d37971 Mon Sep 17 00:00:00 2001 From: Kernc Date: Wed, 22 Jan 2025 03:39:27 +0100 Subject: [PATCH 2/2] Update column order and add test --- backtesting/_stats.py | 6 +++--- backtesting/test/_test.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backtesting/_stats.py b/backtesting/_stats.py index b235cfab..b22dfa2f 100644 --- a/backtesting/_stats.py +++ b/backtesting/_stats.py @@ -64,15 +64,15 @@ def compute_stats( 'ExitBar': [t.exit_bar for t in trades], 'EntryPrice': [t.entry_price for t in trades], 'ExitPrice': [t.exit_price for t in trades], + 'SL': [t.sl for t in trades], + 'TP': [t.tp for t in trades], 'PnL': [t.pl for t in trades], 'ReturnPct': [t.pl_pct for t in trades], 'EntryTime': [t.entry_time for t in trades], 'ExitTime': [t.exit_time for t in trades], - 'SL': [t.sl for t in trades], - 'TP': [t.tp for t in trades], - 'Tag': [t.tag for t in trades], }) trades_df['Duration'] = trades_df['ExitTime'] - trades_df['EntryTime'] + trades_df['Tag'] = [t.tag for t in trades] commissions = sum(t._commissions for t in trades) del trades diff --git a/backtesting/test/_test.py b/backtesting/test/_test.py index 5d7aa20d..d5373f1a 100644 --- a/backtesting/test/_test.py +++ b/backtesting/test/_test.py @@ -335,7 +335,7 @@ def almost_equal(a, b): self.assertSequenceEqual( sorted(stats['_trades'].columns), - sorted(['Size', 'EntryBar', 'ExitBar', 'EntryPrice', 'ExitPrice', + sorted(['Size', 'EntryBar', 'ExitBar', 'EntryPrice', 'ExitPrice', 'SL', 'TP', 'PnL', 'ReturnPct', 'EntryTime', 'ExitTime', 'Duration', 'Tag'])) def test_compute_stats_bordercase(self):