ranaroussi / quantstats

Portfolio analytics for quants, written in Python
Apache License 2.0
4.73k stars 827 forks source link

Possible Calculation Error in CAGR Calculation #291

Open zntech opened 1 year ago

zntech commented 1 year ago

The tool is very useful, but there might be an error in the calculated CAGR. Your algorithm is based on subtracting the start and end dates of the sequence using calendar days. However, you're using 252 trading days per year to calculate the interval length, which leads to an incorrect number of years due to the following calculation using the variable "periods" (which, based on your function's default parameters, is set to 252):

years = (returns.index[-1] - returns.index[0]).days / periods

I believe the correct approach should involve directly calculating the interval length in terms of calendar days to determine the number of years:

years = (returns.index[-1] - returns.index[0]).days / 365

bhoang commented 1 year ago

252 days is very commonly used in finance for trading.

quant2008 commented 1 year ago

I think zntech is correct. If our resurns senquence has date index, we should use 365, if our returns sequence has no index, then we use 252.

yarimiz commented 1 year ago

@zntech is right. When calculating CAGR, you are using calendar year of 365 days (see https://www.investopedia.com/terms/c/cagr.asp#:~:text=To%20calculate%20the%20CAGR%20of,one%20from%20the%20subsequent%20result.)

Using 252 periods is wrong and will produce misleading CAGR.

For a simple example: Say the between 2022-01-01 and 2022-12-31, I turned $100 to $200. My return is 100% and my CAGR is 100% Using 252 periods, my CAGR would be 61.83%, which is totally wrong.

I created https://github.com/ranaroussi/quantstats/pull/302 to fix it @ranaroussi

gnzsnz commented 10 months ago

this is the same than this PR

the fix is really simple. and already implemented in https://github.com/gnzsnz/quantstats-cagr/tree/cagr

if you need a quick fix you can do

pip install --upgrade --no-cache-dir git+https://github.com/gnzsnz/quantstats-cagr.git@cagr