Skip to content

Commit

Permalink
fix cagr calc
Browse files Browse the repository at this point in the history
  • Loading branch information
richklee committed Jun 9, 2024
1 parent fb0cc75 commit 74565cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
13 changes: 5 additions & 8 deletions alphavec/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def backtest(
asset_rets, periods=freq_year, risk_free_rate=ann_risk_free_rate
),
_ann_vol(asset_rets, periods=freq_year),
_cagr(asset_rets, periods=freq_year),
asset_rets.apply(_cagr, periods=freq_year),
_max_drawdown(asset_rets),
],
keys=["annual_sharpe", "annual_volatility", "cagr", "max_drawdown"],
Expand Down Expand Up @@ -183,7 +183,7 @@ def backtest(
strat_rets, periods=freq_year, risk_free_rate=ann_risk_free_rate
),
_ann_vol(strat_rets, periods=freq_year),
_cagr(strat_rets, periods=freq_year),
strat_rets.apply(_cagr, periods=freq_year),
_max_drawdown(strat_rets),
strat_ann_turnover,
_trade_count(weights) / strat_total_days,
Expand Down Expand Up @@ -289,15 +289,12 @@ def _ann_roll_sharpe(
return sr * np.sqrt(periods)


def _cagr(
rets: pd.DataFrame | pd.Series, periods: int = DEFAULT_TRADING_DAYS_YEAR
) -> pd.DataFrame | pd.Series | None:
def _cagr(rets: pd.Series, periods: int = DEFAULT_TRADING_DAYS_YEAR) -> float:
cumprod = (1 + rets).cumprod().dropna()
if len(cumprod) == 0:
return None
if cumprod.empty:
return None # type: ignore

final = cumprod.iloc[-1]

n = len(cumprod) / periods
cagr = final ** (1 / n) - 1

Expand Down
42 changes: 21 additions & 21 deletions example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,11 @@
" <th>BTCUSDT</th>\n",
" <td>0.784522</td>\n",
" <td>0.676503</td>\n",
" <td>0.697523</td>\n",
" <td>0.371469</td>\n",
" <td>0.766293</td>\n",
" <td>2.082824</td>\n",
" <td>0.512587</td>\n",
" <td>4.004693</td>\n",
" <td>1.616165</td>\n",
" <td>0.632456</td>\n",
" <td>2.944701</td>\n",
" <td>1.000637</td>\n",
Expand All @@ -721,11 +721,11 @@
" <th>DOGEUSDT</th>\n",
" <td>1.021940</td>\n",
" <td>2.489353</td>\n",
" <td>3.422070</td>\n",
" <td>1.826904</td>\n",
" <td>0.923328</td>\n",
" <td>0.877637</td>\n",
" <td>1.058797</td>\n",
" <td>1.134931</td>\n",
" <td>0.699507</td>\n",
" <td>0.659074</td>\n",
" <td>2.331747</td>\n",
" <td>1.000745</td>\n",
Expand All @@ -734,11 +734,11 @@
" <th>ETHUSDT</th>\n",
" <td>1.029580</td>\n",
" <td>0.877444</td>\n",
" <td>1.421963</td>\n",
" <td>0.695604</td>\n",
" <td>0.793027</td>\n",
" <td>0.688909</td>\n",
" <td>0.385551</td>\n",
" <td>0.420768</td>\n",
" <td>0.233350</td>\n",
" <td>0.497637</td>\n",
" <td>4.970033</td>\n",
" <td>1.000637</td>\n",
Expand All @@ -747,11 +747,11 @@
" <th>MATICUSDT</th>\n",
" <td>1.409684</td>\n",
" <td>1.405686</td>\n",
" <td>3.913546</td>\n",
" <td>1.879349</td>\n",
" <td>0.879694</td>\n",
" <td>0.398154</td>\n",
" <td>0.615083</td>\n",
" <td>0.124623</td>\n",
" <td>0.081175</td>\n",
" <td>0.585446</td>\n",
" <td>5.556876</td>\n",
" <td>1.000708</td>\n",
Expand All @@ -773,11 +773,11 @@
" <th>XRPUSDT</th>\n",
" <td>0.673812</td>\n",
" <td>1.135899</td>\n",
" <td>0.339995</td>\n",
" <td>0.190893</td>\n",
" <td>0.832385</td>\n",
" <td>-0.063805</td>\n",
" <td>0.408337</td>\n",
" <td>-0.140457</td>\n",
" <td>-0.086424</td>\n",
" <td>0.659143</td>\n",
" <td>8.762681</td>\n",
" <td>1.000637</td>\n",
Expand All @@ -790,22 +790,22 @@
" asset \\\n",
" annual_sharpe annual_volatility cagr max_drawdown \n",
"symbol \n",
"BTCUSDT 0.784522 0.676503 0.697523 0.766293 \n",
"DOGEUSDT 1.021940 2.489353 3.422070 0.923328 \n",
"ETHUSDT 1.029580 0.877444 1.421963 0.793027 \n",
"MATICUSDT 1.409684 1.405686 3.913546 0.879694 \n",
"BTCUSDT 0.784522 0.676503 0.371469 0.766293 \n",
"DOGEUSDT 1.021940 2.489353 1.826904 0.923328 \n",
"ETHUSDT 1.029580 0.877444 0.695604 0.793027 \n",
"MATICUSDT 1.409684 1.405686 1.879349 0.879694 \n",
"SOLUSDT 0.784384 1.216761 0.250883 0.962695 \n",
"XRPUSDT 0.673812 1.135899 0.339995 0.832385 \n",
"XRPUSDT 0.673812 1.135899 0.190893 0.832385 \n",
"\n",
" strategy \\\n",
" annual_sharpe annual_volatility cagr max_drawdown, \n",
"symbol \n",
"BTCUSDT 2.082824 0.512587 4.004693 0.632456 \n",
"DOGEUSDT 0.877637 1.058797 1.134931 0.659074 \n",
"ETHUSDT 0.688909 0.385551 0.420768 0.497637 \n",
"MATICUSDT 0.398154 0.615083 0.124623 0.585446 \n",
"BTCUSDT 2.082824 0.512587 1.616165 0.632456 \n",
"DOGEUSDT 0.877637 1.058797 0.699507 0.659074 \n",
"ETHUSDT 0.688909 0.385551 0.233350 0.497637 \n",
"MATICUSDT 0.398154 0.615083 0.081175 0.585446 \n",
"SOLUSDT 0.533112 0.477024 0.174253 0.459495 \n",
"XRPUSDT -0.063805 0.408337 -0.140457 0.659143 \n",
"XRPUSDT -0.063805 0.408337 -0.086424 0.659143 \n",
"\n",
" \n",
" annual_turnover trades_per_day \n",
Expand Down Expand Up @@ -979,7 +979,7 @@
{
"data": {
"text/plain": [
"portfolio 0.654103\n",
"portfolio 0.986628\n",
"dtype: float64"
]
},
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = alphavec
version = 0.1.1
version = 0.1.2
author = Rich Klee @ TCG
author_email=hello@thecolngroup.com
url = https://github.com/thecolngroup/alphavec
Expand Down

0 comments on commit 74565cc

Please sign in to comment.