复制链接
克隆策略

    {"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":"-3256:features","from_node_id":"-119:data"},{"to_node_id":"-3256:input_data","from_node_id":"-124:data"}],"nodes":[{"node_id":"-111","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2018-12-02","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2019-02-01","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"","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":"max_shift_date=ts_argmax(high_0, 20) #20日高点那天\namount_0\n\nhigh_amount=cal_high_amount(amount_0)#计算高点那天的成交量\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":"0","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":"-3256","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":"#计算高点那天的成交量\ndef func(_x):\n days = 20\n x = _x.copy()\n x.sort_values(\"date\", inplace=True)\n x.reset_index(drop=True, inplace=True)\n x[\"index2\"] = x.index - (days - x.max_shift_date) + 1\n x = x.fillna(0)\n x[\"max_date\"] = x.iloc[x.index2.values,:][\"date\"].values\n x[\"high_amount\"] = x.iloc[x.index2.values,:][\"amount_0\"].values\n\n return x\n\ndef cal_high_amount(df,amount_0): \n result = df.groupby(\"instrument\").apply(func).reset_index(drop=True)\n #合并原df,保证顺序一致\n result = pd.merge(left=df,right=result[['date','instrument','high_amount']],on=['date','instrument'],how='left')\n return result['high_amount']\n\nbigquant_run = {\n 'cal_high_amount': cal_high_amount\n}\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-3256"},{"name":"features","node_id":"-3256"}],"output_ports":[{"name":"data","node_id":"-3256"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-111' Position='803.3778686523438,168.36144161224365,200,200'/><node_position Node='-119' Position='1124.5213012695312,178.84664916992188,200,200'/><node_position Node='-124' Position='1000.0908813476562,283.85652923583984,200,200'/><node_position Node='-3256' Position='1129.8040161132812,395.84667205810547,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [58]:
    # 本代码由可视化策略环境自动生成 2022年8月3日 14:16
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    #计算高点那天的成交量
    def func(_x):
        days = 20
        x = _x.copy()
        x.sort_values("date", inplace=True)
        x.reset_index(drop=True, inplace=True)
        x["index2"] = x.index - (days - x.max_shift_date) + 1
        x = x.fillna(0)
        x["max_date"] = x.iloc[x.index2.values,:]["date"].values
        x["high_amount"] = x.iloc[x.index2.values,:]["amount_0"].values
    
        return x
    
    def cal_high_amount(df,amount_0):    
        result = df.groupby("instrument").apply(func).reset_index(drop=True)
        #合并原df,保证顺序一致
        result = pd.merge(left=df,right=result[['date','instrument','high_amount']],on=['date','instrument'],how='left')
        return result['high_amount']
    
    m5_user_functions_bigquant_run = {
        'cal_high_amount': cal_high_amount
    }
    
    
    m1 = M.instruments.v2(
        start_date='2018-12-02',
        end_date='2019-02-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""max_shift_date=ts_argmax(high_0, 20) #20日高点那天
    amount_0
    
    high_amount=cal_high_amount(amount_0)#计算高点那天的成交量
    """
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m5 = 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_user_functions_bigquant_run
    )
    
    In [60]:
    df = m5.data.read()
    df = df[df.instrument=='000001.SZA']
    df
    
    Out[60]:
    amount_0 date high_0 instrument max_shift_date high_amount
    0 1.126375e+09 2018-12-03 1151.614746 000001.SZA NaN 1.126375e+09
    1 5.657437e+08 2018-12-04 1147.293457 000001.SZA NaN 1.126375e+09
    2 6.081333e+08 2018-12-05 1135.410034 000001.SZA NaN 1.126375e+09
    3 5.946497e+08 2018-12-06 1120.285645 000001.SZA NaN 1.126375e+09
    4 3.511014e+08 2018-12-07 1122.446289 000001.SZA NaN 1.126375e+09
    5 5.040407e+08 2018-12-10 1109.482422 000001.SZA NaN 1.126375e+09
    6 3.053892e+08 2018-12-11 1108.402100 000001.SZA NaN 1.126375e+09
    7 3.754029e+08 2018-12-12 1114.884033 000001.SZA NaN 1.126375e+09
    8 8.199207e+08 2018-12-13 1132.169067 000001.SZA NaN 1.126375e+09
    9 5.392630e+08 2018-12-14 1118.125000 000001.SZA NaN 1.126375e+09
    10 5.846795e+08 2018-12-17 1115.964355 000001.SZA NaN 1.126375e+09
    11 5.471576e+08 2018-12-18 1114.884033 000001.SZA NaN 1.126375e+09
    12 6.000902e+08 2018-12-19 1099.759644 000001.SZA NaN 1.126375e+09
    13 9.642029e+08 2018-12-20 1077.072998 000001.SZA NaN 1.126375e+09
    14 9.444603e+08 2018-12-21 1047.904541 000001.SZA NaN 1.126375e+09
    15 4.771869e+08 2018-12-24 1020.896729 000001.SZA NaN 1.126375e+09
    16 5.452356e+08 2018-12-25 1018.736084 000001.SZA NaN 1.126375e+09
    17 3.932151e+08 2018-12-26 1017.655762 000001.SZA NaN 1.126375e+09
    18 5.863438e+08 2018-12-27 1025.218018 000001.SZA NaN 1.126375e+09
    19 5.415710e+08 2018-12-28 1021.977051 000001.SZA 0.0 1.126375e+09
    70881 4.986951e+08 2019-01-02 1017.655762 000001.SZA 0.0 5.657437e+08
    70882 3.844577e+08 2019-01-03 1007.932922 000001.SZA 0.0 6.081333e+08
    70883 1.422150e+09 2019-01-04 1060.868286 000001.SZA 5.0 8.199207e+08
    70884 8.411664e+08 2019-01-07 1064.109253 000001.SZA 4.0 8.199207e+08
    70885 3.892478e+08 2019-01-08 1052.225830 000001.SZA 3.0 8.199207e+08
    70886 1.229465e+09 2019-01-09 1088.956543 000001.SZA 2.0 8.199207e+08
    70887 1.079711e+09 2019-01-10 1101.920288 000001.SZA 1.0 8.199207e+08
    70888 7.080018e+08 2019-01-11 1104.080933 000001.SZA 0.0 8.199207e+08
    70889 5.078629e+08 2019-01-14 1107.321899 000001.SZA 0.0 5.392630e+08
    70890 5.530273e+08 2019-01-15 1110.562744 000001.SZA 0.0 5.846795e+08
    70891 1.017495e+09 2019-01-16 1134.329712 000001.SZA 19.0 1.017495e+09
    70892 9.102121e+08 2019-01-17 1141.891846 000001.SZA 19.0 9.102121e+08
    70893 7.699736e+08 2019-01-18 1133.249390 000001.SZA 18.0 9.102121e+08
    70894 6.844656e+08 2019-01-21 1131.088745 000001.SZA 17.0 9.102121e+08
    70895 4.389914e+08 2019-01-22 1127.847778 000001.SZA 16.0 9.102121e+08
    70896 5.593844e+08 2019-01-23 1131.088745 000001.SZA 15.0 9.102121e+08
    70897 7.114957e+08 2019-01-24 1139.731201 000001.SZA 14.0 9.102121e+08
    70898 2.299718e+09 2019-01-25 1192.666626 000001.SZA 19.0 2.299718e+09
    70899 1.141854e+09 2019-01-28 1203.469727 000001.SZA 19.0 1.141854e+09
    70900 9.048479e+08 2019-01-29 1195.907593 000001.SZA 18.0 1.141854e+09
    70901 7.852046e+08 2019-01-30 1207.791016 000001.SZA 19.0 7.852046e+08
    70902 9.226029e+08 2019-01-31 1209.951660 000001.SZA 19.0 9.226029e+08
    70903 1.127216e+09 2019-02-01 1215.353271 000001.SZA 19.0 1.127216e+09
    In [ ]: