Skip to content

Commit 0363e88

Browse files
committed
ENH: Add Trade.entry_time/.exit_time
Refs: #47 (comment) Fixes: #117
1 parent 28cf0a6 commit 0363e88

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

backtesting/backtesting.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,16 @@ def _tp_order(self):
574574

575575
# Extra properties
576576

577+
@property
578+
def entry_time(self):
579+
"""Datetime of when the trade was entered."""
580+
return self.__broker._data.index[self.__entry_bar]
581+
582+
@property
583+
def exit_time(self):
584+
"""Datetime of when the trade was exited."""
585+
return self.__broker._data.index[self.__exit_bar]
586+
577587
@property
578588
def is_long(self):
579589
"""True if the trade is long (trade size is positive)."""
@@ -1307,9 +1317,9 @@ def _compute_stats(self, broker: _Broker, strategy: Strategy) -> pd.Series:
13071317
'ExitPrice': [t.exit_price for t in trades],
13081318
'PnL': [t.pl for t in trades],
13091319
'ReturnPct': [t.pl_pct for t in trades],
1320+
'EntryTime': [t.entry_time for t in trades],
1321+
'ExitTime': [t.exit_time for t in trades],
13101322
})
1311-
trades_df['EntryTime'] = trades_df['EntryBar'].apply(index.__getitem__)
1312-
trades_df['ExitTime'] = trades_df['ExitBar'].apply(index.__getitem__)
13131323
trades_df['Duration'] = trades_df['ExitTime'] - trades_df['EntryTime']
13141324

13151325
pl = trades_df['PnL']

0 commit comments

Comments
 (0)