pmorissette / ffn

ffn - a financial function library for Python
pmorissette.github.io/ffn
MIT License
2.03k stars 300 forks source link

YTD returns is in Error #241

Open Dabs212 opened 4 months ago

Dabs212 commented 4 months ago

image

image

QQQ Yearly returns based on Monthly Returns Do not Match!

quant12345 commented 1 month ago

@Dabs212 in my opinion, the calculation is correct. I tried to calculate separately. The first year does not count. To calculate the first year, as far as I understand, the first and last prices for the year are taken (I checked manually, the results are the same).

import pandas as pd

pd.set_option('display.max_rows', None)  # print everything rows
pd.set_option('display.max_columns', None)  # print everything columns

qqq = pd.DataFrame(data['qqq'].resample('YE').last())# dataframe with the latest price for the year
qqq['return'] = (qqq / qqq.shift(1) - 1)*100
print(qqq)

Output:

                   qqq     return
Date                             
2001-12-31   33.130779        NaN
2002-12-31   20.750368 -37.368307
2003-12-31   31.056787  49.668610
2004-12-31   34.329723  10.538553
2005-12-31   34.867130   1.565427
2006-12-31   37.358078   7.144115
2007-12-31   44.466572  19.027997
2008-12-31   25.911940 -41.727148
2009-12-31   40.081654  54.684112
2010-12-31   48.155602  20.143750
2011-12-31   49.829395   3.475803
2012-12-31   58.854736  18.112484
2013-12-31   80.415733  36.634260
2014-12-31   95.840622  19.181431
2015-12-31  104.884018   9.435869
2016-12-31  112.328575   7.097895
2017-12-31  149.019073  32.663548
2018-12-31  148.830978  -0.126222
2019-12-31  206.817886  38.961585
2020-12-31  306.930420  48.406129
2021-12-31  391.089905  27.419728
2022-12-31  263.684509 -32.577010
2023-12-31  408.330200  54.855589
2024-12-31  496.160004  21.509505

And even if we calculation manually: (20.750368/33.130779 -1) * 100 = −37,368306372

And in your table of monthly returns: 2001-09-30 the positive number should be the opposite and 2002-11-30 the negative should be positive. I haven't looked at other years.