复制链接
克隆策略

    {"description":"实验创建于2022/8/9","graph":{"edges":[{"to_node_id":"-243:instruments","from_node_id":"-214:data"},{"to_node_id":"-243:features","from_node_id":"-222:data"},{"to_node_id":"-374:features","from_node_id":"-222:data"},{"to_node_id":"-374:input_data","from_node_id":"-243:data"},{"to_node_id":"-6195:input_1","from_node_id":"-374:data"}],"nodes":[{"node_id":"-214","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2020-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-01-01","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"000002.SZA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-214"}],"output_ports":[{"name":"data","node_id":"-214"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-222","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nclose_0\nmax_close_shift_date = ts_argmax(close_0, 30)\nmacd = ta_macd_hist(close_0, fastperiod=12, slowperiod=26, signalperiod=9)\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-222"}],"output_ports":[{"name":"data","node_id":"-222"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-243","module_id":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-243"},{"name":"features","node_id":"-243"}],"output_ports":[{"name":"data","node_id":"-243"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-374","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"{}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-374"},{"name":"features","node_id":"-374"}],"output_ports":[{"name":"data","node_id":"-374"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-6195","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n df = input_1.read_df()\n def func(_x):\n x = _x.copy()\n x.sort_values(\"date\", inplace=True)\n x.reset_index(drop=True, inplace=True)\n x[\"index2\"] = x.index - (30 - x.max_close_shift_date) + 1\n def del_neg(x):\n x = np.nan if x < 0 else x\n return x\n x[\"index2\"] = x[\"index2\"].apply(del_neg)\n x.replace(np.nan, 0, inplace=True)\n x[\"max_date\"] = x.iloc[x.index2.values,:][\"date\"].values\n x[\"macd2\"] = x.iloc[x.index2.values,:][\"macd\"].values\n x = x.iloc[30:,]\n return x\n result = df.groupby(\"instrument\").apply(func)\n \n result.reset_index(drop=True, inplace=True)\n data_1 = DataSource.write_df(result)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-6195"},{"name":"input_2","node_id":"-6195"},{"name":"input_3","node_id":"-6195"}],"output_ports":[{"name":"data_1","node_id":"-6195"},{"name":"data_2","node_id":"-6195"},{"name":"data_3","node_id":"-6195"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-214' Position='59,215,200,200'/><node_position Node='-222' Position='371,215,200,200'/><node_position Node='-243' Position='199,324,200,200'/><node_position Node='-374' Position='234,420,200,200'/><node_position Node='-6195' Position='199,512,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [12]:
    # 本代码由可视化策略环境自动生成 2022年8月9日 11:47
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m5_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df = input_1.read_df()
        def func(_x):
            x = _x.copy()
            x.sort_values("date", inplace=True)
            x.reset_index(drop=True, inplace=True)
            x["index2"] = x.index - (30 - x.max_close_shift_date) + 1
            def del_neg(x):
                x = np.nan if x < 0 else x
                return x
            x["index2"] = x["index2"].apply(del_neg)
            x.replace(np.nan, 0, inplace=True)
            x["max_date"] = x.iloc[x.index2.values,:]["date"].values
            x["macd2"] = x.iloc[x.index2.values,:]["macd"].values
            x = x.iloc[30:,]
            return x
        result = df.groupby("instrument").apply(func)
        
        result.reset_index(drop=True, inplace=True)
        data_1 = DataSource.write_df(result)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m5_post_run_bigquant_run(outputs):
        return outputs
    
    
    m1 = M.instruments.v2(
        start_date='2020-01-01',
        end_date='2022-01-01',
        market='CN_STOCK_A',
        instrument_list='000002.SZA',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    close_0
    max_close_shift_date = ts_argmax(close_0, 30)
    macd = ta_macd_hist(close_0, fastperiod=12, slowperiod=26, signalperiod=9)
    """
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m4 = M.derived_feature_extractor.v3(
        input_data=m3.data,
        features=m2.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m5 = M.cached.v3(
        input_1=m4.data,
        run=m5_run_bigquant_run,
        post_run=m5_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    In [14]:
    df = m5.data_1.read()
    df.head(35)
    
    Out[14]:
    close_0 date instrument max_close_shift_date macd index2 max_date macd2
    0 4341.048340 2020-02-21 000002.SZA 4.0 0.000000 5.0 2020-01-09 0.000000
    1 4289.104492 2020-02-24 000002.SZA 3.0 0.000000 5.0 2020-01-09 0.000000
    2 4352.921387 2020-02-25 000002.SZA 2.0 0.000000 5.0 2020-01-09 0.000000
    3 4468.682617 2020-02-26 000002.SZA 1.0 103.160698 5.0 2020-01-09 0.000000
    4 4403.381348 2020-02-27 000002.SZA 0.0 97.074364 5.0 2020-01-09 0.000000
    5 4391.508789 2020-02-28 000002.SZA 1.0 88.695442 7.0 2020-01-13 0.000000
    6 4620.062988 2020-03-02 000002.SZA 0.0 109.803604 7.0 2020-01-13 0.000000
    7 4615.610840 2020-03-03 000002.SZA 0.0 118.297058 8.0 2020-01-14 0.000000
    8 4787.768555 2020-03-04 000002.SZA 29.0 140.710648 38.0 2020-03-04 140.710648
    9 4793.705078 2020-03-05 000002.SZA 29.0 149.137756 39.0 2020-03-05 149.137756
    10 4620.062988 2020-03-06 000002.SZA 28.0 125.096161 39.0 2020-03-05 149.137756
    11 4533.983887 2020-03-09 000002.SZA 27.0 92.855110 39.0 2020-03-05 149.137756
    12 4571.086914 2020-03-10 000002.SZA 26.0 72.675690 39.0 2020-03-05 149.137756
    13 4508.753906 2020-03-11 000002.SZA 25.0 48.256912 39.0 2020-03-05 149.137756
    14 4444.937012 2020-03-12 000002.SZA 24.0 22.161674 39.0 2020-03-05 149.137756
    15 4370.730957 2020-03-13 000002.SZA 23.0 -4.992914 39.0 2020-03-05 149.137756
    16 4235.676270 2020-03-16 000002.SZA 22.0 -39.143051 39.0 2020-03-05 149.137756
    17 4171.858887 2020-03-17 000002.SZA 21.0 -66.706047 39.0 2020-03-05 149.137756
    18 3903.233398 2020-03-18 000002.SZA 20.0 -114.684158 39.0 2020-03-05 149.137756
    19 3744.432617 2020-03-19 000002.SZA 19.0 -158.664078 39.0 2020-03-05 149.137756
    20 3829.027344 2020-03-20 000002.SZA 18.0 -166.405014 39.0 2020-03-05 149.137756
    21 3643.512451 2020-03-23 000002.SZA 17.0 -185.122543 39.0 2020-03-05 149.137756
    22 3695.456787 2020-03-24 000002.SZA 16.0 -179.008636 39.0 2020-03-05 149.137756
    23 3932.915771 2020-03-25 000002.SZA 15.0 -133.565170 39.0 2020-03-05 149.137756
    24 3888.392334 2020-03-26 000002.SZA 14.0 -101.539398 39.0 2020-03-05 149.137756
    25 3943.304688 2020-03-27 000002.SZA 13.0 -66.679489 39.0 2020-03-05 149.137756
    26 3864.646240 2020-03-30 000002.SZA 12.0 -48.766979 39.0 2020-03-05 149.137756
    27 3806.765625 2020-03-31 000002.SZA 11.0 -39.652641 39.0 2020-03-05 149.137756
    28 3952.209229 2020-04-01 000002.SZA 10.0 -10.488886 39.0 2020-03-05 149.137756
    29 3989.312256 2020-04-02 000002.SZA 9.0 15.908464 39.0 2020-03-05 149.137756
    30 3972.987061 2020-04-03 000002.SZA 8.0 32.332508 39.0 2020-03-05 149.137756
    31 4017.510498 2020-04-07 000002.SZA 7.0 49.337238 39.0 2020-03-05 149.137756
    32 4001.185303 2020-04-08 000002.SZA 6.0 57.935635 39.0 2020-03-05 149.137756
    33 3961.114014 2020-04-09 000002.SZA 5.0 57.651474 39.0 2020-03-05 149.137756
    34 3989.312256 2020-04-10 000002.SZA 4.0 60.396999 39.0 2020-03-05 149.137756