Skip to content

Commit

Permalink
ENH: Add Trade.entry_time/.exit_time
Browse files Browse the repository at this point in the history
Refs: #47 (comment)
Fixes: #117
  • Loading branch information
kernc committed Jul 26, 2020
1 parent 28cf0a6 commit 0363e88
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,16 @@ def _tp_order(self):

# Extra properties

@property
def entry_time(self):
"""Datetime of when the trade was entered."""
return self.__broker._data.index[self.__entry_bar]

@property
def exit_time(self):
"""Datetime of when the trade was exited."""
return self.__broker._data.index[self.__exit_bar]

@property
def is_long(self):
"""True if the trade is long (trade size is positive)."""
Expand Down Expand Up @@ -1307,9 +1317,9 @@ def _compute_stats(self, broker: _Broker, strategy: Strategy) -> pd.Series:
'ExitPrice': [t.exit_price 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],
})
trades_df['EntryTime'] = trades_df['EntryBar'].apply(index.__getitem__)
trades_df['ExitTime'] = trades_df['ExitBar'].apply(index.__getitem__)
trades_df['Duration'] = trades_df['ExitTime'] - trades_df['EntryTime']

pl = trades_df['PnL']
Expand Down

0 comments on commit 0363e88

Please sign in to comment.