克隆策略

    {"Description":"实验创建于2018/10/16","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-1442:instruments","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-1483:input_1","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-1473:features","SourceOutputPortId":"-1468:data"},{"DestinationInputPortId":"-1632:input_data","SourceOutputPortId":"-1473:data"},{"DestinationInputPortId":"-1473:input_data","SourceOutputPortId":"-1483:data_1"},{"DestinationInputPortId":"-1442:benchmark_ds","SourceOutputPortId":"-1483:data_1"},{"DestinationInputPortId":"-1442:options_data","SourceOutputPortId":"-1632:data"}],"ModuleNodes":[{"Id":"-25","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-09-05 09:01:00","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-10-12 15:15:00","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000001.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":"-25"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-25","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1442","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\n instrument_symbol = context.symbol(context.instruments[0]) # 交易标的\n curr_position = context.portfolio.positions[instrument_symbol].amount # 持仓数量\n \n if context.index < 5: # 日内数据超过窗口范围后才开始交易\n context.index += 1\n return\n \n # 获取当前分钟的买卖信号\n today_date=data.current_dt.strftime('%Y-%m-%d %H:%M:%S')\n try:\n buy_condition = context.buy_condition[today_date]\n except:\n buy_condition = 0\n \n try: \n sell_condition=context.sell_condition[today_date]\n except:\n sell_condition = 0\n \n \n # 如果当前没有仓位,且大于通道价格上限,long开仓\n if curr_position == 0 and buy_condition>0:\n context.order_target_percent(instrument_symbol, 1)\n elif curr_position >= 0 and sell_condition>0:\n context.order_target(instrument_symbol, 0)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n df = context.options['data'].read_df()\n df['date']=df['date'].apply(lambda x:x.strftime('%Y-%m-%d %H:%M:%S'))\n df.set_index('date',inplace=True)\n context.buy_condition=df['buy_condition']\n context.sell_condition=df['sell_condition']\n\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 设置是否是结算模式\n context.set_need_settle(False)\n context.set_commission(PerOrder(buy_cost=0.0013, sell_cost=0.0023, min_cost=5))\n # 当日分钟K线数量记录变量初始化\n context.index = 1\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":"0","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":"50000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"minute","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":"-1442"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1442"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1442"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1442"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1442"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1442","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1468","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nbuy_condition=where(ta_rsi(close,14)>60,1,0)\nsell_condition=where(ta_rsi(close,14)<40,1,0)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-1468"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1468","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1473","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":"-1473"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1473"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1473","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1483","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3,before_days):\n # 示例代码如下。在这里编写您的代码\n start_date=input_1.read_pickle()['start_date']\n end_date=input_1.read_pickle()['end_date']\n ins=input_1.read_pickle()['instruments'][0]\n df = DataSource('bar1m_'+ins).read(start_date=start_date,end_date=end_date).set_index('date')\n df['adjust_factor']=1.0\n \n def resample(df,period):\n # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases\n Xmin_df=pd.DataFrame()\n Xmin_df['open'] = df['open'].resample(period, how='first')\n Xmin_df['high'] = df['high'].resample(period, how='max')\n Xmin_df['low'] = df['low'].resample(period, how='min')\n Xmin_df['close'] = df['close'].resample(period, how='last')\n Xmin_df['volume'] = df['volume'].resample(period, how='sum')\n Xmin_df['amount'] = df['amount'].resample(period, how='sum')\n Xmin_df.dropna(inplace=True)\n return Xmin_df\n df = df.groupby('instrument').apply(lambda x:resample(x, '5min')).reset_index()\n data_1 = DataSource.write_df(df)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{'before_days':1}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-1483"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-1483"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-1483"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-1483","OutputType":null},{"Name":"data_2","NodeId":"-1483","OutputType":null},{"Name":"data_3","NodeId":"-1483","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1632","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1632"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1632","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"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='-25' Position='-55,0,200,200'/><NodePosition Node='-1442' Position='8,463,200,200'/><NodePosition Node='-1468' Position='551,32,200,200'/><NodePosition Node='-1473' Position='362,230,200,200'/><NodePosition Node='-1483' Position='95,131,200,200'/><NodePosition Node='-1632' Position='344,325,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 [4]:
    # 本代码由可视化策略环境自动生成 2019年1月31日 09:37
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m4_run_bigquant_run(input_1, input_2, input_3,before_days):
        # 示例代码如下。在这里编写您的代码
        start_date=input_1.read_pickle()['start_date']
        end_date=input_1.read_pickle()['end_date']
        ins=input_1.read_pickle()['instruments'][0]
        df = DataSource('bar1m_'+ins).read(start_date=start_date,end_date=end_date).set_index('date')
        df['adjust_factor']=1.0
        
        def resample(df,period):
            # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
            Xmin_df=pd.DataFrame()
            Xmin_df['open'] = df['open'].resample(period, how='first')
            Xmin_df['high'] = df['high'].resample(period, how='max')
            Xmin_df['low'] = df['low'].resample(period, how='min')
            Xmin_df['close'] = df['close'].resample(period, how='last')
            Xmin_df['volume'] = df['volume'].resample(period, how='sum')
            Xmin_df['amount'] = df['amount'].resample(period, how='sum')
            Xmin_df.dropna(inplace=True)
            return Xmin_df
        df = df.groupby('instrument').apply(lambda x:resample(x, '5min')).reset_index()
        data_1 = DataSource.write_df(df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m4_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m3_handle_data_bigquant_run(context, data):
    
        instrument_symbol = context.symbol(context.instruments[0]) # 交易标的
        curr_position = context.portfolio.positions[instrument_symbol].amount # 持仓数量
        
        if context.index < 5: # 日内数据超过窗口范围后才开始交易
            context.index += 1
            return
        
        # 获取当前分钟的买卖信号
        today_date=data.current_dt.strftime('%Y-%m-%d %H:%M:%S')
        try:
            buy_condition = context.buy_condition[today_date]
        except:
            buy_condition = 0
        
        try:    
            sell_condition=context.sell_condition[today_date]
        except:
            sell_condition = 0
            
        
        # 如果当前没有仓位,且大于通道价格上限,long开仓
        if curr_position == 0 and buy_condition>0:
            context.order_target_percent(instrument_symbol, 1)
        elif curr_position >= 0 and sell_condition>0:
            context.order_target(instrument_symbol, 0)
    # 回测引擎:准备数据,只执行一次
    def m3_prepare_bigquant_run(context):
        df = context.options['data'].read_df()
        df['date']=df['date'].apply(lambda x:x.strftime('%Y-%m-%d %H:%M:%S'))
        df.set_index('date',inplace=True)
        context.buy_condition=df['buy_condition']
        context.sell_condition=df['sell_condition']
    
    
    # 回测引擎:初始化函数,只执行一次
    def m3_initialize_bigquant_run(context):
       # 设置是否是结算模式
        context.set_need_settle(False)
        context.set_commission(PerOrder(buy_cost=0.0013, sell_cost=0.0023, min_cost=5))
        # 当日分钟K线数量记录变量初始化
        context.index = 1
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m3_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m2 = M.instruments.v2(
        start_date='2018-09-05 09:01:00',
        end_date='2018-10-12 15:15:00',
        market='CN_STOCK_A',
        instrument_list='000001.SZA',
        max_count=0
    )
    
    m4 = M.cached.v3(
        input_1=m2.data,
        run=m4_run_bigquant_run,
        post_run=m4_post_run_bigquant_run,
        input_ports='',
        params='{\'before_days\':1}',
        output_ports='',
        m_cached=False
    )
    
    m5 = M.input_features.v1(
        features="""
    # #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    buy_condition=where(ta_rsi(close,14)>60,1,0)
    sell_condition=where(ta_rsi(close,14)<40,1,0)
    """
    )
    
    m6 = M.derived_feature_extractor.v3(
        input_data=m4.data_1,
        features=m5.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m7 = M.dropnan.v1(
        input_data=m6.data
    )
    
    m3 = M.trade.v4(
        instruments=m2.data,
        options_data=m7.data,
        benchmark_ds=m4.data_1,
        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,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=50000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='minute',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    [2019-01-31 09:37:20.175571] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-31 09:37:20.500163] INFO: bigquant: instruments.v2 运行完成[0.324591s].
    [2019-01-31 09:37:20.503361] INFO: bigquant: cached.v3 开始运行..
    [2019-01-31 09:37:21.297367] INFO: bigquant: cached.v3 运行完成[0.793962s].
    [2019-01-31 09:37:21.300902] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-31 09:37:21.305314] INFO: bigquant: 命中缓存
    [2019-01-31 09:37:21.306386] INFO: bigquant: input_features.v1 运行完成[0.005528s].
    [2019-01-31 09:37:21.309574] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-31 09:37:21.345523] INFO: derived_feature_extractor: 提取完成 buy_condition=where(ta_rsi(close,14)>60,1,0), 0.015s
    [2019-01-31 09:37:21.352616] INFO: derived_feature_extractor: 提取完成 sell_condition=where(ta_rsi(close,14)<40,1,0), 0.006s
    [2019-01-31 09:37:21.707737] INFO: derived_feature_extractor: /data, 1100
    [2019-01-31 09:37:21.808617] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.499009s].
    [2019-01-31 09:37:21.812441] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-31 09:37:22.225501] INFO: dropnan: /data, 1100/1100
    [2019-01-31 09:37:22.232168] INFO: dropnan: 行数: 1100/1100
    [2019-01-31 09:37:22.234170] INFO: bigquant: dropnan.v1 运行完成[0.421729s].
    [2019-01-31 09:37:22.296470] INFO: bigquant: backtest.v8 开始运行..
    [2019-01-31 09:37:22.299467] INFO: bigquant: biglearning backtest:V8.1.7
    [2019-01-31 09:37:22.331892] INFO: bigquant: product_type:stock by specified
    [2019-01-31 09:37:23.526106] INFO: bigquant: 读取股票行情完成:20400
    [2019-01-31 09:37:23.547291] INFO: algo: TradingAlgorithm V1.4.5
    [2019-01-31 09:37:24.450746] INFO: algo: trading transform...
    [2019-01-31 09:37:46.110957] INFO: Performance: Simulated 22 trading days out of 22.
    [2019-01-31 09:37:46.112048] INFO: Performance: first open: 2018-09-05 09:30:00+00:00
    [2019-01-31 09:37:46.112764] INFO: Performance: last close: 2018-10-12 15:00:00+00:00
    
    • 收益率6.62%
    • 年化收益率108.42%
    • 基准收益率0.84%
    • 阿尔法0.69
    • 贝塔0.54
    • 夏普比率3.31
    • 胜率0.4
    • 盈亏比5.3
    • 收益波动率22.04%
    • 信息比率0.19
    • 最大回撤3.4%
    [2019-01-31 09:37:49.261127] INFO: bigquant: backtest.v8 运行完成[26.964628s].