请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3353365 新帖:11

风格切换PB-ROE策略与多策略并行回测

专门套利发表于:5 月 10 日 16:05回复(1)

风格切换下的PB-ROE研究策略构建


本篇策略是基于上一篇帖子, 风格切换下的PB-ROE研究的内容,按照研究的思路在策略回测里面进行模型搭建,具体如下:

  • 回测搭建设置
  • 分组为4的组合收益情况
  • 分组为6的组合收益情况
  • 不同参数设置的收益情况

1.回测搭建设置

  • 参考量化课堂提供的多回测运行和参数分析框架工具函数中进行设置,这里注意一下原贴中工具函数有几个小问题修改下即可

    1. benchmark_id设置为None
      1-1.png1-3.png
    2. 调换一下提示文字
      1-2.png
  • 按照帖子中的方法将编译运行界面里面的algorithmId粘贴到参数设置那里,比如该策略中设置了3个变量,设置好之后运行代码

    param_names = ['mkt_cap','limit','trade_rate'],
    param_values =[['A','B','AB'],['C','D'],['E','F']
    
  • 这个策略里面共十二种组合,生成的 Data Frame 如下
    1.png
  • 在调用多并行回测的时候,从2005年开始,在时间较早的时候会偶尔会出现这个情况,策略里为了避免聚类分组失败导致回测失败,在这种情况下采取了全市场打分排序
    e.png

2.分组为4的组合收益情况

  • 分组回测的各项指标,比如收益率、夏普比率、最大回撤数据部分展示如下
    2-1.png
  • 下面是分组回报率折线图
    2-2.png
  • 超额收益率图
    2-3.png
  • 回测的4个主要指标,包括总回报率、最大回撤夏普率和波动
    2-4.png
  • 年化回报和最大回撤,正负双色显示
    2-5.png

3.分组为6的组合收益情况

  • 下面是分组回报图示
    3-1.png

  • 加入市值标签后,分组的结果收益整体比之前高了很多,但是近期的表现很糟糕,大概率是因为市值因子的暴露造成的

  • 其他的指标数据可以参考研究里面的内容

4.设置不同参数的收益情况

  • 设置了PE、ROE的选取阈值,分组略多,从图上都已经看不清楚哪组最好了,回测分组收益展示如下
    4-1.png

综合来看,目前收益最好的应该是BDF组合,偏好中小市值,调仓周期较长,且做ROE、PE限制,如果为了优化收益还可以进一步对持股个数进行参数设置。

#1 先导入所需要的程序包
import datetime
import numpy as np
import pandas as pd
import time
from jqdata import *
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
import itertools
import copy
import pickle

# 定义类'参数分析'
class parameter_analysis(object):
    
    # 定义函数中不同的变量
    def __init__(self, algorithm_id=None):
        self.algorithm_id = algorithm_id            # 回测id
        
        self.params_df = pd.DataFrame()             # 回测中所有调参备选值的内容,列名字为对应修改面两名称,对应回测中的 g.XXXX
        self.results = {}                           # 回测结果的回报率,key 为 params_df 的行序号,value 为
        self.evaluations = {}                       # 回测结果的各项指标,key 为 params_df 的行序号,value 为一个 dataframe
        self.backtest_ids = {}                      # 回测结果的 id
        
        # 新加入的基准的回测结果 id,可以默认为空 '',则使用回测中设定的基准
        self.benchmark_id = None                      
        
        self.benchmark_returns = []                 # 新加入的基准的回测回报率
        self.returns = {}                           # 记录所有回报率
        self.excess_returns = {}                    # 记录超额收益率
        self.log_returns = {}                       # 记录收益率的 log 值
        self.log_excess_returns = {}                # 记录超额收益的 log 值
        self.dates = []                             # 回测对应的所有日期
        self.excess_max_drawdown = {}               # 计算超额收益的最大回撤
        self.excess_annual_return = {}              # 计算超额收益率的年化指标
        self.evaluations_df = pd.DataFrame()        # 记录各项回测指标,除日回报率外
    
    # 定义排队运行多参数回测函数
    def run_backtest(self,                          #
                     algorithm_id=None,             # 回测策略id
                     running_max=10,                # 回测中同时巡行最大回测数量
                     start_date='2006-01-01',       # 回测的起始日期
                     end_date='2016-11-30',         # 回测的结束日期
                     frequency='day',               # 回测的运行频率
                     initial_cash='1000000',        # 回测的初始持仓金额
                     param_names=[],                # 回测中调整参数涉及的变量
                     param_values=[]                # 回测中每个变量的备选参数值
                     ):
        # 当此处回测策略的 id 没有给出时,调用类输入的策略 id
        if algorithm_id == None: algorithm_id=self.algorithm_id
        
        # 生成所有参数组合并加载到 df 中
        # 包含了不同参数具体备选值的排列组合中一组参数的 tuple 的 list
        param_combinations = list(itertools.product(*param_values))
        # 生成一个 dataframe, 对应的列为每个调参的变量,每个值为调参对应的备选值
        to_run_df = pd.DataFrame(param_combinations)
        # 修改列名称为调参变量的名字
        to_run_df.columns = param_names
        
        # 设定运行起始时间和保存格式
        start = time.time()
        # 记录结束的运行回测
        finished_backtests = {}
        # 记录运行中的回测
        running_backtests = {}
        # 计数器
        pointer = 0
        # 总运行回测数目,等于排列组合中的元素个数
        total_backtest_num = len(param_combinations)
        # 记录回测结果的回报率
        all_results = {}
        # 记录回测结果的各项指标
        all_evaluations = {}
        
        # 在运行开始时显示
        print ('【已完成|运行中|待运行】:'), 
        # 当运行回测开始后,如果没有全部运行完全的话:
        while len(finished_backtests)<total_backtest_num:
            # 显示运行、完成和待运行的回测个数
            print('[%s|%s|%s].' % (len(finished_backtests), 
                                   len(running_backtests), 
                                   (total_backtest_num-len(finished_backtests)-len(running_backtests)) )),
            # 记录当前运行中的空位数量
            to_run = min(running_max-len(running_backtests), total_backtest_num-len(running_backtests)-len(finished_backtests))
            # 把可用的空位进行跑回测
            for i in range(pointer, pointer+to_run):
                # 备选的参数排列组合的 df 中第 i 行变成 dict,每个 key 为列名字,value 为 df 中对应的值
                params = to_run_df.iloc[i].to_dict()
                # 记录策略回测结果的 id,调整参数 extras 使用 params 的内容
                backtest = create_backtest(algorithm_id = algorithm_id,
                                           start_date = start_date, 
                                           end_date = end_date, 
                                           frequency = frequency, 
                                           initial_cash = initial_cash, 
                                           extras = params, 
                                           # 再回测中把改参数的结果起一个名字,包含了所有涉及的变量参数值
                                           name = str(params)
                                           )
                # 记录运行中 i 回测的回测 id
                running_backtests[i] = backtest
            # 计数器计数运行完的数量    
            pointer = pointer+to_run
            
            # 获取回测结果
            failed = []
            finished = []
            # 对于运行中的回测,key 为 to_run_df 中所有排列组合中的序数
            for key in running_backtests.keys():
                # 研究调用回测的结果,running_backtests[key] 为运行中保存的结果 id
                bt = get_backtest(running_backtests[key])
                # 获得运行回测结果的状态,成功和失败都需要运行结束后返回,如果没有返回则运行没有结束
                status = bt.get_status()
                # 当运行回测失败
                if status == 'failed':
                    # 失败 list 中记录对应的回测结果 id
                    failed.append(key)
                # 当运行回测成功时
                elif status == 'done':
                    # 成功 list 记录对应的回测结果 id,finish 仅记录运行成功的
                    finished.append(key)
                    # 回测回报率记录对应回测的回报率 dict, key to_run_df 中所有排列组合中的序数, value 为回报率的 dict
                    # 每个 value 一个 list 每个对象为一个包含时间、日回报率和基准回报率的 dict
                    all_results[key] = bt.get_results()
                    # 回测回报率记录对应回测结果指标 dict, key to_run_df 中所有排列组合中的序数, value 为回测结果指标的 dataframe
                    all_evaluations[key] = bt.get_risk()
            # 记录运行中回测结果 id 的 list 中删除失败的运行
            for key in failed:
                running_backtests.pop(key)
            # 在结束回测结果 dict 中记录运行成功的回测结果 id,同时在运行中的记录中删除该回测
            for key in finished:
                finished_backtests[key] = running_backtests.pop(key)
            # 当一组同时运行的回测结束时报告时间
            if len(finished_backtests) != 0 and len(finished_backtests) % running_max == 0 and to_run !=0:
                # 记录当时时间
                middle = time.time()
                # 计算剩余时间,假设没工作量时间相等的话
                remain_time = (middle - start) * (total_backtest_num - len(finished_backtests)) / len(finished_backtests)
                # print 当前运行时间
                print('[已用%s时,尚余%s时,请不要关闭浏览器].' % (str(round((middle - start) / 60.0 / 60.0,3)), 
                                          str(round(remain_time / 60.0 / 60.0,3)))),
            # 5秒钟后再跑一下
            time.sleep(30) 
        # 记录结束时间
        end = time.time() 
        print ('')
        print('【回测完成】总用时:%s秒(即%s小时)。' % (str(int(end-start)), 
                                           str(round((end-start)/60.0/60.0,2)))),
        # 对应修改类内部对应
        self.params_df = to_run_df
        self.results = all_results
        self.evaluations = all_evaluations
        self.backtest_ids = finished_backtests

        
    #7 最大回撤计算方法
    def find_max_drawdown(self, returns):
        # 定义最大回撤的变量
        result = 0
        # 记录最高的回报率点
        historical_return = 0
        # 遍历所有日期
        for i in range(len(returns)):
            # 最高回报率记录
            historical_return = max(historical_return, returns[i])
            # 最大回撤记录
            drawdown = 1-(returns[i] + 1) / (historical_return + 1)
            # 记录最大回撤
            result = max(drawdown, result)
        # 返回最大回撤值
        return result

    # log 收益、新基准下超额收益和相对与新基准的最大回撤
    def organize_backtest_results(self, benchmark_id=None):
        # 若新基准的回测结果 id 没给出
        if benchmark_id==None:
            # 使用默认的基准回报率,默认的基准在回测策略中设定
            self.benchmark_returns = [x['benchmark_returns'] for x in self.results[0]]
        # 当新基准指标给出后    
        else:
            # 基准使用新加入的基准回测结果
            self.benchmark_returns = [x['returns'] for x in get_backtest(benchmark_id).get_results()]
        # 回测日期为结果中记录的第一项对应的日期
        self.dates = [x['time'] for x in self.results[0]]
        
        # 对应每个回测在所有备选回测中的顺序 (key),生成新数据
        # 由 {key:{u'benchmark_returns': 0.022480100091729405,
        #           u'returns': 0.03184566700000002,
        #           u'time': u'2006-02-14'}} 格式转化为:
        # {key: []} 格式,其中 list 为对应 date 的一个回报率 list
        for key in self.results.keys():
            self.returns[key] = [x['returns'] for x in self.results[key]]
        # 生成对于基准(或新基准)的超额收益率
        for key in self.results.keys():
            self.excess_returns[key] = [(x+1)/(y+1)-1 for (x,y) in zip(self.returns[key], self.benchmark_returns)]
        # 生成 log 形式的收益率
        for key in self.results.keys():
            self.log_returns[key] = [log(x+1) for x in self.returns[key]]
        # 生成超额收益率的 log 形式
        for key in self.results.keys():
            self.log_excess_returns[key] = [log(x+1) for x in self.excess_returns[key]]
        # 生成超额收益率的最大回撤
        for key in self.results.keys():
            self.excess_max_drawdown[key] = self.find_max_drawdown(self.excess_returns[key])
        # 生成年化超额收益率
        for key in self.results.keys():
            self.excess_annual_return[key] = (self.excess_returns[key][-1]+1)**(252./float(len(self.dates)))-1
        # 把调参数据中的参数组合 df 与对应结果的 df 进行合并
        self.evaluations_df = pd.concat([self.params_df, pd.DataFrame(self.evaluations).T], axis=1)
#         self.evaluations_df = 

    # 获取最总分析数据,调用排队回测函数和数据整理的函数    
    def get_backtest_data(self,
                          algorithm_id=None,                         # 回测策略id
                          benchmark_id=None,                         # 新基准回测结果id
                          file_name='results.pkl',                   # 保存结果的 pickle 文件名字
                          running_max=10,                            # 最大同时运行回测数量
                          start_date='2006-01-01',                   # 回测开始时间
                          end_date='2016-11-30',                     # 回测结束日期
                          frequency='day',                           # 回测的运行频率
                          initial_cash='1000000',                    # 回测初始持仓资金
                          param_names=[],                            # 回测需要测试的变量
                          param_values=[]                            # 对应每个变量的备选参数
                          ):
        # 调运排队回测函数,传递对应参数
        self.run_backtest(algorithm_id=algorithm_id,
                          running_max=running_max,
                          start_date=start_date,
                          end_date=end_date,
                          frequency=frequency,
                          initial_cash=initial_cash,
                          param_names=param_names,
                          param_values=param_values
                          )
        # 回测结果指标中加入 log 收益率和超额收益率等指标
        self.organize_backtest_results(benchmark_id)
        # 生成 dict 保存所有结果。
        results = {'returns':self.returns,
                   'excess_returns':self.excess_returns,
                   'log_returns':self.log_returns,
                   'log_excess_returns':self.log_excess_returns,
                   'dates':self.dates,
                   'benchmark_returns':self.benchmark_returns,
                   'evaluations':self.evaluations,
                   'params_df':self.params_df,
                   'backtest_ids':self.backtest_ids,
                   'excess_max_drawdown':self.excess_max_drawdown,
                   'excess_annual_return':self.excess_annual_return,
                   'evaluations_df':self.evaluations_df}
        # 保存 pickle 文件
        pickle_file = open(file_name, 'wb')
        pickle.dump(results, pickle_file)
        pickle_file.close()

    # 读取保存的 pickle 文件,赋予类中的对象名对应的保存内容    
    def read_backtest_data(self, file_name='results.pkl'):
        pickle_file = open(file_name, 'rb')
        results = pickle.load(pickle_file)
        self.returns = results['returns']
        self.excess_returns = results['excess_returns']
        self.log_returns = results['log_returns']
        self.log_excess_returns = results['log_excess_returns']
        self.dates = results['dates']
        self.benchmark_returns = results['benchmark_returns']
        self.evaluations = results['evaluations']
        self.params_df = results['params_df']
        self.backtest_ids = results['backtest_ids']
        self.excess_max_drawdown = results['excess_max_drawdown']
        self.excess_annual_return = results['excess_annual_return']
        self.evaluations_df = results['evaluations_df']
        
    # 回报率折线图    
    def plot_returns(self):
        # 通过figsize参数可以指定绘图对象的宽度和高度,单位为英寸;
        fig = plt.figure(figsize=(20,8))
        ax = fig.add_subplot(110)
        # 作图
        for key in self.returns.keys():
            ax.plot(range(len(self.returns[key])), self.returns[key], label=key)
        # 设定benchmark曲线并标记
        ax.plot(range(len(self.benchmark_returns)), self.benchmark_returns, label='benchmark', c='k', linestyle='--') 
        ticks = [int(x) for x in np.linspace(0, len(self.dates)-1, 11)]
        plt.xticks(ticks, [self.dates[i] for i in ticks])
        # 设置图例样式
        ax.legend(loc = 2, fontsize = 10)
        # 设置y标签样式
        ax.set_ylabel('returns',fontsize=20)
        # 设置x标签样式
        ax.set_yticklabels([str(x*100)+'% 'for x in ax.get_yticks()])
        # 设置图片标题样式
        ax.set_title("Strategy's performances with different parameters", fontsize=21)
        plt.xlim(0, len(self.returns[0]))

    # 超额收益率图    
    def plot_excess_returns(self):
        # 通过figsize参数可以指定绘图对象的宽度和高度,单位为英寸;
        fig = plt.figure(figsize=(20,8))
        ax = fig.add_subplot(110)
        # 作图
        for key in self.returns.keys():
            ax.plot(range(len(self.excess_returns[key])), self.excess_returns[key], label=key)
        # 设定benchmark曲线并标记
        ax.plot(range(len(self.benchmark_returns)), [0]*len(self.benchmark_returns), label='benchmark', c='k', linestyle='--')
        ticks = [int(x) for x in np.linspace(0, len(self.dates)-1, 11)]
        plt.xticks(ticks, [self.dates[i] for i in ticks])
        # 设置图例样式
        ax.legend(loc = 2, fontsize = 10)
        # 设置y标签样式
        ax.set_ylabel('excess returns',fontsize=20)
        # 设置x标签样式
        ax.set_yticklabels([str(x*100)+'% 'for x in ax.get_yticks()])
        # 设置图片标题样式
        ax.set_title("Strategy's performances with different parameters", fontsize=21)
        plt.xlim(0, len(self.excess_returns[0]))
        
    # log回报率图    
    def plot_log_returns(self):
        # 通过figsize参数可以指定绘图对象的宽度和高度,单位为英寸;
        fig = plt.figure(figsize=(20,8))
        ax = fig.add_subplot(110)
        # 作图
        for key in self.returns.keys():
            ax.plot(range(len(self.log_returns[key])), self.log_returns[key], label=key)
        # 设定benchmark曲线并标记
        ax.plot(range(len(self.benchmark_returns)), [log(x+1) for x in self.benchmark_returns], label='benchmark', c='k', linestyle='--')
        ticks = [int(x) for x in np.linspace(0, len(self.dates)-1, 11)]
        plt.xticks(ticks, [self.dates[i] for i in ticks])
        # 设置图例样式
        ax.legend(loc = 2, fontsize = 10)
        # 设置y标签样式
        ax.set_ylabel('log returns',fontsize=20)
        # 设置图片标题样式
        ax.set_title("Strategy's performances with different parameters", fontsize=21)
        plt.xlim(0, len(self.log_returns[0]))
    
    # 超额收益率的 log 图
    def plot_log_excess_returns(self):
        # 通过figsize参数可以指定绘图对象的宽度和高度,单位为英寸;
        fig = plt.figure(figsize=(20,8))
        ax = fig.add_subplot(110)
        # 作图
        for key in self.returns.keys():
            ax.plot(range(len(self.log_excess_returns[key])), self.log_excess_returns[key], label=key)
        # 设定benchmark曲线并标记
        ax.plot(range(len(self.benchmark_returns)), [0]*len(self.benchmark_returns), label='benchmark', c='k', linestyle='--')
        ticks = [int(x) for x in np.linspace(0, len(self.dates)-1, 11)]
        plt.xticks(ticks, [self.dates[i] for i in ticks])
        # 设置图例样式
        ax.legend(loc = 2, fontsize = 10)
        # 设置y标签样式
        ax.set_ylabel('log excess returns',fontsize=20)
        # 设置图片标题样式
        ax.set_title("Strategy's performances with different parameters", fontsize=21)
        plt.xlim(0, len(self.log_excess_returns[0]))

        
    # 回测的4个主要指标,包括总回报率、最大回撤夏普率和波动
    def get_eval4_bar(self, sort_by=[]): 
        
        sorted_params = self.params_df
        for by in sort_by:
            sorted_params = sorted_params.sort(by)
        indices = sorted_params.index
        
        fig = plt.figure(figsize=(20,7))

        # 定义位置
        ax1 = fig.add_subplot(221)
        # 设定横轴为对应分位,纵轴为对应指标
        ax1.bar(range(len(indices)), 
                [self.evaluations[x]['algorithm_return'] for x in indices], 0.6, label = 'Algorithm_return')
        plt.xticks([x+0.3 for x in range(len(indices))], indices)
        # 设置图例样式
        ax1.legend(loc='best',fontsize=15)
        # 设置y标签样式
        ax1.set_ylabel('Algorithm_return', fontsize=15)
        # 设置y标签样式
        ax1.set_yticklabels([str(x*100)+'% 'for x in ax1.get_yticks()])
        # 设置图片标题样式
        ax1.set_title("Strategy's of Algorithm_return performances of different quantile", fontsize=15)
        # x轴范围
        plt.xlim(0, len(indices))

        # 定义位置
        ax2 = fig.add_subplot(224)
        # 设定横轴为对应分位,纵轴为对应指标
        ax2.bar(range(len(indices)), 
                [self.evaluations[x]['max_drawdown'] for x in indices], 0.6, label = 'Max_drawdown')
        plt.xticks([x+0.3 for x in range(len(indices))], indices)
        # 设置图例样式
        ax2.legend(loc='best',fontsize=15)
        # 设置y标签样式
        ax2.set_ylabel('Max_drawdown', fontsize=15)
        # 设置x标签样式
        ax2.set_yticklabels([str(x*100)+'% 'for x in ax2.get_yticks()])
        # 设置图片标题样式
        ax2.set_title("Strategy's of Max_drawdown performances of different quantile", fontsize=15)
        # x轴范围
        plt.xlim(0, len(indices))

        # 定义位置
        ax3 = fig.add_subplot(223)
        # 设定横轴为对应分位,纵轴为对应指标
        ax3.bar(range(len(indices)),
                [self.evaluations[x]['sharpe'] for x in indices], 0.6, label = 'Sharpe')
        plt.xticks([x+0.3 for x in range(len(indices))], indices)
        # 设置图例样式
        ax3.legend(loc='best',fontsize=15)
        # 设置y标签样式
        ax3.set_ylabel('Sharpe', fontsize=15)
        # 设置x标签样式
        ax3.set_yticklabels([str(x*100)+'% 'for x in ax3.get_yticks()])
        # 设置图片标题样式
        ax3.set_title("Strategy's of Sharpe performances of different quantile", fontsize=15)
        # x轴范围
        plt.xlim(0, len(indices))

        # 定义位置
        ax4 = fig.add_subplot(222)
        # 设定横轴为对应分位,纵轴为对应指标
        ax4.bar(range(len(indices)), 
                [self.evaluations[x]['algorithm_volatility'] for x in indices], 0.6, label = 'Algorithm_volatility')
        plt.xticks([x+0.3 for x in range(len(indices))], indices)
        # 设置图例样式
        ax4.legend(loc='best',fontsize=15)
        # 设置y标签样式
        ax4.set_ylabel('Algorithm_volatility', fontsize=15)
        # 设置x标签样式
        ax4.set_yticklabels([str(x*100)+'% 'for x in ax4.get_yticks()])
        # 设置图片标题样式
        ax4.set_title("Strategy's of Algorithm_volatility performances of different quantile", fontsize=15)
        # x轴范围
        plt.xlim(0, len(indices))
        
    #14 年化回报和最大回撤,正负双色表示
    def get_eval(self, sort_by=[]):

        sorted_params = self.params_df
        for by in sort_by:
            sorted_params = sorted_params.sort(by)
        indices = sorted_params.index
        
        # 大小
        fig = plt.figure(figsize = (20, 8))
        # 图1位置
        ax = fig.add_subplot(110)
        # 生成图超额收益率的最大回撤
        ax.bar([x+0.3 for x in range(len(indices))],
               [-self.evaluations[x]['max_drawdown'] for x in indices], color = '#32CD32',  
                     width = 0.6, label = 'Max_drawdown', zorder=10)
        # 图年化超额收益
        ax.bar([x for x in range(len(indices))],
               [self.evaluations[x]['annual_algo_return'] for x in indices], color = 'r', 
                     width = 0.6, label = 'Annual_return')
        plt.xticks([x+0.3 for x in range(len(indices))], indices)
        # 设置图例样式
        ax.legend(loc='best',fontsize=15)
        # 基准线
        plt.plot([0, len(indices)], [0, 0], c='k', 
                 linestyle='--', label='zero')
        # 设置图例样式
        ax.legend(loc='best',fontsize=15)
        # 设置y标签样式
        ax.set_ylabel('Max_drawdown', fontsize=15)
        # 设置x标签样式
        ax.set_yticklabels([str(x*100)+'% 'for x in ax.get_yticks()])
        # 设置图片标题样式
        ax.set_title("Strategy's performances of different quantile", fontsize=15)
        #   设定x轴长度
        plt.xlim(0, len(indices))


    #14 超额收益的年化回报和最大回撤
    # 加入新的benchmark后超额收益和
    def get_excess_eval(self, sort_by=[]):

        sorted_params = self.params_df
        for by in sort_by:
            sorted_params = sorted_params.sort(by)
        indices = sorted_params.index
        
        # 大小
        fig = plt.figure(figsize = (20, 8))
        # 图1位置
        ax = fig.add_subplot(110)
        # 生成图超额收益率的最大回撤
        ax.bar([x+0.3 for x in range(len(indices))],
               [-self.excess_max_drawdown[x] for x in indices], color = '#32CD32',  
                     width = 0.6, label = 'Excess_max_drawdown')
        # 图年化超额收益
        ax.bar([x for x in range(len(indices))],
               [self.excess_annual_return[x] for x in indices], color = 'r', 
                     width = 0.6, label = 'Excess_annual_return')
        plt.xticks([x+0.3 for x in range(len(indices))], indices)
        # 设置图例样式
        ax.legend(loc='best',fontsize=15)
        # 基准线
        plt.plot([0, len(indices)], [0, 0], c='k', 
                 linestyle='--', label='zero')
        # 设置图例样式
        ax.legend(loc='best',fontsize=15)
        # 设置y标签样式
        ax.set_ylabel('Max_drawdown', fontsize=15)
        # 设置x标签样式
        ax.set_yticklabels([str(x*100)+'% 'for x in ax.get_yticks()])
        # 设置图片标题样式
        ax.set_title("Strategy's performances of different quantile", fontsize=15)
        #   设定x轴长度
        plt.xlim(0, len(indices))

应用多策略回测框架,统计回测结果¶

  • 根据回测algorithmId在研究中创建策略
  • 设置不同参数的回测组别
  • 读取回测结果进行展示

创建回测¶

#2 设定回测策略 id 
# 注意!注意!注意!这里的id是在 我的策略里面的编译运行的algorithmId,在浏览器地址里面复制一下
pa = parameter_analysis('cfe46079f32447f30bd51fdc29d5d2a5')
#3 运行回测
pa.get_backtest_data(file_name = 'results1.pkl',
                          running_max = 10,
                          benchmark_id = None,
                          start_date = '2005-07-30',
                           end_date = '2018-07-31',
                          frequency = 'day',
                          initial_cash = '1000000',
                          param_names = ['mkt_cap','limit','trade_rate'],
                          param_values = [['A','B','AB'],['C','D'],['E','F']]
                          )
【已完成|运行中|待运行】: [0|0|12]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [3|7|2]. [3|9|0]. [3|9|0]. [4|8|0]. [4|8|0]. [4|8|0]. [4|8|0]. [5|7|0]. [5|7|0]. [5|7|0]. [6|6|0]. [6|6|0]. [7|5|0]. [7|5|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [10|2|0]. [10|2|0]. [10|2|0]. [10|2|0]. [10|2|0]. [10|2|0]. [11|1|0]. 
【回测完成】总用时:1595秒(即0.44小时)。

读取回测结果¶

#4 数据读取
pa.read_backtest_data('results1.pkl')
#5 查看回测参数的df
pa.params_df
mkt_cap limit trade_rate
0 A C E
1 A C F
2 A D E
3 A D F
4 B C E
5 B C F
6 B D E
7 B D F
8 AB C E
9 AB C F
10 AB D E
11 AB D F
 
#6 查看回测结果指标
pa.evaluations_df
mkt_cap limit trade_rate __version algorithm_return algorithm_volatility alpha annual_algo_return annual_bm_return avg_position_days ... max_drawdown_period max_leverage period_label profit_loss_ratio sharpe sortino trading_days treasury_return win_count win_ratio
0 A C E 101 1.009546 0.2965687 -0.05223199 0.0567117 0.1149268 98.01931 ... [2007-10-10, 2008-11-04] 0 2018-07 1.088274 0.05635018 0.07191144 3163 0.5202192 2757 0.5188182
1 A C F 101 1.267845 0.2992116 -0.04010099 0.06685963 0.1149268 135.3829 ... [2008-01-15, 2008-11-04] 0 2018-07 1.160792 0.08976798 0.1121713 3163 0.5202192 1004 0.5348961
2 A D E 101 3.227688 0.257373 0.04648072 0.1206925 0.1149268 50.16568 ... [2007-10-17, 2008-09-18] 0 2018-07 1.510175 0.3135237 0.4158588 3163 0.5202192 402 0.5575589
3 A D F 101 1.182985 0.2876909 -0.01933845 0.06364865 0.1149268 87.80357 ... [2008-01-10, 2008-11-04] 0 2018-07 1.309774 0.08220159 0.104069 3163 0.5202192 122 0.5236052
4 B C E 101 7.00278 0.3039643 0.07634566 0.1786671 0.1149268 63.51194 ... [2015-06-12, 2018-07-05] 0 2018-07 1.188493 0.4561953 0.5723079 3163 0.5202192 2991 0.5430283
5 B C F 101 7.105567 0.3007711 0.0783099 0.1798566 0.1149268 97.28415 ... [2007-05-29, 2008-11-04] 0 2018-07 1.317819 0.4649934 0.5672308 3163 0.5202192 997 0.5171162
6 B D E 101 0.3825792 0.2676011 -0.0415876 0.02593532 0.1149268 31.90909 ... [2010-01-19, 2013-11-13] 0 2018-07 1.159811 -0.05255838 -0.06191328 3163 0.5202192 169 0.4870317
7 B D F 101 9.506175 0.2478845 0.1323763 0.2042977 0.1149268 71.68794 ... [2007-05-29, 2008-09-18] 0 2018-07 2.430939 0.6627993 0.8630007 3163 0.5202192 90 0.6766917
8 AB C E 101 0.8679224 0.2951216 -0.05759974 0.05062541 0.1149268 97.26053 ... [2007-10-16, 2008-11-04] 0 2018-07 1.085838 0.03600349 0.04588451 3163 0.5202192 2823 0.521715
9 AB C F 101 1.026158 0.292141 -0.04892295 0.05739954 0.1149268 137.7188 ... [2007-10-10, 2008-11-04] 0 2018-07 1.151267 0.05955871 0.07474193 3163 0.5202192 1000 0.5291005
10 AB D E 101 4.291621 0.251081 0.06629345 0.1407531 0.1149268 50.59509 ... [2007-10-16, 2008-09-18] 0 2018-07 1.691732 0.4012771 0.536713 3163 0.5202192 361 0.5149786
11 AB D F 101 2.789675 0.2698129 0.03272587 0.111046 0.1149268 83.99482 ... [2015-06-05, 2016-03-04] 0 2018-07 1.481276 0.263316 0.3338309 3163 0.5202192 159 0.6138996

12 rows × 29 columns

#7 回报率折线图    
pa.plot_returns()
/opt/conda/envs/python2/lib/python2.7/site-packages/matplotlib/axes/_subplots.py:69: MatplotlibDeprecationWarning: The use of 0 (which ends up being the _last_ sub-plot) is deprecated in 1.4 and will raise an error in 1.5
  mplDeprecation)
#8 超额收益率图    
pa.plot_excess_returns()
#9 log回报率图    
pa.plot_log_returns()
#10 超额收益率的 log 图
pa.plot_log_excess_returns()
#11 回测的4个主要指标,包括总回报率、最大回撤夏普率和波动
# get_eval4_bar(self, sort_by=[])
pa.get_eval4_bar()
#12 年化回报和最大回撤,正负双色显示
# get_eval(self, sort_by=[])
pa.get_eval()
#13 超额收益的年化回报和最大回撤
# 加入新的benchmark后超额收益和
# get_excess_eval(self, sort_by=[])
pa.get_excess_eval()

第二部分 将分组设置为6组¶

#2 设定回测策略 id 
# 注意!注意!注意!这里的id是在 我的策略里面的编译运行的algorithmId,在浏览器地址里面复制一下
pa3 = parameter_analysis('f006a8370a7b2035b1228e8b9934938e')
#3 运行回测
pa3.get_backtest_data(file_name = 'results3.pkl',
                          running_max = 10,
                          benchmark_id = None,
                          start_date = '2005-07-30',
                           end_date = '2018-07-31',
                          frequency = 'day',
                          initial_cash = '1000000',
                          param_names = ['mkt_cap','limit','trade_rate'],
                          param_values = [['A','B','AB'],['C','D'],['E','F']]
                          )
【已完成|运行中|待运行】: [0|0|12]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [0|10|2]. [1|9|2]. [1|10|1]. [1|10|1]. [1|10|1]. [2|9|1]. [2|10|0]. [2|10|0]. [3|9|0]. [3|9|0]. [4|8|0]. [4|8|0]. [4|8|0]. [5|7|0]. [5|7|0]. [5|7|0]. [5|7|0]. [6|6|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [7|5|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [8|4|0]. [9|3|0]. [10|2|0]. [10|2|0]. [10|2|0]. [10|2|0]. [10|2|0]. [11|1|0]. [11|1|0]. [11|1|0]. [11|1|0]. [11|1|0]. 
【回测完成】总用时:1756秒(即0.49小时)。
#4 数据读取
pa3.read_backtest_data('results3.pkl')
#5 查看回测参数的df
pa3.params_df
mkt_cap limit trade_rate
0 A C E
1 A C F
2 A D E
3 A D F
4 B C E
5 B C F
6 B D E
7 B D F
8 AB C E
9 AB C F
10 AB D E
11 AB D F
#6 查看回测结果指标
pa3.evaluations_df
mkt_cap limit trade_rate __version algorithm_return algorithm_volatility alpha annual_algo_return annual_bm_return avg_position_days ... max_drawdown_period max_leverage period_label profit_loss_ratio sharpe sortino trading_days treasury_return win_count win_ratio
0 A C E 101 0.5709307 0.2948358 -0.07108044 0.03634423 0.1149268 86.95659 ... [2007-10-10, 2008-11-04] 0 2018-07 1.068143 -0.01239935 -0.01605101 3163 0.5202192 2419 0.5047997
1 A C F 101 0.9387855 0.2928793 -0.05126712 0.05372196 0.1149268 131.2079 ... [2007-10-16, 2008-11-06] 0 2018-07 1.13801 0.04685194 0.05949642 3163 0.5202192 846 0.4970623
2 A D E 101 1.760996 0.2527152 0.01122596 0.08358092 0.1149268 39.65625 ... [2007-10-17, 2008-09-18] 0 2018-07 1.390838 0.1724507 0.2271863 3163 0.5202192 204 0.4678899
3 A D F 101 -0.1373378 0.2848252 -0.08996669 -0.01160867 0.1149268 83.57627 ... [2015-06-05, 2016-03-04] 0 2018-07 0.9579951 -0.1811942 -0.2221585 3163 0.5202192 86 0.5308642
4 B C E 101 3.358017 0.3059861 0.02174232 0.1233852 0.1149268 NaN ... [2008-01-15, 2008-11-04] 0 2018-07 NaN 0.2725129 0.3413356 3163 0.5202192 NaN NaN
5 B C F 101 4.623526 0.2997309 0.04624343 0.1462513 0.1149268 104.7444 ... [2007-09-06, 2008-11-04] 0 2018-07 1.32698 0.3544891 0.4370363 3163 0.5202192 896 0.5404101
6 B D E 101 0.6371078 0.2387703 -0.02058226 0.03972965 0.1149268 32 ... [2016-04-13, 2018-02-09] 0 2018-07 1.182374 -0.001132249 -0.001440891 3163 0.5202192 114 0.5277778
7 B D F 101 1.39288 0.2444687 0.006854711 0.07139469 0.1149268 62.82171 ... [2007-08-03, 2010-07-05] 0 2018-07 1.535104 0.1284201 0.1575824 3163 0.5202192 53 0.4206349
8 AB C E 101 1.160629 0.2901814 -0.0427238 0.06278362 0.1149268 84.76636 ... [2007-10-16, 2008-11-04] 0 2018-07 1.095541 0.07851509 0.1017285 3163 0.5202192 2396 0.4998957
9 AB C F 101 1.712243 0.2943371 -0.02237821 0.08205617 0.1149268 132.9497 ... [2015-06-04, 2018-07-06] 0 2018-07 1.191637 0.1428844 0.1817647 3163 0.5202192 832 0.5142151
10 AB D E 101 0.3440323 0.2316609 -0.04242732 0.02364498 0.1149268 37.62128 ... [2007-01-22, 2008-06-19] 0 2018-07 1.135712 -0.07059899 -0.08887362 3163 0.5202192 186 0.4638404
11 AB D F 101 1.452818 0.259866 0.005297632 0.07349174 0.1149268 76.34513 ... [2015-06-05, 2016-03-04] 0 2018-07 1.489177 0.1288808 0.1676386 3163 0.5202192 88 0.6241135

12 rows × 29 columns

#7 回报率折线图    
pa3.plot_returns()
#8 超额收益率图    
pa3.plot_excess_returns()
#9 log回报率图    
pa3.plot_log_returns()
#10 超额收益率的 log 图
pa3.plot_log_excess_returns()
#11 回测的4个主要指标,包括总回报率、最大回撤夏普率和波动
# get_eval4_bar(self, sort_by=[])
pa3.get_eval4_bar()
#12 年化回报和最大回撤,正负双色显示
# get_eval(self, sort_by=[])
pa3.get_eval()
#13 超额收益的年化回报和最大回撤
# 加入新的benchmark后超额收益和
# get_excess_eval(self, sort_by=[])
pa3.get_excess_eval()

第三部分¶

  • 选择D组
  • 加入PE、ROE限制参数
#2 设定回测策略 id 
# 注意!注意!注意!这里的id是在 我的策略里面的编译运行的algorithmId,在浏览器地址里面复制一下
pa = parameter_analysis('8fc5f2da0f9a52131889e9f305c8a959')
#3 运行回测
pa.get_backtest_data(file_name = 'results2.pkl',
                          running_max = 10,
                          benchmark_id = None,
                          start_date = '2005-07-30',
                           end_date = '2018-07-31',
                          frequency = 'day',
                          initial_cash = '1000000',
                          param_names = ['mkt_cap','trade_rate','roe','pe'],
                          param_values = [['A','B','AB'],['E','F'],[4,6,8],[100,150]]
                          )
【已完成|运行中|待运行】: [0|0|36]. [0|10|26]. [0|10|26]. [0|10|26]. [0|10|26]. [0|10|26]. [0|10|26]. [0|10|26]. [0|10|26]. [0|10|26]. [0|10|26]. [3|7|26]. [4|9|23]. [5|9|22]. [5|10|21]. [7|8|21]. [7|10|19]. [7|10|19]. [7|10|19]. [7|10|19]. [9|8|19]. [11|8|17]. [11|10|15]. [11|10|15]. [11|10|15]. [11|10|15]. [11|10|15]. [12|9|15]. [12|10|14]. [12|10|14]. [13|9|14]. [13|10|13]. [13|10|13]. [13|10|13]. [13|10|13]. [14|9|13]. [15|9|12]. [15|10|11]. [15|10|11]. [16|9|11]. [17|9|10]. [18|9|9]. [18|10|8]. [18|10|8]. [19|9|8]. [已用0.408时,尚余0.326时,请不要关闭浏览器]. [20|9|7]. [21|9|6]. [23|8|5]. [23|10|3]. [23|10|3]. [23|10|3]. [23|10|3]. [23|10|3]. [23|10|3]. [23|10|3]. [24|9|3]. [27|7|2]. [27|9|0]. [28|8|0]. [28|8|0]. [29|7|0]. [30|6|0]. [30|6|0]. [31|5|0]. [31|5|0]. [33|3|0]. [33|3|0]. [33|3|0]. [34|2|0]. [34|2|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. [35|1|0]. 
【回测完成】总用时:2708秒(即0.75小时)。
#4 数据读取
pa.read_backtest_data('results2.pkl')
#5 查看回测参数的df
pa.params_df
mkt_cap trade_rate roe pe
0 A E 4 100
1 A E 4 150
2 A E 6 100
3 A E 6 150
4 A E 8 100
5 A E 8 150
6 A F 4 100
7 A F 4 150
8 A F 6 100
9 A F 6 150
10 A F 8 100
11 A F 8 150
12 B E 4 100
13 B E 4 150
14 B E 6 100
15 B E 6 150
16 B E 8 100
17 B E 8 150
18 B F 4 100
19 B F 4 150
20 B F 6 100
21 B F 6 150
22 B F 8 100
23 B F 8 150
24 AB E 4 100
25 AB E 4 150
26 AB E 6 100
27 AB E 6 150
28 AB E 8 100
29 AB E 8 150
30 AB F 4 100
31 AB F 4 150
32 AB F 6 100
33 AB F 6 150
34 AB F 8 100
35 AB F 8 150
#6 查看回测结果指标
pa.evaluations_df
mkt_cap trade_rate roe pe __version algorithm_return algorithm_volatility alpha annual_algo_return annual_bm_return ... max_drawdown_period max_leverage period_label profit_loss_ratio sharpe sortino trading_days treasury_return win_count win_ratio
0 A E 4 100 101 1.51397 0.2790864 -0.019942 0.07558321 0.1149268 ... [2007-10-16, 2008-11-04] 0 2018-07 1.151376 0.1274989 0.1610413 3163 0.5202192 1724 0.5202173
1 A E 4 150 101 2.201168 0.2670016 0.002570072 0.09632426 0.1149268 ... [2007-10-16, 2008-09-18] 0 2018-07 1.183885 0.210951 0.2783873 3163 0.5202192 1793 0.5350642
2 A E 6 100 101 2.454931 0.2701605 0.01530891 0.1029546 0.1149268 ... [2007-09-21, 2008-09-18] 0 2018-07 1.232382 0.2330268 0.2939738 3163 0.5202192 958 0.5234973
3 A E 6 150 101 2.506293 0.2746815 0.01337882 0.1042418 0.1149268 ... [2007-10-17, 2008-09-18] 0 2018-07 1.257508 0.2338775 0.2994088 3163 0.5202192 1019 0.5164724
4 A E 8 100 101 4.635243 0.2521377 0.0706987 0.1464399 0.1149268 ... [2007-09-03, 2008-09-18] 0 2018-07 1.637245 0.42215 0.564588 3163 0.5202192 362 0.5526718
5 A E 8 150 101 6.398365 0.2543058 0.09405614 0.1713738 0.1149268 ... [2007-12-26, 2008-09-18] 0 2018-07 1.70225 0.516598 0.6986493 3163 0.5202192 413 0.5455746
6 A F 4 100 101 1.410829 0.2842492 -0.02328658 0.07202772 0.1149268 ... [2007-10-16, 2008-11-04] 0 2018-07 1.236065 0.1126748 0.1434027 3163 0.5202192 700 0.5681818
7 A F 4 150 101 1.43893 0.278648 -0.02186649 0.07301008 0.1149268 ... [2007-09-24, 2008-11-06] 0 2018-07 NaN 0.1184652 0.1546604 3163 0.5202192 NaN NaN
8 A F 6 100 101 -0.2872708 0.2748843 -0.1165686 -0.02641175 0.1149268 ... [2007-09-17, 2017-05-10] 0 2018-07 0.9809332 -0.241599 -0.2929818 3163 0.5202192 356 0.4843537
9 A F 6 150 101 0.06853355 0.262299 -0.08127252 0.005253015 0.1149268 ... [2015-06-04, 2017-05-10] 0 2018-07 1.090149 -0.1324709 -0.1639984 3163 0.5202192 390 0.5349794
10 A F 8 100 101 1.334134 0.2754402 -0.01224447 0.06929183 0.1149268 ... [2007-09-05, 2008-11-06] 0 2018-07 1.285037 0.1063455 0.1347418 3163 0.5202192 144 0.576
11 A F 8 150 101 5.269912 0.2860172 0.07342999 0.1561513 0.1149268 ... [2015-06-05, 2016-03-04] 0 2018-07 1.541932 0.4060989 0.5318765 3163 0.5202192 164 0.5836299
12 B E 4 100 101 8.715938 0.2831101 0.1137039 0.1968775 0.1149268 ... [2010-04-12, 2012-12-03] 0 2018-07 1.355939 0.5541219 0.7220779 3163 0.5202192 1136 0.5184847
13 B E 4 150 101 7.545552 0.2895099 0.09873985 0.1847963 0.1149268 ... [2016-07-05, 2018-02-06] 0 2018-07 1.285978 0.5001429 0.6435695 3163 0.5202192 1259 0.5334746
14 B E 6 100 101 11.10379 0.310555 0.1342414 0.2178474 0.1149268 ... [2010-11-29, 2012-11-29] 0 2018-07 1.65699 0.5726761 0.7553973 3163 0.5202192 587 0.5255148
15 B E 6 150 101 8.427137 0.3017455 0.1135001 0.1940263 0.1149268 ... [2010-01-18, 2012-11-29] 0 2018-07 1.415349 0.5104512 0.6759887 3163 0.5202192 552 0.5227273
16 B E 8 100 101 2.787665 0.2710864 0.04518249 0.1109995 0.1149268 ... [2010-01-18, 2012-11-29] 0 2018-07 1.53032 0.2619071 0.356042 3163 0.5202192 185 0.5727554
17 B E 8 150 101 0.1515235 0.2620863 -0.05589475 0.01121367 0.1149268 ... [2010-01-19, 2015-09-02] 0 2018-07 1.065999 -0.1098353 -0.1437794 3163 0.5202192 175 0.5177515
18 B F 4 100 101 2.059935 0.3064447 -0.0008759597 0.09242129 0.1149268 ... [2007-07-31, 2008-11-04] 0 2018-07 1.271903 0.1710628 0.2179025 3163 0.5202192 502 0.5810185
19 B F 4 150 101 1.136955 0.3170365 -0.03236932 0.06185854 0.1149268 ... [2015-06-03, 2018-02-06] 0 2018-07 1.133699 0.06894643 0.0877739 3163 0.5202192 541 0.5560123
20 B F 6 100 101 4.573862 0.3119459 0.05815135 0.1454479 0.1149268 ... [2015-06-02, 2018-02-09] 0 2018-07 1.310422 0.3380328 0.4261629 3163 0.5202192 298 0.6464208
21 B F 6 150 101 1.674304 0.3034413 -0.007178739 0.08085207 0.1149268 ... [2015-06-02, 2018-02-09] 0 2018-07 1.210923 0.1346292 0.1651778 3163 0.5202192 274 0.6227273
22 B F 8 100 101 7.00193 0.2501033 0.1072077 0.1786572 0.1149268 ... [2007-05-29, 2008-09-23] 0 2018-07 1.957108 0.5543997 0.7164344 3163 0.5202192 93 0.6118421
23 B F 8 150 101 3.064521 0.2820985 0.04646405 0.1172116 0.1149268 ... [2015-12-24, 2018-02-09] 0 2018-07 1.428752 0.2737043 0.3511455 3163 0.5202192 90 0.6666667
24 AB E 4 100 101 1.710126 0.2777164 -0.01224169 0.0819894 0.1149268 ... [2007-09-03, 2008-09-18] 0 2018-07 1.168465 0.1511953 0.1927286 3163 0.5202192 1706 0.521553
25 AB E 4 150 101 2.46135 0.2646219 0.008788849 0.1031164 0.1149268 ... [2007-10-15, 2008-09-18] 0 2018-07 1.199137 0.2385156 0.3133218 3163 0.5202192 1841 0.5339327
26 AB E 6 100 101 3.250583 0.272181 0.03380003 0.121171 0.1149268 ... [2007-10-17, 2008-09-18] 0 2018-07 1.25352 0.2982245 0.385035 3163 0.5202192 969 0.5521368
27 AB E 6 150 101 2.172089 0.2661536 0.005822409 0.09553378 0.1149268 ... [2007-10-17, 2008-09-18] 0 2018-07 1.211035 0.2086532 0.2662634 3163 0.5202192 913 0.5129213
28 AB E 8 100 101 4.517565 0.2449024 0.07139256 0.1445293 0.1149268 ... [2007-09-03, 2008-09-18] 0 2018-07 1.784187 0.4268201 0.5715919 3163 0.5202192 367 0.5594512
29 AB E 8 150 101 6.552652 0.2515478 0.09741396 0.1732863 0.1149268 ... [2008-01-15, 2008-09-18] 0 2018-07 1.893545 0.5298647 0.7363525 3163 0.5202192 365 0.559816
30 AB F 4 100 101 0.9496564 0.283369 -0.04120936 0.05418774 0.1149268 ... [2007-10-16, 2008-11-04] 0 2018-07 1.167627 0.0500681 0.06438606 3163 0.5202192 669 0.5412621
31 AB F 4 150 101 0.6876189 0.2781035 -0.05186657 0.04222987 0.1149268 ... [2007-10-16, 2008-11-04] 0 2018-07 1.138221 0.008018118 0.01030235 3163 0.5202192 657 0.5389664
32 AB F 6 100 101 0.1611334 0.2767069 -0.07347329 0.01187813 0.1149268 ... [2007-07-31, 2017-05-10] 0 2018-07 NaN -0.1016305 -0.1266932 3163 0.5202192 NaN NaN
33 AB F 6 150 101 -0.4141099 0.2765859 -0.1325307 -0.04137567 0.1149268 ... [2007-10-17, 2008-11-04] 0 2018-07 NaN -0.2942148 -0.3553613 3163 0.5202192 NaN NaN
34 AB F 8 100 101 -0.01183065 0.2827976 -0.08103046 -0.0009402145 0.1149268 ... [2015-06-05, 2016-03-04] 0 2018-07 1.015071 -0.1447686 -0.1803393 3163 0.5202192 107 0.5095238
35 AB F 8 150 101 1.834839 0.2637732 0.002478197 0.08584376 0.1149268 ... [2007-10-17, 2008-11-06] 0 2018-07 1.326925 0.1737999 0.2178736 3163 0.5202192 170 0.6273063

36 rows × 30 columns

#7 回报率折线图    
pa.plot_returns()
#8 超额收益率图    
pa.plot_excess_returns()
#9 log回报率图    
pa.plot_log_returns()
#10 超额收益率的 log 图
pa.plot_log_excess_returns()
#11 回测的4个主要指标,包括总回报率、最大回撤夏普率和波动
# get_eval4_bar(self, sort_by=[])
pa.get_eval4_bar()
#12 年化回报和最大回撤,正负双色显示
# get_eval(self, sort_by=[])
pa.get_eval()
#13 超额收益的年化回报和最大回撤
# 加入新的benchmark后超额收益和
# get_excess_eval(self, sort_by=[])
pa.get_excess_eval()

全部回复

0/140

量化课程

    移动端课程