quantstats icon indicating copy to clipboard operation
quantstats copied to clipboard

CAGR is not accurate - need to update to take the

Open TraderMikeS opened this issue 1 year ago • 6 comments

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.

TraderMikeS avatar Nov 30 '23 20:11 TraderMikeS

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

gnzsnz avatar Dec 14 '23 18:12 gnzsnz

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

paolobalasso avatar Jan 06 '24 18:01 paolobalasso

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

gnzsnz avatar Jan 07 '24 19:01 gnzsnz

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?

paolobalasso avatar Jan 07 '24 19:01 paolobalasso

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.

brent-ridian avatar Feb 20 '24 16:02 brent-ridian

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

grzesir avatar Jul 30 '24 06:07 grzesir