克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-215:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-215:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-222:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-222:input_data","SourceOutputPortId":"-215:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2014-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"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), 60)\n\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-215","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":90,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-215"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-215"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-215","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-222","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":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","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-222"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-222"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-222","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='127.41879272460938,-10.02908706665039,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='614.553955078125,7.594470977783203,200,200'/><NodePosition Node='-215' Position='259.21026611328125,156.95555114746094,200,200'/><NodePosition Node='-222' Position='272.76239013671875,258.5076904296875,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":false}
    In [61]:
    # 本代码由可视化策略环境自动生成 2021年6月18日16:04
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    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), 60)
    
    
    """
    )
    
    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
    )
    

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

    In [64]:
    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[64]:
    adjust_factor amount close date high instrument low open turn volume
    0 1.0 7.612834e+10 2453.583008 2013-10-09 2453.746094 000300.HIX 2424.769043 2432.939941 0.3712 7627764500
    In [65]:
    df = m16.data.read()
    df[df['date'] == '2013-10-09']
    
    Out[65]:
    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) , 60)
    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
    306 78.842621 2013-10-09 000007.SZA 77.296684 77.296684 78.842621 2432.939941 2453.583008 156.139313 NaN
    367 50.036095 2013-10-09 000008.SZA 48.314205 48.314205 50.036095 2432.939941 2453.583008 98.350296 NaN
    428 48.266930 2013-10-09 000009.SZA 46.969669 46.969669 48.266930 2432.939941 2453.583008 95.236603 NaN
    488 80.054893 2013-10-09 000010.SZA 77.038017 77.038017 80.054893 2432.939941 2453.583008 157.092911 NaN
    540 28.118490 2013-10-09 000011.SZA 27.958906 27.958906 28.118490 2432.939941 2453.583008 56.077396 NaN
    601 144.451141 2013-10-09 000012.SZA 141.113983 141.113983 144.451141 2432.939941 2453.583008 285.565125 NaN
    662 61.020779 2013-10-09 000014.SZA 59.682606 59.682606 61.020779 2432.939941 2453.583008 120.703384 NaN
    723 35.079292 2013-10-09 000016.SZA 35.592148 35.592148 35.079292 2432.939941 2453.583008 70.671440 NaN
    785 17.661240 2013-10-09 000018.SZA 17.540033 17.540033 17.661240 2432.939941 2453.583008 35.201271 NaN
    846 16.678856 2013-10-09 000019.SZA 16.699245 16.699245 16.678856 2432.939941 2453.583008 33.378101 NaN
    907 11.912410 2013-10-09 000020.SZA 11.702191 11.702191 11.912410 2432.939941 2453.583008 23.614601 NaN
    968 84.547058 2013-10-09 000021.SZA 81.863022 81.863022 84.547058 2432.939941 2453.583008 166.410080 NaN
    1029 53.435829 2013-10-09 000022.SZA 50.562153 50.562153 53.435829 2432.939941 2453.583008 103.997986 NaN

    查看构建出来的衍生因子

    In [67]:
    df.tail()
    
    Out[67]:
    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) , 60)
    5499 41.433651 2014-12-25 000023.SZA 39.013401 39.013401 41.433651 3254.479980 3335.418945 80.447052 0.018573
    5500 41.723030 2014-12-26 000023.SZA 42.670082 42.670082 41.723030 3343.639893 3445.839111 84.393112 0.021356
    5501 41.801949 2014-12-29 000023.SZA 41.039043 41.039043 41.801949 3502.183105 3455.455078 82.840988 0.021356
    5502 41.696720 2014-12-30 000023.SZA 41.565186 41.565186 41.696720 3450.813965 3457.553955 83.261902 0.021346
    5503 41.828259 2014-12-31 000023.SZA 41.696720 41.696720 41.828259 3462.388916 3533.705078 83.524979 0.021603