关于“股票配对交易策略”模板的修改使用报错?

策略分享
标签: #<Tag:0x00007f25ae5901e8>

(youke) #1
克隆策略

配对交易策略

版本 v1.0

目录

  • ### 配对交易策略的交易规则

  • ### 策略构建步骤

  • ### 策略的实现

正文

一、配对策略的交易规则

  • 对于股价有长期协整关系的两只股票X和Y, 可以通过历史数据回归计算两只股票的股价关系,即 Y = a*X + b, 得到相关系数a和残差项b;
  • 如果两个股票所属同一行业,我们可以认为两者的股价未来应该保持上述关系,即序列 zscore=(b-mean(b,N))/std(b,N) 存在比较稳定的均值回归特性,保持在-1和1之间往复震荡;
  • 当zscore小于-1时,Y股票低估,此时卖出X, 全仓买入Y;
  • 当zscore大于 1时,X股票低估,此时卖出Y, 全仓买入X;

二、策略构建步骤

1、确定股票池和回测时间

  • 通过证券代码列表输入要回测的两只股票,以及回测的起止日期

2、获取历史价格数据

  • 通过输入特征列表输入 close_0/adjust_factor_0 因子,指定获取股票的收盘价真实价格。
  • 通过基础特征抽取模块获取股票的价格数据。
  • 通过缺失数据处理模块删去有缺失值的数据。

3、确定买卖原则

  • 当zscore小于-1时,卖出X股票, 全仓买入Y;
  • 当zscore大于 1时,卖出Y股票, 全仓买入X;

4、模拟回测

  • 通过 trade 模块中的初始化函数定义交易手续费和滑点;
  • 通过 trade 模块中的盘前处理函数每日用当日之前的历史数据计算一次zscore变量序列,并存放在context.zscore 变量中;
  • 回测第一天用过去240个自然日的历史数据计算,后面每日用于计算zscore的历史数据逐日递增;
  • 通过 trade 模块中的主函数(handle函数)查看每日zscore值,按照买卖原则执行相应的卖出/买入操作。

三、策略的实现

