Skip to content

Remove flooring on ratios & add Kelly Criterion #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2022
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Avg. Trade Duration 32 days 00:00:00
Profit Factor 2.13
Expectancy [%] 6.91
SQN 1.78
Kelly Criterion 0.6134
_strategy SmaCross(n1=10, n2=20)
_equity_curve Equ...
_trades Size EntryB...
Expand Down
8 changes: 5 additions & 3 deletions backtesting/_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ def _round_timedelta(value, _period=_data_period(index)):

# Our Sharpe mismatches `empyrical.sharpe_ratio()` because they use arithmetic mean return
# and simple standard deviation
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
s.loc['Sharpe Ratio'] = (s.loc['Return (Ann.) [%]'] - risk_free_rate) / (s.loc['Volatility (Ann.) [%]'] or np.nan) # noqa: E501
# Our Sortino mismatches `empyrical.sortino_ratio()` because they use arithmetic mean return
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
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
max_dd = -np.nan_to_num(dd.max())
s.loc['Calmar Ratio'] = np.clip(annualized_return / (-max_dd or np.nan), 0, np.inf)
s.loc['Calmar Ratio'] = annualized_return / (-max_dd or np.nan)
s.loc['Max. Drawdown [%]'] = max_dd * 100
s.loc['Avg. Drawdown [%]'] = -dd_peaks.mean() * 100
s.loc['Max. Drawdown Duration'] = _round_timedelta(dd_dur.max())
Expand All @@ -137,6 +137,8 @@ def _round_timedelta(value, _period=_data_period(index)):
s.loc['Profit Factor'] = returns[returns > 0].sum() / (abs(returns[returns < 0].sum()) or np.nan) # noqa: E501
s.loc['Expectancy [%]'] = returns.mean() * 100
s.loc['SQN'] = np.sqrt(n_trades) * pl.mean() / (pl.std() or np.nan)
win_prob = (pl > 0).sum() / n_trades
s.loc['Kelly Criterion'] = win_prob - (1 - win_prob) / (pl[pl > 0].mean() / pl[pl < 0].mean()) # noqa: E501

s.loc['_strategy'] = strategy_instance
s.loc['_equity_curve'] = equity_df
Expand Down
1 change: 1 addition & 0 deletions backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ def run(self, **kwargs) -> pd.Series:
Profit Factor 2.08802
Expectancy [%] 8.79171
SQN 0.916893
Kelly Criterion 0.6134
_strategy SmaCross
_equity_curve Eq...
_trades Size EntryB...
Expand Down
1 change: 1 addition & 0 deletions backtesting/test/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def test_compute_stats(self):
'Return [%]': 414.2298999999996,
'Volatility (Ann.) [%]': 36.49390889140787,
'SQN': 1.0766187356697705,
'Kelly Criterion': 0.7875234266909678,
'Sharpe Ratio': 0.5803778344714113,
'Sortino Ratio': 1.0847880675854096,
'Start': pd.Timestamp('2004-08-19 00:00:00'),
Expand Down