Skip to content

Commit 27bfb3f

Browse files
committed
Remove flooring on ratios and adding Kelly Criterion
1 parent 65f54f6 commit 27bfb3f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

backtesting/_stats.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ def _round_timedelta(value, _period=_data_period(index)):
117117

118118
# Our Sharpe mismatches `empyrical.sharpe_ratio()` because they use arithmetic mean return
119119
# and simple standard deviation
120-
s.loc['Sharpe Ratio'] = np.clip((s.loc['Return (Ann.) [%]'] - risk_free_rate) / (s.loc['Volatility (Ann.) [%]'] or np.nan), 0, np.inf) # noqa: E501
120+
s.loc['Sharpe Ratio'] = (s.loc['Return (Ann.) [%]'] - risk_free_rate) / (s.loc['Volatility (Ann.) [%]'] or np.nan) # noqa: E501
121121
# Our Sortino mismatches `empyrical.sortino_ratio()` because they use arithmetic mean return
122-
s.loc['Sortino Ratio'] = np.clip((annualized_return - risk_free_rate) / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)), 0, np.inf) # noqa: E501
122+
s.loc['Sortino Ratio'] = (annualized_return - risk_free_rate) / (np.sqrt(np.mean(day_returns.clip(-np.inf, 0)**2)) * np.sqrt(annual_trading_days)) # noqa: E501
123123
max_dd = -np.nan_to_num(dd.max())
124-
s.loc['Calmar Ratio'] = np.clip(annualized_return / (-max_dd or np.nan), 0, np.inf)
124+
s.loc['Calmar Ratio'] = annualized_return / (-max_dd or np.nan)
125125
s.loc['Max. Drawdown [%]'] = max_dd * 100
126126
s.loc['Avg. Drawdown [%]'] = -dd_peaks.mean() * 100
127127
s.loc['Max. Drawdown Duration'] = _round_timedelta(dd_dur.max())
@@ -137,6 +137,8 @@ def _round_timedelta(value, _period=_data_period(index)):
137137
s.loc['Profit Factor'] = returns[returns > 0].sum() / (abs(returns[returns < 0].sum()) or np.nan) # noqa: E501
138138
s.loc['Expectancy [%]'] = returns.mean() * 100
139139
s.loc['SQN'] = np.sqrt(n_trades) * pl.mean() / (pl.std() or np.nan)
140+
win_prob = (pl > 0).sum() / n_trades
141+
s.loc['Kelly Criterion'] = win_prob - (1 - win_prob) / (pl[pl > 0].mean() / pl[pl < 0].mean()) # noqa: E501
140142

141143
s.loc['_strategy'] = strategy_instance
142144
s.loc['_equity_curve'] = equity_df

0 commit comments

Comments
 (0)