复制链接
克隆策略

    {"description":"实验创建于2022/7/29","graph":{"edges":[{"to_node_id":"-124:instruments","from_node_id":"-111:data"},{"to_node_id":"-124:features","from_node_id":"-119:data"},{"to_node_id":"-45:input_1","from_node_id":"-124:data"}],"nodes":[{"node_id":"-111","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2018-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2020-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\n000063.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":"-111"}],"output_ports":[{"name":"data","node_id":"-111"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-119","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nvolume_0\nclose_0\nclose_1\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-119"}],"output_ports":[{"name":"data","node_id":"-119"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-124","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":90,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-124"},{"name":"features","node_id":"-124"}],"output_ports":[{"name":"data","node_id":"-124"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-45","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 df = input_1.read()\n result = dict()\n sd = 50\n ed = 20\n def _get_max_vols(x2):\n x = x2.copy()\n x.sort_values(\"date\", ascending=True, inplace=True)\n code = x.instrument.values[0]\n print(code)\n x[\"shift_date\"] = x.date.shift(ed)\n x[\"shift_vol\"] = x.volume_0.shift(ed)\n x.dropna(inplace=True)\n max_vol_df = dict()\n k = 0\n for i in x.rolling(window=30):\n if len(i)<30:\n continue\n _i = i.sort_values(\"shift_vol\", ascending=False)\n max_vol = _i.iloc[0,:]\n i[\"max_date\"] = max_vol[\"shift_date\"]\n i[\"max_vol\"] = max_vol[\"shift_vol\"]\n max_vol_df[str(k)] = i.iloc[-1,:]\n k = k+1\n result[code] = pd.DataFrame(max_vol_df).T\n df.groupby(\"instrument\").apply(_get_max_vols)\n \n res_df = pd.DataFrame()\n for k,v in result.items():\n res_df = pd.concat([res_df, v], axis=0)\n \n res_df.reset_index(drop=True, inplace=True)\n data_1 = DataSource.write_df(res_df)\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":"-45"},{"name":"input_2","node_id":"-45"},{"name":"input_3","node_id":"-45"}],"output_ports":[{"name":"data_1","node_id":"-45"},{"name":"data_2","node_id":"-45"},{"name":"data_3","node_id":"-45"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-111' Position='115,153,200,200'/><node_position Node='-119' Position='516,153,200,200'/><node_position Node='-124' Position='310,282,200,200'/><node_position Node='-45' Position='306,394,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [69]:
    # 本代码由可视化策略环境自动生成 2022年7月29日 18:32
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m4_run_bigquant_run(input_1, input_2, input_3):
        df = input_1.read()
        result = dict()
        sd = 50
        ed = 20
        def _get_max_vols(x2):
            x = x2.copy()
            x.sort_values("date", ascending=True, inplace=True)
            code = x.instrument.values[0]
            print(code)
            x["shift_date"] = x.date.shift(ed)
            x["shift_vol"] = x.volume_0.shift(ed)
            x.dropna(inplace=True)
            max_vol_df = dict()
            k = 0
            for i in x.rolling(window=30):
                if len(i)<30:
                    continue
                _i = i.sort_values("shift_vol", ascending=False)
                max_vol = _i.iloc[0,:]
                i["max_date"] = max_vol["shift_date"]
                i["max_vol"] = max_vol["shift_vol"]
                max_vol_df[str(k)] = i.iloc[-1,:]
                k = k+1
            result[code] = pd.DataFrame(max_vol_df).T
        df.groupby("instrument").apply(_get_max_vols)
        
        res_df = pd.DataFrame()
        for k,v in result.items():
            res_df = pd.concat([res_df, v], axis=0)
        
        res_df.reset_index(drop=True, inplace=True)
        data_1 = DataSource.write_df(res_df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m4_post_run_bigquant_run(outputs):
        return outputs
    
    
    m1 = M.instruments.v2(
        start_date='2018-01-01',
        end_date='2020-01-01',
        market='CN_STOCK_A',
        instrument_list="""000002.SZA
    000063.SZA""",
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    volume_0
    close_0
    close_1
    """
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m4 = M.cached.v3(
        input_1=m3.data,
        run=m4_run_bigquant_run,
        post_run=m4_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    000002.SZA
    000063.SZA
    
    In [70]:
    m4.data_1.read()
    
    Out[70]:
    close_0 close_1 date instrument volume_0 shift_date shift_vol max_date max_vol
    0 3996.804688 4134.104492 2017-12-15 000002.SZA 39622918 2017-11-17 56394399.0 2017-11-14 73582554.0
    1 3907.55957 3996.804688 2017-12-18 000002.SZA 42128694 2017-11-20 39508849.0 2017-11-14 73582554.0
    2 4003.669678 3907.55957 2017-12-19 000002.SZA 34008410 2017-11-21 108178862.0 2017-11-21 108178862.0
    3 3926.781494 4003.669678 2017-12-20 000002.SZA 32902669 2017-11-22 105296977.0 2017-11-21 108178862.0
    4 4080.557617 3926.781494 2017-12-21 000002.SZA 59516386 2017-11-23 58032538.0 2017-11-21 108178862.0
    ... ... ... ... ... ... ... ... ... ...
    952 532.827271 522.881165 2019-12-25 000063.SZA 82451923 2019-11-27 42622296.0 2019-10-29 126619346.0
    953 535.195374 532.827271 2019-12-26 000063.SZA 45610528 2019-11-28 36999431.0 2019-10-29 126619346.0
    954 531.090637 535.195374 2019-12-27 000063.SZA 63917054 2019-11-29 37841288.0 2019-10-29 126619346.0
    955 542.299744 531.090637 2019-12-30 000063.SZA 86198543 2019-12-02 32676778.0 2019-10-29 126619346.0
    956 558.71875 542.299744 2019-12-31 000063.SZA 135154282 2019-12-03 33969901.0 2019-10-29 126619346.0

    957 rows × 9 columns