Skip to content

Commit 9bf2988

Browse files
committed
Clip ratios to [0, inf)
1 parent 75355a5 commit 9bf2988

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

backtesting/backtesting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,11 +1410,11 @@ def geometric_mean(x):
14101410

14111411
# Our Sharpe mismatches `empyrical.sharpe_ratio()` because they use arithmetic mean return
14121412
# and simple standard deviation
1413-
s.loc['Sharpe Ratio'] = np.clip(s.loc['Return (Ann.) [%]'] / (s.loc['Volatility (Ann.) [%]'] or np.nan), -1, np.inf) # noqa: E501
1413+
s.loc['Sharpe Ratio'] = np.clip(s.loc['Return (Ann.) [%]'] / (s.loc['Volatility (Ann.) [%]'] or np.nan), 0, np.inf) # noqa: E501
14141414
# Our Sortino mismatches `empyrical.sortino_ratio()` because they use arithmetic mean return
1415-
s.loc['Sortino Ratio'] = annualized_return / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)) # noqa: E501
1415+
s.loc['Sortino Ratio'] = np.clip(annualized_return / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)), 0, np.inf) # noqa: E501
14161416
max_dd = -np.nan_to_num(dd.max())
1417-
s.loc['Calmar Ratio'] = annualized_return / (-max_dd or np.nan)
1417+
s.loc['Calmar Ratio'] = np.clip(annualized_return / (-max_dd or np.nan), 0, np.inf)
14181418
s.loc['Max. Drawdown [%]'] = max_dd * 100
14191419
s.loc['Avg. Drawdown [%]'] = -dd_peaks.mean() * 100
14201420
s.loc['Max. Drawdown Duration'] = _round_timedelta(dd_dur.max())

0 commit comments

Comments
 (0)