复制链接
克隆策略

    {"description":"实验创建于2017/8/26","graph":{"edges":[{"to_node_id":"-215:instruments","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"to_node_id":"-215:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-222:features","from_node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"to_node_id":"-222:input_data","from_node_id":"-215:data"}],"nodes":[{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","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-12-31","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":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"# 当天涨/跌停 即为TRUE ,否则为FALSE\nup_limit_bool = where(price_limit_status_0==3 , True, False)\ndown_limit_bool = where(price_limit_status_0==1, True, False)\nreturn_0\nprice_limit_status_1","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-215","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":"-215"},{"name":"features","node_id":"-215"}],"output_ports":[{"name":"data","node_id":"-215"}],"cacheable":true,"seq_num":15,"comment":"","comment_collapsed":true},{"node_id":"-222","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":"-222"},{"name":"features","node_id":"-222"}],"output_ports":[{"name":"data","node_id":"-222"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='131,-19,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='541,-25.325772523880005,200,200'/><node_position Node='-215' Position='360,160,200,200'/><node_position Node='-222' Position='358.2020263671875,282.022705078125,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [1]:
    # 本代码由可视化策略环境自动生成 2022年5月20日 16:14
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date='2018-01-01',
        end_date='2020-12-31',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m3 = M.input_features.v1(
        features="""# 当天涨/跌停 即为TRUE ,否则为FALSE
    up_limit_bool = where(price_limit_status_0==3 , True, False)
    down_limit_bool = where(price_limit_status_0==1, True, False)
    return_0
    price_limit_status_1"""
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m3.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m16 = M.derived_feature_extractor.v3(
        input_data=m15.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False
    )
    
    In [2]:
    df = m16.data.read()
    
    def cal_lianban_num(df):
        # "计算个股连板数量"
        df['lianban_num'] = df.groupby(df['up_limit_bool'].astype(int).diff().ne(0).cumsum())['up_limit_bool'].cumsum()
        return df 
    
    # 按标的进行分区
    result = df.groupby('instrument').apply(cal_lianban_num)
    result
    
    Out[2]:
    date instrument price_limit_status_0 price_limit_status_1 return_0 up_limit_bool down_limit_bool lianban_num
    0 2017-10-09 000001.SZA 2.0 2.0 1.017102 False False 0
    1 2017-10-10 000001.SZA 2.0 2.0 1.015044 False False 0
    2 2017-10-11 000001.SZA 2.0 2.0 1.005231 False False 0
    3 2017-10-12 000001.SZA 2.0 2.0 1.001735 False False 0
    4 2017-10-13 000001.SZA 2.0 2.0 0.983550 False False 0
    ... ... ... ... ... ... ... ... ...
    2841208 2020-12-25 689009.SHA 2.0 2.0 1.027360 False False 0
    2841209 2020-12-28 689009.SHA 2.0 2.0 1.066578 False False 0
    2841210 2020-12-29 689009.SHA 2.0 2.0 0.967041 False False 0
    2841211 2020-12-30 689009.SHA 2.0 2.0 1.044539 False False 0
    2841212 2020-12-31 689009.SHA 2.0 2.0 1.060685 False False 0

    2841213 rows × 8 columns

    In [7]:
    result[(result.date=='2020-12-31')]
    
    Out[7]:
    date instrument price_limit_status_0 price_limit_status_1 return_0 up_limit_bool down_limit_bool lianban_num
    1895494 2020-12-31 000001.SZA 2.0 2.0 1.007292 False False 0
    1895737 2020-12-31 000002.SZA 2.0 2.0 1.011276 False False 0
    1895980 2020-12-31 000004.SZA 2.0 2.0 1.011236 False False 0
    1896223 2020-12-31 000005.SZA 2.0 2.0 1.016064 False False 0
    1896466 2020-12-31 000006.SZA 2.0 2.0 1.012635 False False 0
    ... ... ... ... ... ... ... ... ...
    2840973 2020-12-31 688699.SHA 2.0 2.0 1.017897 False False 0
    2841001 2020-12-31 688777.SHA 2.0 2.0 1.002200 False False 0
    2841052 2020-12-31 688788.SHA 2.0 2.0 1.002256 False False 0
    2841167 2020-12-31 688981.SHA 2.0 2.0 1.022667 False False 0
    2841212 2020-12-31 689009.SHA 2.0 2.0 1.060685 False False 0

    4117 rows × 8 columns

    In [28]:
    dt = m16.data.read()
    dt
    
    Out[28]:
    date instrument price_limit_status_0 price_limit_status_1 return_0 up_limit_bool down_limit_bool
    0 2017-10-09 000001.SZA 2.0 2.0 1.017102 False False
    1 2017-10-10 000001.SZA 2.0 2.0 1.015044 False False
    2 2017-10-11 000001.SZA 2.0 2.0 1.005231 False False
    3 2017-10-12 000001.SZA 2.0 2.0 1.001735 False False
    4 2017-10-13 000001.SZA 2.0 2.0 0.983550 False False
    ... ... ... ... ... ... ... ...
    2841208 2020-12-25 689009.SHA 2.0 2.0 1.027360 False False
    2841209 2020-12-28 689009.SHA 2.0 2.0 1.066578 False False
    2841210 2020-12-29 689009.SHA 2.0 2.0 0.967041 False False
    2841211 2020-12-30 689009.SHA 2.0 2.0 1.044539 False False
    2841212 2020-12-31 689009.SHA 2.0 2.0 1.060685 False False

    2841213 rows × 7 columns

    In [29]:
    dt[dt.price_limit_status_1==3][["date", "instrument", "return_0", "price_limit_status_1"]]
    
    Out[29]:
    date instrument return_0 price_limit_status_1
    92 2017-11-22 000002.SZA 1.022334 3.0
    145 2017-11-13 000004.SZA 0.954493 3.0
    148 2017-11-16 000004.SZA 1.044703 3.0
    152 2017-11-22 000004.SZA 0.993715 3.0
    369 2017-10-20 000010.SZA 1.051051 3.0
    ... ... ... ... ...
    2838016 2020-11-10 688521.SHA 0.944039 3.0
    2838296 2020-10-13 688536.SHA 1.038306 3.0
    2838670 2020-09-18 688556.SHA 1.024007 3.0
    2839370 2020-08-04 688568.SHA 0.943546 3.0
    2840072 2020-06-02 688588.SHA 0.967944 3.0

    49163 rows × 4 columns

    In [22]:
    up_limit_num = dt.groupby("date")["up_limit_bool"].sum()
    down_limit_num = dt.groupby("date")["down_limit_bool"].sum()
    ds1 = pd.DataFrame([up_limit_num, down_limit_num])
    
    In [24]:
    ds1.T
    
    Out[24]:
    up_limit_bool down_limit_bool
    date
    2017-10-09 60 4
    2017-10-10 76 3
    2017-10-11 41 4
    2017-10-12 39 1
    2017-10-13 64 1
    ... ... ...
    2020-12-25 101 18
    2020-12-28 87 31
    2020-12-29 76 63
    2020-12-30 79 17
    2020-12-31 72 10

    790 rows × 2 columns