Skip to content

Commit 04b2217

Browse files
committed
BUG: Fix sign in Kelly Criterion (fixup d47185f)
#640 (comment)
1 parent e4b16f9 commit 04b2217

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

backtesting/_stats.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ def _round_timedelta(value, _period=_data_period(index)):
128128
s.loc['Max. Drawdown Duration'] = _round_timedelta(dd_dur.max())
129129
s.loc['Avg. Drawdown Duration'] = _round_timedelta(dd_dur.mean())
130130
s.loc['# Trades'] = n_trades = len(trades_df)
131-
s.loc['Win Rate [%]'] = np.nan if not n_trades else (pl > 0).sum() / n_trades * 100 # noqa: E501
131+
win_rate = np.nan if not n_trades else (pl > 0).mean()
132+
s.loc['Win Rate [%]'] = win_rate * 100
132133
s.loc['Best Trade [%]'] = returns.max() * 100
133134
s.loc['Worst Trade [%]'] = returns.min() * 100
134135
mean_return = geometric_mean(returns)
@@ -138,8 +139,7 @@ def _round_timedelta(value, _period=_data_period(index)):
138139
s.loc['Profit Factor'] = returns[returns > 0].sum() / (abs(returns[returns < 0].sum()) or np.nan) # noqa: E501
139140
s.loc['Expectancy [%]'] = returns.mean() * 100
140141
s.loc['SQN'] = np.sqrt(n_trades) * pl.mean() / (pl.std() or np.nan)
141-
win_prob = (pl > 0).sum() / n_trades
142-
s.loc['Kelly Criterion'] = win_prob - (1 - win_prob) / (pl[pl > 0].mean() / pl[pl < 0].mean()) # noqa: E501
142+
s.loc['Kelly Criterion'] = win_rate - (1 - win_rate) / (pl[pl > 0].mean() / -pl[pl < 0].mean())
143143

144144
s.loc['_strategy'] = strategy_instance
145145
s.loc['_equity_curve'] = equity_df

backtesting/test/_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def test_compute_stats(self):
276276
'Return [%]': 414.2298999999996,
277277
'Volatility (Ann.) [%]': 36.49390889140787,
278278
'SQN': 1.0766187356697705,
279-
'Kelly Criterion': 0.7875234266909678,
279+
'Kelly Criterion': 0.1518705127029717,
280280
'Sharpe Ratio': 0.5803778344714113,
281281
'Sortino Ratio': 1.0847880675854096,
282282
'Start': pd.Timestamp('2004-08-19 00:00:00'),

0 commit comments

Comments
 (0)