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 Doesn't Match Closely with Portfolio Visualizer #273

Open
glformanek opened this issue Jul 6, 2023 · 2 comments · May be fixed by #281
Open

CAGR Doesn't Match Closely with Portfolio Visualizer #273

glformanek opened this issue Jul 6, 2023 · 2 comments · May be fixed by #281

Comments

@glformanek
Copy link

I've been try to get the utils.make_index function to work in Quantstats. First, I cannot get the rebalance parameter to work. Leaving the default value gives an error, so I have to set it to 'None'. The error message:
File "C:\Users\Techie\PycharmProjects\StockAnalysis\venv\Lib\site-packages\quantstats\utils.py", line 400, in make_index
return index[index.index <= last_day].sum(axis=1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Second, when I set rebalance='None', it will work. But, when I run the reports.html function I get CAGR results that are very different from Portfolio Visualizer. Is there another parameter I need to tweak to match up with PV? Here is my code:

import matplotlib
import quantstats as qs
matplotlib.use('Agg')

qs.extend_pandas()

symbols = {
'SPY': 0.40,
'IEF': 0.40,
'TLT': 0.20
}

port = qs.utils.make_index(symbols, rebalance=None)
qs.reports.html(port, benchmark='SPY', title='Strategy vs. SPY', output='port.html')

Here is a link to the same portfolio when run in Portfolio Visualizer: https://tinyurl.com/2mbsvp4c
I realize PV does it monthly but I'm off by almost 5%. Plus, I cannot do monthly in Quantstats due to the error above on rebalance.
The results vary by quite a bit. I'm adding a screenshot of the results for the Quantstats run. Maybe I have something wrong?

quantstats

Thanks!

@gnzsnz
Copy link

gnzsnz commented Jul 14, 2023

Hi,

I'm facing the same issue with CAGR. and I think that i have found the problem.

the number of years is calculated as

(shortvol.returns.index[-1] - shortvol.returns.index[0]).days/252

the problem with this is that returns a wrong number of years. ex

dt.datetime(2023,7,6) - dt.datetime(2005,12,20)

datetime.timedelta(days=6407)

now 6407/252=25.42 which is wrong.

however 6407/365 = 17.55 which is correct

the fix is quite easy. probably just remove the periods=252parameter and hardcore 365 in the formula. the formula is working in days already, so having a periods parameter can only lead to problems. it should always be 365.

this has a great side effect, now all my backtests will have a higher CAGR 🤪
it's a bit late here, i'll try to submit a pull request in the next few days

@glformanek
Copy link
Author

glformanek commented Jul 15, 2023 via email

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

Successfully merging a pull request may close this issue.

2 participants