diff --git a/quantstats/reports.py b/quantstats/reports.py index 7d29e317..5ac27be4 100644 --- a/quantstats/reports.py +++ b/quantstats/reports.py @@ -31,6 +31,7 @@ __version__, stats as _stats, utils as _utils, plots as _plots ) +from dateutil.relativedelta import relativedelta try: from IPython.core.display import ( @@ -486,34 +487,28 @@ def metrics(returns, benchmark=None, rf=0., display=True, comp_func = _stats.comp if compounded else _np.sum today = df.index[-1] # _dt.today() - metrics['MTD %'] = comp_func( - df[df.index >= _dt(today.year, today.month, 1)]) * pct - - d = today - _td(3*365/12) - metrics['3M %'] = comp_func( - df[df.index >= _dt(d.year, d.month, d.day)]) * pct - - d = today - _td(6*365/12) - metrics['6M %'] = comp_func( - df[df.index >= _dt(d.year, d.month, d.day)]) * pct - + metrics['MTD %'] = comp_func(df[df.index >= _dt(today.year, today.month, 1)]) * pct + + d = today - relativedelta(months=3) + metrics['3M %'] = comp_func(df[df.index >= d]) * pct + + d = today - relativedelta(months=6) + metrics['6M %'] = comp_func(df[df.index >= d]) * pct + metrics['YTD %'] = comp_func(df[df.index >= _dt(today.year, 1, 1)]) * pct - - d = today - _td(12*365/12) - metrics['1Y %'] = comp_func( - df[df.index >= _dt(d.year, d.month, d.day)]) * pct - d = today - _td(3*365) - metrics['3Y (ann.) %'] = _stats.cagr( - df[df.index >= _dt(d.year, d.month, d.day) - ], 0., compounded) * pct - d = today - _td(5*365) - metrics['5Y (ann.) %'] = _stats.cagr( - df[df.index >= _dt(d.year, d.month, d.day) - ], 0., compounded) * pct - d = today - _td(10*365) - metrics['10Y (ann.) %'] = _stats.cagr( - df[df.index >= _dt(d.year, d.month, d.day) - ], 0., compounded) * pct + + d = today - relativedelta(years=1) + metrics['1Y %'] = comp_func(df[df.index >= d]) * pct + + d = today - relativedelta(months=35) + metrics['3Y (ann.) %'] = _stats.cagr(df[df.index >= d], 0., compounded) * pct + + d = today - relativedelta(months=59) + metrics['5Y (ann.) %'] = _stats.cagr(df[df.index >= d], 0., compounded) * pct + + d = today - relativedelta(years=10) + metrics['10Y (ann.) %'] = _stats.cagr(df[df.index >= d], 0., compounded) * pct + metrics['All-time (ann.) %'] = _stats.cagr(df, 0., compounded) * pct # best/worst