diff --git a/backtesting/_stats.py b/backtesting/_stats.py index f2bb4f7c..f3010153 100644 --- a/backtesting/_stats.py +++ b/backtesting/_stats.py @@ -38,6 +38,7 @@ def compute_stats( ohlc_data: pd.DataFrame, strategy_instance: 'Strategy', risk_free_rate: float = 0, + order_fees:float = 0, ) -> pd.Series: assert -1 < risk_free_rate < 1 @@ -68,6 +69,7 @@ def compute_stats( 'Tag': [t.tag for t in trades], }) trades_df['Duration'] = trades_df['ExitTime'] - trades_df['EntryTime'] + total_fees = len(trades) * order_fees del trades pl = trades_df['PnL'] @@ -90,7 +92,11 @@ def _round_timedelta(value, _period=_data_period(index)): have_position[t.EntryBar:t.ExitBar + 1] = 1 s.loc['Exposure Time [%]'] = have_position.mean() * 100 # In "n bars" time, not index time - s.loc['Equity Final [$]'] = equity[-1] + if total_fees != 0: + s.loc['Equity Final [$] (without fees)'] = equity[-1] + s.loc['Equity Final [$]'] = equity[-1] - total_fees + else: + s.loc['Equity Final [$]'] = equity[-1] s.loc['Equity Peak [$]'] = equity.max() s.loc['Return [%]'] = (equity[-1] - equity[0]) / equity[0] * 100 c = ohlc_data.Close.values diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index 9c168703..2a2d5e9a 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -1030,7 +1030,8 @@ def __init__(self, margin: float = 1., trade_on_close=False, hedging=False, - exclusive_orders=False + exclusive_orders=False, + order_fees : float = 0 ): """ Initialize a backtest. Requires data and a strategy to test. @@ -1133,6 +1134,7 @@ def __init__(self, ) self._strategy = strategy self._results: Optional[pd.Series] = None + self.order_fees = order_fees def run(self, **kwargs) -> pd.Series: """ @@ -1238,6 +1240,7 @@ def run(self, **kwargs) -> pd.Series: ohlc_data=self._data, risk_free_rate=0.0, strategy_instance=strategy, + order_fees=self.order_fees ) return self._results