From e56f75830178f5fd49bb9dbc736c4aebce3b499c Mon Sep 17 00:00:00 2001 From: Kernc Date: Wed, 27 May 2020 02:44:04 +0200 Subject: [PATCH] REF: Use geometric mean return in Sharpe/Sortino stats computation See: https://qoppac.blogspot.com/2017/02/can-you-eat-geometric-returns.html https://www.investopedia.com/ask/answers/06/geometricmean.asp --- backtesting/backtesting.py | 2 +- backtesting/test/_test.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index 326bcc3c..17d51c05 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -1285,7 +1285,7 @@ def _round_timedelta(value, _period=_data_period(index)): s.loc['Win Rate [%]'] = win_rate = np.nan if not n_trades else (pl > 0).sum() / n_trades * 100 # noqa: E501 s.loc['Best Trade [%]'] = returns.max() * 100 s.loc['Worst Trade [%]'] = returns.min() * 100 - mean_return = returns.mean() + mean_return = np.exp(np.log(1 + returns).sum() / (len(returns) or np.nan)) - 1 s.loc['Avg. Trade [%]'] = mean_return * 100 s.loc['Max. Trade Duration'] = _round_timedelta(durations.max()) s.loc['Avg. Trade Duration'] = _round_timedelta(durations.mean()) diff --git a/backtesting/test/_test.py b/backtesting/test/_test.py index fd4db1fe..6c217d53 100644 --- a/backtesting/test/_test.py +++ b/backtesting/test/_test.py @@ -253,10 +253,10 @@ def test_compute_stats(self): 'Avg. Drawdown Duration': pd.Timedelta('41 days 00:00:00'), 'Avg. Drawdown [%]': -5.925851581948801, 'Avg. Trade Duration': pd.Timedelta('46 days 00:00:00'), - 'Avg. Trade [%]': 3.097629974370268, + 'Avg. Trade [%]': 2.3537113951143773, 'Best Trade [%]': 53.59595229490424, 'Buy & Hold Return [%]': 703.4582419772772, - 'Calmar Ratio': 0.06456068720154355, + 'Calmar Ratio': 0.049055964204885415, 'Duration': pd.Timedelta('3116 days 00:00:00'), 'End': pd.Timestamp('2013-03-01 00:00:00'), 'Equity Final [$]': 51959.94999999997, @@ -269,8 +269,8 @@ def test_compute_stats(self): 'Profit Factor': 2.0880175388920286, 'Return [%]': 419.59949999999964, 'SQN': 0.916892986080858, - 'Sharpe Ratio': 0.2357610034211845, - 'Sortino Ratio': 0.7355072888872161, + 'Sharpe Ratio': 0.17914126763602636, + 'Sortino Ratio': 0.5588698138148217, 'Start': pd.Timestamp('2004-08-19 00:00:00'), 'Win Rate [%]': 46.15384615384615, 'Worst Trade [%]': -18.39887353835481,