diff --git a/alphavec/backtest.py b/alphavec/backtest.py index 1e500dc..f7e1767 100644 --- a/alphavec/backtest.py +++ b/alphavec/backtest.py @@ -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"], @@ -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, @@ -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 diff --git a/example.ipynb b/example.ipynb index 7238448..22f93f8 100644 --- a/example.ipynb +++ b/example.ipynb @@ -708,11 +708,11 @@ " BTCUSDT\n", " 0.784522\n", " 0.676503\n", - " 0.697523\n", + " 0.371469\n", " 0.766293\n", " 2.082824\n", " 0.512587\n", - " 4.004693\n", + " 1.616165\n", " 0.632456\n", " 2.944701\n", " 1.000637\n", @@ -721,11 +721,11 @@ " DOGEUSDT\n", " 1.021940\n", " 2.489353\n", - " 3.422070\n", + " 1.826904\n", " 0.923328\n", " 0.877637\n", " 1.058797\n", - " 1.134931\n", + " 0.699507\n", " 0.659074\n", " 2.331747\n", " 1.000745\n", @@ -734,11 +734,11 @@ " ETHUSDT\n", " 1.029580\n", " 0.877444\n", - " 1.421963\n", + " 0.695604\n", " 0.793027\n", " 0.688909\n", " 0.385551\n", - " 0.420768\n", + " 0.233350\n", " 0.497637\n", " 4.970033\n", " 1.000637\n", @@ -747,11 +747,11 @@ " MATICUSDT\n", " 1.409684\n", " 1.405686\n", - " 3.913546\n", + " 1.879349\n", " 0.879694\n", " 0.398154\n", " 0.615083\n", - " 0.124623\n", + " 0.081175\n", " 0.585446\n", " 5.556876\n", " 1.000708\n", @@ -773,11 +773,11 @@ " XRPUSDT\n", " 0.673812\n", " 1.135899\n", - " 0.339995\n", + " 0.190893\n", " 0.832385\n", " -0.063805\n", " 0.408337\n", - " -0.140457\n", + " -0.086424\n", " 0.659143\n", " 8.762681\n", " 1.000637\n", @@ -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", @@ -979,7 +979,7 @@ { "data": { "text/plain": [ - "portfolio 0.654103\n", + "portfolio 0.986628\n", "dtype: float64" ] }, diff --git a/setup.cfg b/setup.cfg index d8bea74..17f0e73 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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