Ejemplo n.º 1
0
 def range_maxdrawdown(self, start, end, rel=True):
     try:
         if rel:
             return stats.max_drawdown(self.active_return.loc[start:end])
         else:
             return stats.max_drawdown(self.portfolio_return.loc[start:end])
     except:
         return np.nan
Ejemplo n.º 2
0
    def get_summary(self, freq='m'):
        if freq == 'm':
            monthlyRet = self.MonthlyRet(False)
            monthlyBenchmarkRet = self.BenchmarkMonthlyRet()
            hedgeMonthlyRet = self.MonthlyRet()

            _dict = {
                'annual_return':
                stats.annual_return(hedgeMonthlyRet, annualization=12),
                'annual_vol':
                stats.annual_volatility(hedgeMonthlyRet, annualization=12),
                'IR':
                stats.information_ratio(monthlyRet, monthlyBenchmarkRet),
                't_stats':
                stats_scp.ttest_1samp(hedgeMonthlyRet, 0)[0],
                'win_rate':
                stats.win_rate(monthlyRet, monthlyBenchmarkRet),
                'max_drawdown':
                stats.max_drawdown(hedgeMonthlyRet),
                'drawn_down_start':
                pf.timeseries.get_top_drawdowns(hedgeMonthlyRet, 1)[0][0],
                'draw_down_end':
                pf.timeseries.get_top_drawdowns(hedgeMonthlyRet, 1)[0][1],
            }
        else:
            _dict = {
                'annual_return':
                stats.annual_return(self.activeRet, annualization=12),
                'annual_vol':
                stats.annual_volatility(self.activeRet, annualization=12),
                'IR':
                stats.information_ratio(self.Ret, self.BenchmarkRet),
                't_stats':
                stats_scp.ttest_1samp(self.activeRet, 0)[0],
                'win_rate':
                stats.win_rate(self.Ret, self.BenchmarkRet),
                'max_drawdown':
                stats.max_drawdown(self.activeRet),
                'drawn_down_start':
                pf.timeseries.get_top_drawdowns(self.activeRet, 1)[0],
                'draw_down_end':
                pf.timeseries.get_top_drawdowns(self.activeRet, 1)[1],
            }
        return pd.Series(_dict)
Ejemplo n.º 3
0
    def YearlyPerformance(self):
        monthlyRet = self.MonthlyRet(False)  # 单边做多模型月度收益率
        benchmarkMonthlyRet = self.BenchmarkMonthlyRet()
        hedgeMonthlyRet = self.MonthlyRet(True)  # 对冲组合月收益率

        a = stats.aggregate_returns(monthlyRet, 'yearly')  # 多头组合分年收益
        b = stats.aggregate_returns(benchmarkMonthlyRet, 'yearly')  # 基准组合分年收益

        _l = []
        for i, year in enumerate(a.index):
            hedgeMonthlyRet_current_year = hedgeMonthlyRet.ix[str(year)]
            monthlyRet_current_year = monthlyRet.ix[str(year)]
            benchmarkMonthlyRet_current_year = benchmarkMonthlyRet.ix[str(
                year)]

            hdSharp_current_year = stats.sharpe_ratio(
                hedgeMonthlyRet_current_year, annualization=12)
            hdMaxDown_current_year = stats.max_drawdown(
                hedgeMonthlyRet_current_year)
            hdReturn_current_year = stats.annual_return(
                hedgeMonthlyRet_current_year, annualization=12)
            hdWinRate_current_year = stats.win_rate(
                monthlyRet_current_year, benchmarkMonthlyRet_current_year)
            _l.append([
                hdSharp_current_year, hdReturn_current_year,
                hdMaxDown_current_year, hdWinRate_current_year
            ])
        # 计算全年收益表现
        hdSharp_all = stats.sharpe_ratio(hedgeMonthlyRet, annualization=12)
        hdMaxDown_all = stats.max_drawdown(hedgeMonthlyRet)
        hdReturn_all = stats.annual_return(hedgeMonthlyRet, annualization=12)
        hdWinRate_all = stats.win_rate(hedgeMonthlyRet, benchmarkMonthlyRet)
        _l.append([hdSharp_all, hdReturn_all, hdMaxDown_all, hdWinRate_all])
        result = pd.DataFrame(_l,
                              columns=['夏普比率', '年化收益', '最大回撤', '胜率'],
                              index=list(a.index) + ['All'])
        return result
Ejemplo n.º 4
0
 def total_performance(self):
     """策略全局表现"""
     win_rate_func = lambda x, y: stats.win_rate(
         x, factor_returns=0, period=y)
     df = pd.DataFrame(
         [[
             self.abs_total_return, self.total_benchmark_return,
             self.rel_total_return
         ],
          [
              self.abs_maxdrawdown,
              stats.max_drawdown(self.benchmark_return),
              self.rel_maxdrawdown
          ],
          [
              self.abs_annual_volatility,
              stats.annual_volatility(self.benchmark_return),
              self.rel_annual_volatility
          ],
          [
              self.abs_annual_return,
              stats.annual_return(self.benchmark_return),
              self.rel_annual_return
          ],
          [
              self.abs_sharp_ratio,
              stats.sharpe_ratio(self.benchmark_return,
                                 simple_interest=True), self.rel_sharp_ratio
          ],
          [
              win_rate_func(self.portfolio_return, 'monthly'),
              win_rate_func(self.benchmark_return, 'monthly'),
              win_rate_func(self.active_return, 'monthly')
          ]],
         columns=['portfolio', 'benchmark', 'hedge'],
         index=[
             'total_return', 'maxdd', 'volatility', 'annual_return',
             'sharp', 'win_rate'
         ])
     return df
Ejemplo n.º 5
0
    def calc(self, df):
        print('计算绩效')
        df['equity'] = (df['returns'] + 1).cumprod()
        df['bench_equity'] = (df['bench_returns'] + 1).cumprod()

        self.period_return = df['equity'][-1] - 1
        self.benchmark_return = df['bench_equity'][-1] - 1

        self.trading_days = len(df)  #交易天数
        self.annu_return = self.period_return * 252 / self.trading_days
        self.bench_annu_return = self.benchmark_return * 252 / self.trading_days

        # 波动率
        self.volatility = stats.annual_volatility(df['returns'])

        # 夏普比率
        self.sharpe = stats.sharpe_ratio(df['returns'])
        # 最大回撤
        self.max_drawdown = stats.max_drawdown(df['returns'].values)

        #信息比率
        # self.information = stats.information_ratio(df['returns'].values,df['benchmark_returns'].values)

        self.alpha, self.beta = stats.alpha_beta_aligned(
            df['returns'].values, df['bench_returns'].values)

        return {
            'returns': self.period_return,
            'annu_returns': self.annu_return,
            'bench_returns': self.benchmark_return,
            'bench_annu_returns': self.bench_annu_return,
            'trading_days': self.trading_days,
            'max_drawdown': self.max_drawdown,
            'volatility': self.volatility,
            'sharpe': self.sharpe,
            'alpha': self.alpha,
            'beta': self.beta
        }
Ejemplo n.º 6
0
 def MaxDrawDown(self, hedge=True):
     ret = self.activeRet if hedge else self.Ret
     return stats.max_drawdown(ret)
Ejemplo n.º 7
0
 def rel_maxdrawdown(self):
     return stats.max_drawdown(self.active_return)
Ejemplo n.º 8
0
 def abs_maxdrawdown(self):
     return stats.max_drawdown(self.portfolio_return)