克隆策略

    {"Description":"实验创建于6/25/2021","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-622:instruments","SourceOutputPortId":"-598:data"},{"DestinationInputPortId":"-607:instruments","SourceOutputPortId":"-598:data"},{"DestinationInputPortId":"-614:input_data","SourceOutputPortId":"-607:data"},{"DestinationInputPortId":"-811:data2","SourceOutputPortId":"-614:data"},{"DestinationInputPortId":"-811:data1","SourceOutputPortId":"-622:data"},{"DestinationInputPortId":"-1245:input_data","SourceOutputPortId":"-811:data"},{"DestinationInputPortId":"-797:training_ds","SourceOutputPortId":"-1245:data"},{"DestinationInputPortId":"-607:features","SourceOutputPortId":"-1746:data"},{"DestinationInputPortId":"-797:features","SourceOutputPortId":"-1746:data"},{"DestinationInputPortId":"-614:features","SourceOutputPortId":"-1746:data"}],"ModuleNodes":[{"Id":"-598","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"20210101","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"20210601","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-598"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-598","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-607","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":90,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-607"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-607"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-607","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-614","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":"import talib\ndef cal_macd1(df_total,close):\n def get_macd(df):\n close=[float(x) for x in df['close_0']]\n df['MACD'],_,_=talib.MACD(np.array(close),fastperiod=6,slowperiod=12,signalperiod=9)\n return df\n \n df1=df_total.groupby([\"instrument\"]).apply(get_macd)\n merge_df = pd.merge(df_total, df1[['date',\"instrument\",'MACD']], on=['date','instrument'], how='left')\n return merge_df['MACD']\n\ndef cal_macd2(df_total,close):\n def get_macd(df):\n close=[float(x) for x in df['close_0']]\n _,df['MACD2'],_=talib.MACD(np.array(close),fastperiod=6,slowperiod=12,signalperiod=9)\n return df\n \n df1=df_total.groupby([\"instrument\"]).apply(get_macd)\n merge_df = pd.merge(df_total, df1[['date',\"instrument\",'MACD2']], on=['date','instrument'], how='left')\n return merge_df['MACD2']\n\ndef cal_macd3(df_total,close):\n def get_macd(df):\n close=[float(x) for x in df['close_0']]\n _,_,df['MACD3']=talib.MACD(np.array(close),fastperiod=6,slowperiod=12,signalperiod=9)\n return df\n \n df1=df_total.groupby([\"instrument\"]).apply(get_macd)\n merge_df = pd.merge(df_total, df1[['date',\"instrument\",'MACD3']], on=['date','instrument'], how='left')\n return merge_df['MACD3']\n\nbigquant_run = {\n 'cal_macd1':cal_macd1,\n 'cal_macd2':cal_macd2,\n 'cal_macd3':cal_macd3\n}\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-614"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-614"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-614","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-622","ModuleId":"BigQuantSpace.advanced_auto_labeler.advanced_auto_labeler-v2","ModuleParameters":[{"Name":"label_expr","Value":"# #号开始的表示注释\n# 0. 每行一个,顺序执行,从第二个开始,可以使用label字段\n# 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html\n# 添加benchmark_前缀,可使用对应的benchmark数据\n# 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_\n\n# 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)\nshift(close, -5) / shift(open, -1)\n\n# 极值处理:用1%和99%分位的值做clip\nclip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))\n\n# 将分数映射到分类,这里使用20个分类\nall_wbins(label, 20)\n\n# 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)\nwhere(shift(high, -1) == shift(low, -1), NaN, label)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na_label","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"cast_label_int","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-622"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-622","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-797","ModuleId":"BigQuantSpace.stock_ranker_train.stock_ranker_train-v6","ModuleParameters":[{"Name":"learning_algorithm","Value":"排序","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_leaves","Value":30,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"minimum_docs_per_leaf","Value":1000,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"number_of_trees","Value":"2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"learning_rate","Value":"0.5","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_bins","Value":1023,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"feature_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_row_fraction","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"ndcg_discount_base","Value":1,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"m_lazy_run","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"training_ds","NodeId":"-797"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-797"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"test_ds","NodeId":"-797"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"base_model","NodeId":"-797"}],"OutputPortsInternal":[{"Name":"model","NodeId":"-797","OutputType":null},{"Name":"feature_gains","NodeId":"-797","OutputType":null},{"Name":"m_lazy_run","NodeId":"-797","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-811","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-811"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-811"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-811","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-1245","ModuleId":"BigQuantSpace.dropnan.dropnan-v2","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1245"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1245"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1245","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"-1746","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\n\nreturn_5\nmacd1=cal_macd1(close_0)\nmacd2=cal_macd2(close_0)\nmacd3=cal_macd3(close_0)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-1746"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1746","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"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='-598' Position='513,-299,200,200'/><NodePosition Node='-607' Position='861.7161865234375,-179.45822143554688,200,200'/><NodePosition Node='-614' Position='895,18,200,200'/><NodePosition Node='-622' Position='555,8,200,200'/><NodePosition Node='-797' Position='871,491,200,200'/><NodePosition Node='-811' Position='689,170,200,200'/><NodePosition Node='-1245' Position='747,286,200,200'/><NodePosition Node='-1746' Position='1127.58642578125,-328.926513671875,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 [66]:
    # 本代码由可视化策略环境自动生成 2021年6月25日18:11
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    import talib
    def cal_macd1(df_total,close):
        def get_macd(df):
            close=[float(x) for x in df['close_0']]
            df['MACD'],_,_=talib.MACD(np.array(close),fastperiod=6,slowperiod=12,signalperiod=9)
            return df
        
        df1=df_total.groupby(["instrument"]).apply(get_macd)
        merge_df = pd.merge(df_total, df1[['date',"instrument",'MACD']], on=['date','instrument'], how='left')
        return merge_df['MACD']
    
    def cal_macd2(df_total,close):
        def get_macd(df):
            close=[float(x) for x in df['close_0']]
            _,df['MACD2'],_=talib.MACD(np.array(close),fastperiod=6,slowperiod=12,signalperiod=9)
            return df
        
        df1=df_total.groupby(["instrument"]).apply(get_macd)
        merge_df = pd.merge(df_total, df1[['date',"instrument",'MACD2']], on=['date','instrument'], how='left')
        return merge_df['MACD2']
    
    def cal_macd3(df_total,close):
        def get_macd(df):
            close=[float(x) for x in df['close_0']]
            _,_,df['MACD3']=talib.MACD(np.array(close),fastperiod=6,slowperiod=12,signalperiod=9)
            return df
        
        df1=df_total.groupby(["instrument"]).apply(get_macd)
        merge_df = pd.merge(df_total, df1[['date',"instrument",'MACD3']], on=['date','instrument'], how='left')
        return merge_df['MACD3']
    
    m3_user_functions_bigquant_run = {
        'cal_macd1':cal_macd1,
        'cal_macd2':cal_macd2,
        'cal_macd3':cal_macd3
    }
    
    
    m1 = M.instruments.v2(
        start_date='20210101',
        end_date='20210601',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m4 = M.advanced_auto_labeler.v2(
        instruments=m1.data,
        label_expr="""# #号开始的表示注释
    # 0. 每行一个,顺序执行,从第二个开始,可以使用label字段
    # 1. 可用数据字段见 https://bigquant.com/docs/develop/datasource/deprecated/history_data.html
    #   添加benchmark_前缀,可使用对应的benchmark数据
    # 2. 可用操作符和函数见 `表达式引擎 <https://bigquant.com/docs/develop/bigexpr/usage.html>`_
    
    # 计算收益:5日收盘价(作为卖出价格)除以明日开盘价(作为买入价格)
    shift(close, -5) / shift(open, -1)
    
    # 极值处理:用1%和99%分位的值做clip
    clip(label, all_quantile(label, 0.01), all_quantile(label, 0.99))
    
    # 将分数映射到分类,这里使用20个分类
    all_wbins(label, 20)
    
    # 过滤掉一字涨停的情况 (设置label为NaN,在后续处理和训练中会忽略NaN的label)
    where(shift(high, -1) == shift(low, -1), NaN, label)
    """,
        start_date='',
        end_date='',
        benchmark='000300.SHA',
        drop_na_label=True,
        cast_label_int=True,
        user_functions={}
    )
    
    m7 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    
    return_5
    macd1=cal_macd1(close_0)
    macd2=cal_macd2(close_0)
    macd3=cal_macd3(close_0)
    """
    )
    
    m2 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m7.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m3 = M.derived_feature_extractor.v3(
        input_data=m2.data,
        features=m7.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions=m3_user_functions_bigquant_run
    )
    
    m6 = M.join.v3(
        data1=m4.data,
        data2=m3.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m8 = M.dropnan.v2(
        input_data=m6.data
    )
    
    m5 = M.stock_ranker_train.v6(
        training_ds=m8.data,
        features=m7.data,
        learning_algorithm='排序',
        number_of_leaves=30,
        minimum_docs_per_leaf=1000,
        number_of_trees=2,
        learning_rate=0.5,
        max_bins=1023,
        feature_fraction=1,
        data_row_fraction=1,
        ndcg_discount_base=1,
        m_lazy_run=False
    )
    
    设置评估测试数据集,查看训练曲线
    [视频教程]StockRanker训练曲线
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-e7300b45666a49c48855c8b376b6a34e"}/bigcharts-data-end