Skip to content

Commit 921c931

Browse files
committed
REF: Fix geometric mean computation so that no warnings are issued
Fixes #168 (comment)
1 parent b4d04dc commit 921c931

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

backtesting/backtesting.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,8 +1392,10 @@ def _round_timedelta(value, _period=_data_period(index)):
13921392
c = data.Close.values
13931393
s.loc['Buy & Hold Return [%]'] = (c[-1] - c[0]) / c[0] * 100 # long-only return
13941394

1395-
def geometric_mean(x):
1396-
return np.exp(np.log(1 + x).sum() / (len(x) or np.nan)) - 1
1395+
def geometric_mean(returns):
1396+
returns = returns.fillna(0) + 1
1397+
return (0 if np.any(returns <= 0) else
1398+
np.exp(np.log(returns).sum() / (len(returns) or np.nan)) - 1)
13971399

13981400
day_returns = gmean_day_return = annual_trading_days = np.array(np.nan)
13991401
if index.is_all_dates:

0 commit comments

Comments
 (0)