quantstats
quantstats copied to clipboard
CAGR is not accurate - need to update to take the
Discussed in https://github.com/ranaroussi/quantstats/discussions/311
Originally posted by wpearce1 November 7, 2023 The current way to calculate CAGR is incorrect. The years component should be:
total_days = len(df.index)
years = total_days / 252.
it looks like the way it is currently configured it counting weekends in the numerator but not the denominator. this understates CAGR.
thanks
def cagr(returns, rf=0.0, compounded=True, periods=252): """ Calculates the communicative annualized growth return (CAGR%) of access returns
If rf is non-zero, you must specify periods.
In this case, rf is assumed to be expressed in yearly (annualized) terms
"""
total = _utils._prepare_returns(returns, rf)
if compounded:
total = comp(total)
else:
total = _np.sum(total)
**years = (returns.index[-1] - returns.index[0]).days / periods**
res = abs(total + 1.0) ** (1.0 / years) - 1
if isinstance(returns, _pd.DataFrame):
res = _pd.Series(res)
res.index = returns.columns
return res</div>
Your "total_days = len(df.index)" and "years = total_days / 252" . Is correct. However, note that the number of days in the quantstats formula years = (returns.index[-1] - returns.index[0]).days / periods is calculating a calendar number of days where len(df.index) is counting trading days. If you are counting calendar days, use 365. If counting trading days use 252.
The quantstats formula should use 365 unless it changes the method to use trading days vs calendar days. Both 252 and 365 are not quite the same for all years. It would probably be better to use 365.25 to account for leap years.
i found this issue and submited a pull request to fix it some time ago, #281
you can pull from here if it works for you https://github.com/gnzsnz/quantstats-cagr/tree/cagr
Regards
i found this issue and submited a pull request to fix it some time ago, #281
you can pull from here if it works for you https://github.com/gnzsnz/quantstats-cagr/tree/cagr
Regards
Thank you very much for your help. But how I can use your updated version in spyder now? which code I have to use? Thank you very much
I add this to my requirement.txt file
quantstats @ git+https://github.com/gnzsnz/quantstats-cagr.git@cagr
or like this on the command line
pip install git+https://github.com/gnzsnz/quantstats-cagr.git@cagr
Thank you very much for your answer. Just a clarification. I have Anaconda and I use spyder. I can write ! pip install ... Etc directly in spyder?
total_days = len(df.index)
I have a problem with that: you are assuming daily market data. Is there anywhere in the quantstats docs that say that it only works with daily data?
I do not think that the code should make that assumption.
Instead, it should look at the actual time difference of the first and last indices (which should be checked to confirm that they are times and not something like str or int) and from that difference compute the effective number of days.
Check out https://github.com/Lumiwealth/quantstats_lumi, which is being updated regularly. We are a fork of this library that is being maintained by Lumiwealth