克隆策略

    {"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":"2014-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2015-01-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":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"output_ports":[{"name":"data","node_id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"cacheable":false,"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":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nopen = open_0 # 表示个股开盘价\nclose = close_0 # 个股收盘价\nbenchmark_open = cal_bm_open() # 基准指数开盘价\nbenchmark_close = cal_bm_close() # 基准指数收盘价\naddx(close_0,open_0) # 自定义一个表达式\n\n# 构建我们希望得到的衍生因子\nsum(where((close/open-benchmark_close/benchmark_open)<0 , (close/open-benchmark_close/benchmark_open)**2, 0),5)\n\n\n","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":"def cal_bm_open(df):\n bm_df = DataSource('bar1d_index_CN_STOCK_A').read(instruments=['000300.HIX'])\n bm_df.rename(columns={'open':'benchmark_open'}, inplace=True)\n merge_df = pd.merge(df, bm_df[['date','benchmark_open']], on='date', how='left')\n return merge_df['benchmark_open']\n\ndef cal_bm_close(df):\n bm_df = DataSource('bar1d_index_CN_STOCK_A').read(instruments=['000300.HIX'])\n bm_df.rename(columns={'close':'benchmark_close'}, inplace=True)\n merge_df = pd.merge(df, bm_df[['date','benchmark_close']], on='date', how='left')\n return merge_df['benchmark_close']\n\ndef addx(df, x1, x2):\n return x1+x2\n\nbigquant_run = {\n 'cal_bm_open': cal_bm_open,\n 'cal_bm_close': cal_bm_close,\n 'addx':addx\n}\n","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":false,"seq_num":16,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='127,-10,200,200'/><node_position Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='610.25244140625,4.501630783081055,200,200'/><node_position Node='-215' Position='259,156,200,200'/><node_position Node='-222' Position='272,258,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [20]:
    # 本代码由可视化策略环境自动生成 2021年10月15日 14:53
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    def cal_bm_open(df):
        bm_df = DataSource('bar1d_index_CN_STOCK_A').read(instruments=['000300.HIX'])
        bm_df.rename(columns={'open':'benchmark_open'}, inplace=True)
        merge_df = pd.merge(df, bm_df[['date','benchmark_open']], on='date', how='left')
        return merge_df['benchmark_open']
    
    def cal_bm_close(df):
        bm_df = DataSource('bar1d_index_CN_STOCK_A').read(instruments=['000300.HIX'])
        bm_df.rename(columns={'close':'benchmark_close'}, inplace=True)
        merge_df = pd.merge(df, bm_df[['date','benchmark_close']], on='date', how='left')
        return merge_df['benchmark_close']
    
    def addx(df, x1, x2):
        return x1+x2
    
    m16_user_functions_bigquant_run = {
        'cal_bm_open': cal_bm_open,
        'cal_bm_close': cal_bm_close,
        'addx':addx
    }
    
    
    m1 = M.instruments.v2(
        start_date='2014-01-01',
        end_date='2015-01-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0,
        m_cached=False
    )
    
    m3 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    open = open_0  # 表示个股开盘价
    close = close_0 # 个股收盘价
    benchmark_open = cal_bm_open() # 基准指数开盘价
    benchmark_close = cal_bm_close() # 基准指数收盘价
    addx(close_0,open_0) # 自定义一个表达式
    
    # 构建我们希望得到的衍生因子
    sum(where((close/open-benchmark_close/benchmark_open)<0 , (close/open-benchmark_close/benchmark_open)**2, 0),5)
    
    
    """
    )
    
    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,
        user_functions=m16_user_functions_bigquant_run,
        m_cached=False
    )
    

    核对大盘指数数据是否正确

    In [21]:
    bm_df = DataSource('bar1d_index_CN_STOCK_A').read(instruments=['000300.HIX'], start_date='2013-10-09', end_date='2013-10-09')
    bm_df
    
    Out[21]:
    turn adjust_factor instrument date open volume low close high amount
    0 0.3712 1.0 000300.HIX 2013-10-09 2432.939941 7627764500 2424.769043 2453.583008 2453.746094 7.612834e+10
    In [22]:
    df = m16.data.read()
    df[df['date'] == '2013-10-09']
    
    Out[22]:
    close_0 date instrument open_0 open close benchmark_open benchmark_close addx(close_0,open_0) # 自定义一个表达式 sum(where((close/open-benchmark_close/benchmark_open)<0 , (close/open-benchmark_close/benchmark_open)**2, 0),5)
    1 732.757812 2013-10-09 000001.SZA 707.651367 707.651367 732.757812 2432.939941 2453.583008 1440.409180 NaN
    62 1097.435425 2013-10-09 000002.SZA 1110.291382 1110.291382 1097.435425 2432.939941 2453.583008 2207.726807 NaN
    123 52.789566 2013-10-09 000004.SZA 52.748928 52.748928 52.789566 2432.939941 2453.583008 105.538498 NaN
    184 23.817732 2013-10-09 000005.SZA 23.169001 23.169001 23.817732 2432.939941 2453.583008 46.986732 NaN
    245 146.813889 2013-10-09 000006.SZA 145.582733 145.582733 146.813889 2432.939941 2453.583008 292.396606 NaN
    ... ... ... ... ... ... ... ... ... ... ...
    142968 7.098151 2013-10-09 603333.SHA 7.098151 7.098151 7.098151 2432.939941 2453.583008 14.196301 NaN
    143029 16.028158 2013-10-09 603366.SHA 15.772219 15.772219 16.028158 2432.939941 2453.583008 31.800377 NaN
    143090 12.445930 2013-10-09 603399.SHA 12.496115 12.496115 12.445930 2432.939941 2453.583008 24.942043 NaN
    143151 8.208617 2013-10-09 603766.SHA 8.076549 8.076549 8.208617 2432.939941 2453.583008 16.285166 NaN
    143212 7.302272 2013-10-09 603993.SHA 7.425691 7.425691 7.302272 2432.939941 2453.583008 14.727962 NaN

    2361 rows × 10 columns

    查看构建出来的衍生因子

    In [23]:
    df.tail()
    
    Out[23]:
    close_0 date instrument open_0 open close benchmark_open benchmark_close addx(close_0,open_0) # 自定义一个表达式 sum(where((close/open-benchmark_close/benchmark_open)<0 , (close/open-benchmark_close/benchmark_open)**2, 0),5)
    713215 34.900002 2014-12-25 603998.SHA 35.009998 35.009998 34.900002 3254.479980 3335.418945 69.910004 0.009926
    713216 34.560001 2014-12-26 603998.SHA 34.509998 34.509998 34.560001 3343.639893 3445.839111 69.070000 0.007317
    713217 35.189999 2014-12-29 603998.SHA 34.490002 34.490002 35.189999 3502.183105 3455.455078 69.680000 0.001632
    713218 37.459999 2014-12-30 603998.SHA 35.000000 35.000000 37.459999 3450.813965 3457.553955 72.459999 0.001632
    713219 35.259998 2014-12-31 603998.SHA 36.049999 36.049999 35.259998 3462.388916 3533.705078 71.309998 0.003440