MCPcopy
hub / github.com/ranaroussi/quantstats / metrics

Function metrics

quantstats/reports.py:721–1202  ·  view source on GitHub ↗
(
    returns,
    benchmark=None,
    rf=0.0,
    display=True,
    mode="basic",
    sep=False,
    compounded=True,
    periods_per_year=252,
    prepare_returns=True,
    match_dates=True,
    **kwargs,
)

Source from the content-addressed store, hash-verified

719
720
721def metrics(
722 returns,
723 benchmark=None,
724 rf=0.0,
725 display=True,
726 mode="basic",
727 sep=False,
728 compounded=True,
729 periods_per_year=252,
730 prepare_returns=True,
731 match_dates=True,
732 **kwargs,
733):
734
735 if match_dates:
736 returns = returns.dropna()
737 returns.index = returns.index.tz_localize(None)
738 win_year, _ = _get_trading_periods(periods_per_year)
739
740 benchmark_colname = kwargs.get("benchmark_title", "Benchmark")
741 strategy_colname = kwargs.get("strategy_title", "Strategy")
742
743 if benchmark is not None:
744 if isinstance(benchmark, str):
745 benchmark_colname = f"Benchmark ({benchmark.upper()})"
746 elif isinstance(benchmark, _pd.DataFrame) and len(benchmark.columns) > 1:
747 raise ValueError(
748 "`benchmark` must be a pandas Series, "
749 "but a multi-column DataFrame was passed"
750 )
751
752 if isinstance(returns, _pd.DataFrame):
753 if len(returns.columns) > 1:
754 blank = [""] * len(returns.columns)
755 if isinstance(strategy_colname, str):
756 strategy_colname = list(returns.columns)
757 else:
758 blank = [""]
759
760 # if isinstance(returns, _pd.DataFrame):
761 # if len(returns.columns) > 1:
762 # raise ValueError("`returns` needs to be a Pandas Series or one column DataFrame. multi colums DataFrame was passed")
763 # returns = returns[returns.columns[0]]
764
765 if prepare_returns:
766 df = _utils._prepare_returns(returns)
767
768 if isinstance(returns, _pd.Series):
769 df = _pd.DataFrame({"returns": returns})
770 elif isinstance(returns, _pd.DataFrame):
771 df = _pd.DataFrame(
772 {
773 "returns_" + str(i + 1): returns[strategy_col]
774 for i, strategy_col in enumerate(returns.columns)
775 }
776 )
777
778 if benchmark is not None:

Callers 3

htmlFunction · 0.85
fullFunction · 0.85
basicFunction · 0.85

Calls 3

_get_trading_periodsFunction · 0.85
_match_datesFunction · 0.85
_calc_ddFunction · 0.85

Tested by

no test coverage detected