コード例 #1
0
 def test_information_ratio(self):
     res_a = empyrical.excess_sharpe(ret['a'], benchmark_rets['a'])
     res_b = empyrical.excess_sharpe(ret['b'], benchmark_rets['b'])
     res_c = empyrical.excess_sharpe(ret['c'], benchmark_rets['c'])
     assert isclose(
         ret['a'].vbt.returns.information_ratio(benchmark_rets['a']), res_a)
     pd.testing.assert_series_equal(
         ret.vbt.returns.information_ratio(benchmark_rets),
         pd.Series([res_a, res_b, res_c], index=ret.columns))
コード例 #2
0
 def test_information_ratio(self):
     res_a = empyrical.excess_sharpe(ret['a'], benchmark_rets['a'])
     res_b = empyrical.excess_sharpe(ret['b'], benchmark_rets['b'])
     res_c = empyrical.excess_sharpe(ret['c'], benchmark_rets['c'])
     assert isclose(
         ret['a'].vbt.returns.information_ratio(benchmark_rets['a']), res_a)
     pd.testing.assert_series_equal(
         ret.vbt.returns.information_ratio(benchmark_rets),
         pd.Series([res_a, res_b, res_c],
                   index=ret.columns).rename('information_ratio'))
     pd.testing.assert_series_equal(
         ret.vbt.returns.rolling_information_ratio(ret.shape[0],
                                                   benchmark_rets,
                                                   minp=1).iloc[-1],
         pd.Series([res_a, res_b, res_c],
                   index=ret.columns).rename(ret.index[-1]))
コード例 #3
0
ファイル: backtest.py プロジェクト: yjy-1999/MyQuant
 def _info(self):
     self.__info = ey.excess_sharpe(returns = self.__returns, factor_returns = self.__benchReturns)
コード例 #4
0
def report_metrics(strategy_rets, benchmark_rets, factor_returns=0):
    """使用 `empyrical`_ 库计算各种常见财务风险和绩效指标。

    Args:
        strategy_rets (:py:class:`pandas.Series`): 策略收益。
        benchmark_rets (:py:class:`pandas.Series`): 基准收益。
        factor_returns : 计算 excess_sharpe 时使用,策略计算时使用`strategy_rets`作为`factor_returns`,
            当不存在`strategy_rets`时使用`factor_returns`。
            `factor_returns`参考 :py:func:`empyrical.excess_sharpe` 中的`factor_returns`参数的解释。

    Examples:
        >>> from finance_tools_py._jupyter_helper import report_metrics
        >>> import pandas as pd
        >>> rep = report_metrics(pd.Series([-0.01,0.04,0.03,-0.02]),
                                 pd.Series([0.04,0.05,0.06,0.07]))
        >>> print(rep)
                                  基准         策略
        最大回撤                0.000000  -0.020000
        年化收益           713630.025679  10.326756
        年度波动性               0.204939   0.467333
        夏普比率               67.629875   5.392302
        R平方                 0.994780   0.614649
        盈利比率                1.650602   2.081081
        excess_sharpe       4.260282  -1.317465
        年复合增长率         713630.025679  10.326756


    Returns:
        :py:class:`pandas.DataFrame`:

    .. _empyrical:
        http://quantopian.github.io/empyrical/

    """
    if not benchmark_rets.empty:
        max_drawdown_benchmark = empyrical.max_drawdown(benchmark_rets)
        annual_return_benchmark = empyrical.annual_return(benchmark_rets)
        annual_volatility_benchmark = empyrical.annual_volatility(
            benchmark_rets)
        sharpe_ratio_benchmark = empyrical.sharpe_ratio(benchmark_rets)
        stability_of_timeseries_benchmark = empyrical.stability_of_timeseries(
            benchmark_rets)
        tail_ratio_benchmark = empyrical.tail_ratio(benchmark_rets)
        excess_sharpe_benchmark = empyrical.excess_sharpe(
            benchmark_rets, factor_returns)
        cagr_benchmark = empyrical.cagr(benchmark_rets)
    else:
        max_drawdown_benchmark = None
        annual_return_benchmark = None
        annual_volatility_benchmark = None
        sharpe_ratio_benchmark = None
        stability_of_timeseries_benchmark = None
        tail_ratio_benchmark = None
        excess_sharpe_benchmark = None
        cagr_benchmark = None
    max_drawdown_strategy = empyrical.max_drawdown(strategy_rets)
    annual_return_strategy = empyrical.annual_return(strategy_rets)
    annual_volatility_strategy = empyrical.annual_volatility(strategy_rets)
    sharpe_ratio_strategy = empyrical.sharpe_ratio(strategy_rets)
    stability_of_timeseries_strategy = empyrical.stability_of_timeseries(
        strategy_rets)
    tail_ratio_strategy = empyrical.tail_ratio(strategy_rets)
    excess_sharpe_strategy = empyrical.excess_sharpe(
        strategy_rets,
        benchmark_rets if not benchmark_rets.empty else factor_returns)
    cagr_strategy = empyrical.cagr(strategy_rets)

    return pd.DataFrame(
        {
            '基准': [
                max_drawdown_benchmark, annual_return_benchmark,
                annual_volatility_benchmark, sharpe_ratio_benchmark,
                stability_of_timeseries_benchmark, tail_ratio_benchmark,
                excess_sharpe_benchmark, cagr_benchmark
            ],
            '策略': [
                max_drawdown_strategy, annual_return_strategy,
                annual_volatility_strategy, sharpe_ratio_strategy,
                stability_of_timeseries_strategy, tail_ratio_strategy,
                excess_sharpe_strategy, cagr_strategy
            ]
        },
        index=[
            '最大回撤', '年化收益', '年度波动性', '夏普比率', 'R平方', '盈利比率', 'excess_sharpe',
            '年复合增长率'
        ])