Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion backtesting/_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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']
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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
Expand Down