quantstats icon indicating copy to clipboard operation
quantstats copied to clipboard

CAGR Doesn't Match Closely with Portfolio Visualizer

Open glformanek opened this issue 3 years ago • 3 comments

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!

glformanek avatar Jul 06 '23 21:07 glformanek

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

gnzsnz avatar Jul 14 '23 21:07 gnzsnz

Please see my reply to issue #275.

Regards

On Fri, Jul 14, 2023 at 4:31 PM gnzsnz @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/ranaroussi/quantstats/issues/273#issuecomment-1636466247, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJWQW5BU2W7QORWEJ3EPQHTXQG3DVANCNFSM6AAAAAA2A7JMRE . You are receiving this because you authored the thread.Message ID: @.***>

glformanek avatar Jul 15 '23 17:07 glformanek

Closing this issue as it's older than 2025. CAGR calculation discrepancy issues should be addressed through new issues with version 0.0.64 if specific calculation problems are still encountered.

ranaroussi avatar Jul 18 '25 14:07 ranaroussi