Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAGR is not accurate - need to update to take the #317

Open
TraderMikeS opened this issue Nov 30, 2023 Discussed in #311 · 5 comments
Open

CAGR is not accurate - need to update to take the #317

TraderMikeS opened this issue Nov 30, 2023 Discussed in #311 · 5 comments

Comments

@TraderMikeS
Copy link

Discussed in #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.

@gnzsnz
Copy link

gnzsnz commented Dec 14, 2023

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

@paolobalasso
Copy link

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

@gnzsnz
Copy link

gnzsnz commented Jan 7, 2024

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

@paolobalasso
Copy link

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?

@brent-ridian
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants