Skip to content

Commit

Permalink
Merge pull request ranaroussi#170 from Amirnajafi/main
Browse files Browse the repository at this point in the history
Fix date calculation error if data resample as monthly
  • Loading branch information
ranaroussi authored Apr 23, 2022
2 parents 0a51fe0 + b1051dd commit b62499a
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions quantstats/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b62499a

Please sign in to comment.