可视化策略实现如下:

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-836:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-3301:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-836:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-3041:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-3041:options_data","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-3301:input_data","SourceOutputPortId":"-836:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-3301:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nclose_0/adjust_factor_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2018-10-28","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"601328.SHA\n601998.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"-86","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-86","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-836","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"300","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-836"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-836"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-836","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-3041","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n today = data.current_dt.strftime('%Y-%m-%d')\n zscore_today =context.zscore.ix[today]\n #获取股票的列表\n stocklist=context.instruments\n # 转换成回测引擎所需要的symbol格式\n symbol_1 = context.symbol(stocklist[0]) \n symbol_2 = context.symbol(stocklist[1]) \n\n # 持仓\n cur_position_1 = context.portfolio.positions[symbol_1].amount\n cur_position_2 = context.portfolio.positions[symbol_2].amount\n \n # 交易逻辑\n # 如果zesore大于上轨(>1),则价差会向下回归均值,因此需要买入股票x,卖出股票y\n if zscore_today > 1 and cur_position_1 == 0 and data.can_trade(symbol_1) and data.can_trade(symbol_2): \n context.order_target_percent(symbol_2, 0)\n context.order_target_percent(symbol_1, 1)\n print(today, '全仓买入:',stocklist[0])\n \n # 如果zesore小于下轨(<-1),则价差会向上回归均值,因此需要买入股票y,卖出股票x\n elif zscore_today < -1 and cur_position_2 == 0 and data.can_trade(symbol_1) and data.can_trade(symbol_2): \n context.order_target_percent(symbol_1, 0) \n context.order_target_percent(symbol_2, 1)\n print(today, '全仓买入:',stocklist[1])\n \n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"def bigquant_run(context,data): \n # 加载股票历史数据\n df = context.options['data'].read_df()\n df['date'] = df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))\n today = data.current_dt.strftime('%Y-%m-%d')\n # 获取前240个自然日的数据\n start_date = (pd.to_datetime(data.current_dt)-datetime.timedelta(days=240)).strftime('%Y-%m-%d')\n stock_data = df[df.date <= today]\n #获取股票的列表,由于可能上市天数不同,对缺失值填充处理\n stocklist=context.instruments\n prices_df=pd.pivot_table(stock_data, values='close_0', index=['date'], columns=['instrument'])\n prices_df.fillna(method='ffill',inplace=True)\n # 线性回归两个股票的股价 y=ax+b\n import statsmodels.api as sm\n x = prices_df[stocklist[0]] # 股票1\n y = prices_df[stocklist[1]] # 股票2\n \n X = sm.add_constant(x)\n result = (sm.OLS(y,X)).fit() \n \n def zscore(series):\n return (series - series.mean()) / np.std(series)\n # 计算 y-a*x 序列的zscore值序列\n zscore_calcu = zscore(y-result.params[1]*x)\n context.zscore=zscore_calcu","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"真实价格","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-3041"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-3041"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-3041"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-3041"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-3041"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-3041","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-3301","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-3301"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-3301"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-3301","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='1306,105,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='812,113,200,200'/><NodePosition Node='-86' Position='1081,397,200,200'/><NodePosition Node='-836' Position='1079,236,200,200'/><NodePosition Node='-3041' Position='1002,519,200,200'/><NodePosition Node='-3301' Position='1088,305,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [2]:
    # 本代码由可视化策略环境自动生成 2019年3月21日 11:11
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m3_handle_data_bigquant_run(context, data):
        today = data.current_dt.strftime('%Y-%m-%d')
        zscore_today =context.zscore.ix[today]
        #获取股票的列表
        stocklist=context.instruments
        # 转换成回测引擎所需要的symbol格式
        symbol_1 = context.symbol(stocklist[0]) 
        symbol_2 = context.symbol(stocklist[1])  
    
        # 持仓
        cur_position_1 = context.portfolio.positions[symbol_1].amount
        cur_position_2 = context.portfolio.positions[symbol_2].amount
           
        # 交易逻辑
        # 如果zesore大于上轨(>1),则价差会向下回归均值,因此需要买入股票x,卖出股票y
        if zscore_today > 1 and cur_position_1 == 0 and data.can_trade(symbol_1) and data.can_trade(symbol_2):  
            context.order_target_percent(symbol_2, 0)
            context.order_target_percent(symbol_1, 1)
            print(today, '全仓买入:',stocklist[0])
            
        # 如果zesore小于下轨(<-1),则价差会向上回归均值,因此需要买入股票y,卖出股票x
        elif zscore_today < -1 and cur_position_2 == 0 and data.can_trade(symbol_1) and data.can_trade(symbol_2):  
            context.order_target_percent(symbol_1, 0)  
            context.order_target_percent(symbol_2, 1)
            print(today, '全仓买入:',stocklist[1])
     
              
    
    # 回测引擎:准备数据,只执行一次
    def m3_prepare_bigquant_run(context):
        pass
        
    
    # 回测引擎:初始化函数,只执行一次
    def m3_initialize_bigquant_run(context):
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    def m3_before_trading_start_bigquant_run(context,data):    
        # 加载股票历史数据
        df = context.options['data'].read_df()
        df['date'] = df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))
        today = data.current_dt.strftime('%Y-%m-%d')
        # 获取前240个自然日的数据
        start_date = (pd.to_datetime(data.current_dt)-datetime.timedelta(days=240)).strftime('%Y-%m-%d')
        stock_data = df[df.date <= today]
        #获取股票的列表,由于可能上市天数不同,对缺失值填充处理
        stocklist=context.instruments
        prices_df=pd.pivot_table(stock_data, values='close_0', index=['date'], columns=['instrument'])
        prices_df.fillna(method='ffill',inplace=True)
        # 线性回归两个股票的股价 y=ax+b
        import statsmodels.api as sm
        x = prices_df[stocklist[0]] # 股票1
        y = prices_df[stocklist[1]] # 股票2
        
        X = sm.add_constant(x)
        result = (sm.OLS(y,X)).fit()  
        
        def zscore(series):
            return (series - series.mean()) / np.std(series)
        # 计算 y-a*x 序列的zscore值序列
        zscore_calcu = zscore(y-result.params[1]*x)
        context.zscore=zscore_calcu
    
    m1 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    close_0/adjust_factor_0
    """
    )
    
    m2 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2015-01-01'),
        end_date=T.live_run_param('trading_date', '2018-10-28'),
        market='CN_STOCK_A',
        instrument_list="""601328.SHA
    601998.SHA""",
        max_count=0
    )
    
    m7 = M.general_feature_extractor.v7(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=300
    )
    
    m4 = M.derived_feature_extractor.v3(
        input_data=m7.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m6 = M.dropnan.v1(
        input_data=m4.data
    )
    
    m3 = M.trade.v4(
        instruments=m2.data,
        options_data=m6.data,
        start_date='',
        end_date='',
        handle_data=m3_handle_data_bigquant_run,
        prepare=m3_prepare_bigquant_run,
        initialize=m3_initialize_bigquant_run,
        before_trading_start=m3_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    • 收益率54.33%
    • 年化收益率12.48%
    • 基准收益率-10.19%
    • 阿尔法0.15
    • 贝塔0.74
    • 夏普比率0.44
    • 胜率0.67
    • 盈亏比0.69
    • 收益波动率30.75%
    • 信息比率0.04
    • 最大回撤40.63%

    如题, 我用模板新建了一个“配对交易策略”

    运行起来是正常。

    2015-06-02卖出的601328的数量正是2015-01-06建仓的数量141800。

    可是问题来了,当我把配对的股票代码改成其他的,比如
    instrument_list=""“601788.SHA
    000977.SZA”"",

    就出现错误:

    Traceback (most recent call last):
    KeyError: '2015-01-30'
    [2019-03-21 11:37:11.782102] ERROR: bigquant: module name: trade, module version: v4, trackeback: Traceback (most recent call last):
    TypeError: 'str' object cannot be interpreted as an integer
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
    KeyError: '2015-01-30'
    
    TypeError                                 Traceback (most recent call last)
    TypeError: 'str' object cannot be interpreted as an integer
    
    During handling of the above exception, another exception occurred:
    
    KeyError                                  Traceback (most recent call last)
    <ipython-input-15-da352dbfba31> in <module>()
        125     backtest_only=False,
        126     m_cached=False,
    --> 127     benchmark=''
        128 )
    
    <ipython-input-15-da352dbfba31> in m3_handle_data_bigquant_run(context, data)
          6 def m3_handle_data_bigquant_run(context, data):
          7     today = data.current_dt.strftime('%Y-%m-%d')
    ----> 8     zscore_today =context.zscore.ix[today]
          9     #获取股票的列表
         10     stocklist=context.instruments
    
    KeyError: '2015-01-30'
    

    另外,我把股票对改成 instrument_list=""“600999.SHA
    000977.SZA”"",
    运行是没错误提示,但是看交易记录,问题来了,建仓后,第一次卖出的数量比建仓的数量还多,如下图,这要如何修改?请高手指点,先谢过了


    (iQuant) #2

    收到您的提问,已提交给策略工程师,会尽快给您回复。


    (zuhe) #3

    在4月27日发生了除权 数量变化了


    (youke) #4

    感谢!查过,2015-04-27除权,10送10,并且每10股送现金0.8元,
    那数量不是从20000变成40000,派送的现金也变成股票数量了?


    (youke) #5

    可是问题来了,当我把配对的股票代码改成其他的,比如
    instrument_list=""“601788.SHA
    000977.SZA”"",

    就出现错误:
    Traceback (most recent call last):
    KeyError: ‘2015-01-30’
    [2019-03-21 11:37:11.782102] ERROR: bigquant: module name: trade, module version: v4, trackeback: Traceback (most recent call last):
    TypeError: ‘str’ object cannot be interpreted as an integer

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    KeyError: ‘2015-01-30’

    这个是怎么回事呢?


    (达达) #6
        try:
            zscore_today =context.zscore.ix[today]
        except:
            return
    

    指标计算的时候某些天可能因为停牌,有股票没数据。导致当日没有zscore,可以该问上述代码容错处理

    克隆策略

      {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-836:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-3301:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-836:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-3041:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-3041:options_data","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-3301:input_data","SourceOutputPortId":"-836:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-3301:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nclose_0/adjust_factor_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2018-10-28","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"601788.SHA\n000977.SZA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"-86","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-86","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-836","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"300","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-836"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-836"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-836","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-3041","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n today = data.current_dt.strftime('%Y-%m-%d')\n try:\n zscore_today =context.zscore.ix[today]\n except:\n return\n #获取股票的列表\n stocklist=context.instruments\n # 转换成回测引擎所需要的symbol格式\n symbol_1 = context.symbol(stocklist[0]) \n symbol_2 = context.symbol(stocklist[1]) \n\n # 持仓\n cur_position_1 = context.portfolio.positions[symbol_1].amount\n cur_position_2 = context.portfolio.positions[symbol_2].amount\n \n # 交易逻辑\n # 如果zesore大于上轨(>1),则价差会向下回归均值,因此需要买入股票x,卖出股票y\n if zscore_today > 1 and cur_position_1 == 0 and data.can_trade(symbol_1) and data.can_trade(symbol_2): \n context.order_target_percent(symbol_2, 0)\n context.order_target_percent(symbol_1, 1)\n print(today, '全仓买入:',stocklist[0])\n \n # 如果zesore小于下轨(<-1),则价差会向上回归均值,因此需要买入股票y,卖出股票x\n elif zscore_today < -1 and cur_position_2 == 0 and data.can_trade(symbol_1) and data.can_trade(symbol_2): \n context.order_target_percent(symbol_1, 0) \n context.order_target_percent(symbol_2, 1)\n print(today, '全仓买入:',stocklist[1])\n \n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"def bigquant_run(context,data): \n # 加载股票历史数据\n df = context.options['data'].read_df()\n df['date'] = df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))\n today = data.current_dt.strftime('%Y-%m-%d')\n # 获取前240个自然日的数据\n start_date = (pd.to_datetime(data.current_dt)-datetime.timedelta(days=240)).strftime('%Y-%m-%d')\n stock_data = df[df.date <= today]\n #获取股票的列表,由于可能上市天数不同,对缺失值填充处理\n stocklist=context.instruments\n prices_df=pd.pivot_table(stock_data, values='close_0', index=['date'], columns=['instrument'])\n prices_df.fillna(method='ffill',inplace=True)\n # 线性回归两个股票的股价 y=ax+b\n import statsmodels.api as sm\n x = prices_df[stocklist[0]] # 股票1\n y = prices_df[stocklist[1]] # 股票2\n \n X = sm.add_constant(x)\n result = (sm.OLS(y,X)).fit() \n \n def zscore(series):\n return (series - series.mean()) / np.std(series)\n # 计算 y-a*x 序列的zscore值序列\n zscore_calcu = zscore(y-result.params[1]*x)\n context.zscore=zscore_calcu","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":1000000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"真实价格","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-3041"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-3041"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-3041"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-3041"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-3041"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-3041","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-3301","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-3301"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-3301"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-3301","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='1306,105,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='812,113,200,200'/><NodePosition Node='-86' Position='1081,397,200,200'/><NodePosition Node='-836' Position='1079,236,200,200'/><NodePosition Node='-3041' Position='1002,519,200,200'/><NodePosition Node='-3301' Position='1088,305,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
      In [2]:
      # 本代码由可视化策略环境自动生成 2019年3月21日 17:23
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # 回测引擎:每日数据处理函数,每天执行一次
      def m3_handle_data_bigquant_run(context, data):
          today = data.current_dt.strftime('%Y-%m-%d')
          try:
              zscore_today =context.zscore.ix[today]
          except:
              return
          #获取股票的列表
          stocklist=context.instruments
          # 转换成回测引擎所需要的symbol格式
          symbol_1 = context.symbol(stocklist[0]) 
          symbol_2 = context.symbol(stocklist[1])  
      
          # 持仓
          cur_position_1 = context.portfolio.positions[symbol_1].amount
          cur_position_2 = context.portfolio.positions[symbol_2].amount
             
          # 交易逻辑
          # 如果zesore大于上轨(>1),则价差会向下回归均值,因此需要买入股票x,卖出股票y
          if zscore_today > 1 and cur_position_1 == 0 and data.can_trade(symbol_1) and data.can_trade(symbol_2):  
              context.order_target_percent(symbol_2, 0)
              context.order_target_percent(symbol_1, 1)
              print(today, '全仓买入:',stocklist[0])
              
          # 如果zesore小于下轨(<-1),则价差会向上回归均值,因此需要买入股票y,卖出股票x
          elif zscore_today < -1 and cur_position_2 == 0 and data.can_trade(symbol_1) and data.can_trade(symbol_2):  
              context.order_target_percent(symbol_1, 0)  
              context.order_target_percent(symbol_2, 1)
              print(today, '全仓买入:',stocklist[1])
       
                
      
      # 回测引擎:准备数据,只执行一次
      def m3_prepare_bigquant_run(context):
          pass
          
      
      # 回测引擎:初始化函数,只执行一次
      def m3_initialize_bigquant_run(context):
          # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
          context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
      def m3_before_trading_start_bigquant_run(context,data):    
          # 加载股票历史数据
          df = context.options['data'].read_df()
          df['date'] = df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))
          today = data.current_dt.strftime('%Y-%m-%d')
          # 获取前240个自然日的数据
          start_date = (pd.to_datetime(data.current_dt)-datetime.timedelta(days=240)).strftime('%Y-%m-%d')
          stock_data = df[df.date <= today]
          #获取股票的列表,由于可能上市天数不同,对缺失值填充处理
          stocklist=context.instruments
          prices_df=pd.pivot_table(stock_data, values='close_0', index=['date'], columns=['instrument'])
          prices_df.fillna(method='ffill',inplace=True)
          # 线性回归两个股票的股价 y=ax+b
          import statsmodels.api as sm
          x = prices_df[stocklist[0]] # 股票1
          y = prices_df[stocklist[1]] # 股票2
          
          X = sm.add_constant(x)
          result = (sm.OLS(y,X)).fit()  
          
          def zscore(series):
              return (series - series.mean()) / np.std(series)
          # 计算 y-a*x 序列的zscore值序列
          zscore_calcu = zscore(y-result.params[1]*x)
          context.zscore=zscore_calcu
      
      m1 = M.input_features.v1(
          features="""# #号开始的表示注释
      # 多个特征,每行一个,可以包含基础特征和衍生特征
      close_0/adjust_factor_0
      """
      )
      
      m2 = M.instruments.v2(
          start_date=T.live_run_param('trading_date', '2015-01-01'),
          end_date=T.live_run_param('trading_date', '2018-10-28'),
          market='CN_STOCK_A',
          instrument_list="""601788.SHA
      000977.SZA""",
          max_count=0
      )
      
      m7 = M.general_feature_extractor.v7(
          instruments=m2.data,
          features=m1.data,
          start_date='',
          end_date='',
          before_start_days=300
      )
      
      m4 = M.derived_feature_extractor.v3(
          input_data=m7.data,
          features=m1.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=False,
          remove_extra_columns=False,
          user_functions={}
      )
      
      m6 = M.dropnan.v1(
          input_data=m4.data
      )
      
      m3 = M.trade.v4(
          instruments=m2.data,
          options_data=m6.data,
          start_date='',
          end_date='',
          handle_data=m3_handle_data_bigquant_run,
          prepare=m3_prepare_bigquant_run,
          initialize=m3_initialize_bigquant_run,
          before_trading_start=m3_before_trading_start_bigquant_run,
          volume_limit=0.025,
          order_price_field_buy='open',
          order_price_field_sell='open',
          capital_base=1000000,
          auto_cancel_non_tradable_orders=True,
          data_frequency='daily',
          price_type='真实价格',
          product_type='股票',
          plot_charts=True,
          backtest_only=False,
          benchmark=''
      )
      
      2015-01-19 全仓买入: 000977.SZA
      2015-05-08 全仓买入: 601788.SHA
      
      2015-07-06 全仓买入: 000977.SZA
      2015-09-21 全仓买入: 601788.SHA
      
      • 收益率-41.41%
      • 年化收益率-13.49%
      • 基准收益率-10.19%
      • 阿尔法-0.05
      • 贝塔1.17
      • 夏普比率-0.18
      • 胜率0.5
      • 盈亏比0.74
      • 收益波动率43.69%
      • 信息比率-0.01
      • 最大回撤69.61%