请问如何取到指数的因子

用户成长系列
标签: #<Tag:0x00007fb11accab78>

(sunxking) #1

用以下代码可以取出个股的因子
D.features(instruments=[‘000001.SZA’], start_date=‘2017-01-01’, end_date=‘2017-01-07’, fields=[‘close_1’])

但是无法获取指数的因子,例如:‘000001.HIX’
请问如何获取指数的因子


(达达) #2

指数是没有因子库数据的,您可以使用衍生特征抽取用表达式来自定义

比如先用数据源模块抽取指数日线行情数据表中的000001.HIX指数的close,然后连接衍生特征抽取模块,利用输入特征列表模块定义一个因子close_1=shift(close,1)

克隆策略

    {"Description":"实验创建于2019/10/28","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-106:input_data","SourceOutputPortId":"-87:data"},{"DestinationInputPortId":"-87:instruments","SourceOutputPortId":"-93:data"},{"DestinationInputPortId":"-106:features","SourceOutputPortId":"-101:data"}],"ModuleNodes":[{"Id":"-87","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_index_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-87"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-87"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-87","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-93","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-03-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000001.HIX","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-93"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-93","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-101","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nclose_1=shift(close,1)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-101"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-101","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-106","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":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-106"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-106"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"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='-87' Position='110,206,200,200'/><NodePosition Node='-93' Position='-55,64,200,200'/><NodePosition Node='-101' Position='400,195,200,200'/><NodePosition Node='-106' Position='113,311,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 [1]:
    # 本代码由可视化策略环境自动生成 2019年10月28日 14:00
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m2 = M.instruments.v2(
        start_date='2019-01-01',
        end_date='2019-03-01',
        market='CN_STOCK_A',
        instrument_list='000001.HIX',
        max_count=0
    )
    
    m1 = M.use_datasource.v1(
        instruments=m2.data,
        datasource_id='bar1d_index_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m3 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    close_1=shift(close,1)"""
    )
    
    m4 = M.derived_feature_extractor.v3(
        input_data=m1.data,
        features=m3.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    In [3]:
    m4.data.read_df().head()
    
    Out[3]:
    adjust_factor amount close date high instrument low open turn volume close_1
    0 1.0 9.759257e+10 2465.290039 2019-01-02 2500.280029 000001.HIX 2456.419922 2497.879883 NaN 1.099320e+10 NaN
    1 1.0 1.069228e+11 2464.360107 2019-01-03 2488.479980 000001.HIX 2455.929932 2461.780029 NaN 1.243975e+10 2465.290039
    2 1.0 1.392987e+11 2514.870117 2019-01-04 2515.320068 000001.HIX 2440.909912 2446.020020 NaN 1.688777e+10 2464.360107
    3 1.0 1.455132e+11 2533.090088 2019-01-07 2536.979980 000001.HIX 2515.510010 2528.699951 NaN 1.773050e+10 2514.870117
    4 1.0 1.233790e+11 2526.459961 2019-01-08 2531.340088 000001.HIX 2520.159912 2530.300049 NaN 1.580992e+10 2533.090088