ranaroussi / quantstats

Portfolio analytics for quants, written in Python
Apache License 2.0
4.73k stars 827 forks source link

CAGR Doesn't Match Closely with Portfolio Visualizer #273

Open glformanek opened 1 year ago

glformanek commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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: @.***>