Skip to content
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ build/*
*~*

.venv/

.DS_Store
**/.DS_Store
7 changes: 7 additions & 0 deletions backtesting/_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ def _round_timedelta(value, _period=_data_period(index)):
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'] = annualized_return / (-max_dd or np.nan)
equity_log_returns = np.log(equity[1:] / equity[:-1])
market_log_returns = np.log(c[1:] / c[:-1])
cov_matrix = np.cov(equity_log_returns, market_log_returns)
beta = cov_matrix[0, 1] / cov_matrix[1, 1]
alpha = (s.loc['Return [%]'] - risk_free_rate * 100) - beta * (s.loc['Buy & Hold Return [%]'] - risk_free_rate * 100)
s.loc['Alpha [%]'] = alpha
s.loc['Beta'] = beta
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 Down
2 changes: 2 additions & 0 deletions backtesting/test/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ def test_compute_stats(self):
'Start': pd.Timestamp('2004-08-19 00:00:00'),
'Win Rate [%]': 46.96969696969697,
'Worst Trade [%]': -18.39887353835481,
'Alpha [%]': 394.37391142027462,
'Beta': 0.03803390709192,
})

def almost_equal(a, b):
Expand Down
Loading