可视化配对交易策略

策略分享
新手专区
标签: #<Tag:0x00007fcf6e7a4500> #<Tag:0x00007fcf6e7a7ef8>

(iQuant) #1
克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81:options_data","SourceOutputPortId":"-86:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nclose_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-05-18","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":false,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-70","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v6","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-70","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v2","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-76","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","ModuleId":"BigQuantSpace.trade.trade-v3","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 # 按日期过滤得到今日的股票数据\n zscore =context.zscore\n zscore_today=zscore.ix[today]\n #获取股票的列表\n stocklist=context.stocklist\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\n # 加载股票历史数据\n stock_data = context.options['data'].read_df()\n #获取股票的列表\n stocklist=stock_data.instrument.unique()\n stocklist.sort()\n prices_df=pd.pivot_table(stock_data, values='close_0', index=['date'], columns=['instrument'])\n prices_df.fillna(method='ffill',inplace=True)\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 zscore_calcu = zscore(y-result.params[1]*x)\n context.zscore=zscore_calcu\n context.stocklist=stocklist\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":"","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":"benchmark","Value":"000300.SHA","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":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"amount_integer","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-81","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"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":false,"moduleIdForCode":6,"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='732,35,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1074,124,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-70' Position='1078,236,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-76' Position='1079,327,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-81' Position='1068,569,200,200'/><NodePosition Node='-86' Position='1076,418,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 [83]:
    # 本代码由可视化策略环境自动生成 2018年6月13日 13:59
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    close_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-05-18'),
        market='CN_STOCK_A',
        instrument_list="""601328.SHA
    601998.SHA""",
        max_count=0,
        m_cached=False
    )
    
    m3 = M.general_feature_extractor.v6(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m4 = M.derived_feature_extractor.v2(
        input_data=m3.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument'
    )
    
    m6 = M.dropnan.v1(
        input_data=m4.data,
        m_cached=False
    )
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m5_handle_data_bigquant_run(context, data):
        today=data.current_dt.strftime('%Y-%m-%d')
        # 按日期过滤得到今日的股票数据
        zscore =context.zscore
        zscore_today=zscore.ix[today]
        #获取股票的列表
        stocklist=context.stocklist
        # 转换成回测引擎所需要的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 m5_prepare_bigquant_run(context):
    
        # 加载股票历史数据
        stock_data = context.options['data'].read_df()
        #获取股票的列表
        stocklist=stock_data.instrument.unique()
        stocklist.sort()
        prices_df=pd.pivot_table(stock_data, values='close_0', index=['date'], columns=['instrument'])
        prices_df.fillna(method='ffill',inplace=True)
        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)
        zscore_calcu = zscore(y-result.params[1]*x)
        context.zscore=zscore_calcu
        context.stocklist=stocklist
        
    
    # 回测引擎:初始化函数,只执行一次
    def m5_initialize_bigquant_run(context):
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    m5 = M.trade.v3(
        instruments=m2.data,
        options_data=m6.data,
        start_date='',
        end_date='',
        handle_data=m5_handle_data_bigquant_run,
        prepare=m5_prepare_bigquant_run,
        initialize=m5_initialize_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        benchmark='000300.SHA',
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='真实价格',
        plot_charts=True,
        backtest_only=False,
        amount_integer=False
    )
    
    [2018-06-01 14:05:22.215801] INFO: bigquant: input_features.v1 开始运行..
    [2018-06-01 14:05:22.224771] INFO: bigquant: 命中缓存
    [2018-06-01 14:05:22.232822] INFO: bigquant: input_features.v1 运行完成[0.012812s].
    [2018-06-01 14:05:22.263509] INFO: bigquant: instruments.v2 开始运行..
    [2018-06-01 14:05:22.268641] INFO: bigquant: instruments.v2 运行完成[0.005143s].
    [2018-06-01 14:05:22.298076] INFO: bigquant: general_feature_extractor.v6 开始运行..
    [2018-06-01 14:05:22.494511] INFO: 基础特征抽取: 年份 2015, 特征行数=486
    [2018-06-01 14:05:22.539361] INFO: 基础特征抽取: 年份 2016, 特征行数=488
    [2018-06-01 14:05:22.578169] INFO: 基础特征抽取: 年份 2017, 特征行数=488
    [2018-06-01 14:05:22.750812] INFO: 基础特征抽取: 年份 2018, 特征行数=180
    [2018-06-01 14:05:22.788953] INFO: 基础特征抽取: 总行数: 1642
    [2018-06-01 14:05:22.800966] INFO: bigquant: general_feature_extractor.v6 运行完成[0.502859s].
    [2018-06-01 14:05:22.818414] INFO: bigquant: derived_feature_extractor.v2 开始运行..
    [2018-06-01 14:05:23.122108] INFO: derived_feature_extractor: /y_2015, 486
    [2018-06-01 14:05:23.219501] INFO: derived_feature_extractor: /y_2016, 488
    [2018-06-01 14:05:23.311712] INFO: derived_feature_extractor: /y_2017, 488
    [2018-06-01 14:05:23.453335] INFO: derived_feature_extractor: /y_2018, 180
    [2018-06-01 14:05:23.562823] INFO: bigquant: derived_feature_extractor.v2 运行完成[0.744392s].
    [2018-06-01 14:05:23.578129] INFO: bigquant: dropnan.v1 开始运行..
    [2018-06-01 14:05:23.819933] INFO: dropnan: /y_2015, 486/486
    [2018-06-01 14:05:23.902942] INFO: dropnan: /y_2016, 488/488
    [2018-06-01 14:05:24.044632] INFO: dropnan: /y_2017, 488/488
    [2018-06-01 14:05:24.103985] INFO: dropnan: /y_2018, 180/180
    [2018-06-01 14:05:24.157009] INFO: dropnan: 行数: 1642/1642
    [2018-06-01 14:05:24.158888] INFO: bigquant: dropnan.v1 运行完成[0.580775s].
    [2018-06-01 14:05:24.236000] INFO: bigquant: backtest.v7 开始运行..
    [2018-06-01 14:05:25.260703] INFO: algo: set price type:original
    2015-01-05 全仓买入: 601328.SHA
    [2018-06-01 14:05:28.319154] INFO: algo: get splits [2015-06-03 00:00:00+00:00] [asset:Equity(0 [601328.SHA]), ratio:0.963807225227356]
    [2018-06-01 14:05:28.329222] INFO: Position: position handle split[sid:0, orig_amount:141800, new_amount:147124.0, orig_cost:6.942082099708411,new_cost:6.69, ratio:0.963807225227356, last_sale_price:7.190001964569092]
    [2018-06-01 14:05:28.340786] INFO: Position: after split: asset: Equity(0 [601328.SHA]), amount: 147124.0, cost_basis: 6.69, last_sale_price: 7.460000038146973
    [2018-06-01 14:05:28.343268] INFO: Position: returning cash: 6.16
    2015-06-08 全仓买入: 601998.SHA
    2015-07-08 全仓买入: 601328.SHA
    2015-09-01 全仓买入: 601998.SHA
    2015-11-10 全仓买入: 601328.SHA
    2016-06-17 全仓买入: 601998.SHA
    [2018-06-01 14:05:31.443716] INFO: algo: get splits [2016-07-25 00:00:00+00:00] [asset:Equity(1 [601998.SHA]), ratio:0.9657426445424234]
    [2018-06-01 14:05:31.445407] INFO: Position: position handle split[sid:1, orig_amount:218000, new_amount:225733.0, orig_cost:5.571723787323411,new_cost:5.38, ratio:0.9657426445424234, last_sale_price:5.9200025215655305]
    [2018-06-01 14:05:31.446727] INFO: Position: after split: asset: Equity(1 [601998.SHA]), amount: 225733.0, cost_basis: 5.38, last_sale_price: 6.130000114440918
    [2018-06-01 14:05:31.447958] INFO: Position: returning cash: 0.1
    2016-11-23 全仓买入: 601328.SHA
    2017-04-27 全仓买入: 601998.SHA
    [2018-06-01 14:05:33.252830] INFO: algo: get splits [2017-07-24 00:00:00+00:00] [asset:Equity(1 [601998.SHA]), ratio:0.9700427168666901]
    [2018-06-01 14:05:33.254683] INFO: Position: position handle split[sid:1, orig_amount:262800, new_amount:270915.0, orig_cost:6.001890749641365,new_cost:5.82, ratio:0.9700427168666901, last_sale_price:6.799999667260655]
    [2018-06-01 14:05:33.259713] INFO: Position: after split: asset: Equity(1 [601998.SHA]), amount: 270915.0, cost_basis: 5.82, last_sale_price: 7.010000228881836
    [2018-06-01 14:05:33.265121] INFO: Position: returning cash: 6.15
    2018-02-05 全仓买入: 601328.SHA
    [2018-06-01 14:05:34.999916] INFO: Performance: Simulated 822 trading days out of 822.
    [2018-06-01 14:05:35.004648] INFO: Performance: first open: 2015-01-05 01:30:00+00:00
    [2018-06-01 14:05:35.008521] INFO: Performance: last close: 2018-05-18 07:00:00+00:00
    
    • 收益率88.54%
    • 年化收益率21.46%
    • 基准收益率10.45%
    • 阿尔法0.18
    • 贝塔0.76
    • 夏普比率0.53
    • 胜率0.75
    • 盈亏比1.014
    • 收益波动率32.16%
    • 信息比率0.7
    • 最大回撤40.62%
    [2018-06-01 14:05:42.271045] INFO: bigquant: backtest.v7 运行完成[18.035026s].
    

    传统策略的可视化开发