ranaroussi / quantstats

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

Reports not passing period parameter to metrics #284

Open farrellhung opened 1 year ago

farrellhung commented 1 year ago

In reports.py, the function metrics have the parameter periods_per_year=252. But this parameter is not passed down to CAGR calculation: metrics["CAGR﹪%"] = _stats.cagr(df, rf, compounded) * pct in line 839.

Thus, when using the quantstats.reports function (full/html/basic) with non-default periods_per_year parameter (e.g. 365), the CAGR calculation will be incorrect.

Similarly on Calmar Ratio calculation, where in line 926 of reports.py is written as follow: metrics["Calmar"] = _stats.calmar(df, prepare_returns=False) No periods_per_year parameter is passed, making the Calmar Ratio calculation becomes incorrect.

The function stats.calmar also does not expect to receive periods parameter, but calmar ratio calculation requires CAGR. On stats.py line 572:

def calmar(returns, prepare_returns=True):
    """Calculates the calmar ratio (CAGR% / MaxDD%)"""
    if prepare_returns:
        returns = _utils._prepare_returns(returns)
    cagr_ratio = cagr(returns)
    max_dd = max_drawdown(returns)
    return cagr_ratio / abs(max_dd)

Besides the periods parameter is not passed to CAGR calculation, the stats.calmar function is not even expecting the parameter.

To sum up, here is my proposal:

  1. Add periods_per_year to be passed down to CAGR calculation in qs.reports.metrics
  2. Add the parameter periods in qs.stats.calmar
  3. In qs.stats.calmar, add periods parameter to be passed down to CAGR calculation

This will make the reports give more accurate results for non-default periods_per_year parameter. Thank you.