相对收益率特征提取不成功,老师帮忙指点,

新手专区
标签: #<Tag:0x00007fc4d7de3ae8>

(nanquant) #1

本代码由可视化策略环境自动生成 2020年3月17日 15:59

本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。

def calcu_relative_ret(lenth,df):
# 先获取hs300指数数据
start_date = min(m7.data.read_df().date).strftime(’%Y-%m-%d’)
end_date = max(m7.data.read_df().date).strftime(’%Y-%m-%d’)
hs300_df = D.history_data(
‘000300.SHA’,
start_date=(pd.to_datetime(start_date) - datetime.timedelta(days=10)).strftime(’%Y-%m-%d’), # 多取几天的数据
end_date=end_date)[[‘date’, ‘close’]].rename(columns={‘close’: ‘hs300_close’})

# 与个股数据合并
df = df[['date', 'close_0']].reset_index().merge(hs300_df, on='date', how='left').set_index('index')

# 返回超额收益率
return df['close_0'].pct_change(lenth) - df['hs300_close'].pct_change(lenth)

按股票代码groupby计算个股超额收益率数据

def relative_ret(lenth, close_0,df):
return df.groupby(‘instrument’, group_keys=False).apply(calcu_relative_ret)

m8_user_functions_bigquant_run = {
‘relative_ret’: relative_ret
}

m1 = M.instruments.v2(
start_date=‘2017-01-01’,
end_date=‘2018-01-01’,
market=‘CN_STOCK_A’,
instrument_list="""600000.SHA
600010.SHA
600015.SHA
600016.SHA
600018.SHA
600028.SHA
600029.SHA
600030.SHA
600036.SHA
600048.SHA

“”",
max_count=0
)

m2 = M.input_features.v1(
features="""return_5
sum(mf_net_amount_l_0, 5)
close_0
market_cap_float_0
close_0/close_5-1

“”"
)

m7 = M.general_feature_extractor.v7(
instruments=m1.data,
features=m2.data,
start_date=’’,
end_date=’’,
m_cached=False
)

m4 = M.input_features.v1(
features=‘relativ_ret_5 = relative_ret(5,close_0,df)’
)

m8 = M.derived_feature_extractor.v3(
input_data=m7.data,
features=m4.data,
date_col=‘date’,
instrument_col=‘instrument’,
drop_na=False,
remove_extra_columns=False,
user_functions=m8_user_functions_bigquant_run
)

m9 = M.derived_feature_extractor.v3(
input_data=m8.data,
features=m2.data,
date_col=‘date’,
instrument_col=‘instrument’,
drop_na=False,
remove_extra_columns=False,
user_functions={}
)


(达达) #2
克隆策略

    {"Description":"实验创建于2020/3/18","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-319:input_2","SourceOutputPortId":"-305:data"},{"DestinationInputPortId":"-319:input_1","SourceOutputPortId":"-309:data"},{"DestinationInputPortId":"-325:instruments","SourceOutputPortId":"-309:data"},{"DestinationInputPortId":"-345:data1","SourceOutputPortId":"-319:data_1"},{"DestinationInputPortId":"-336:input_data","SourceOutputPortId":"-325:data"},{"DestinationInputPortId":"-325:features","SourceOutputPortId":"-331:data"},{"DestinationInputPortId":"-336:features","SourceOutputPortId":"-331:data"},{"DestinationInputPortId":"-345:data2","SourceOutputPortId":"-336:data"},{"DestinationInputPortId":"-365:input_data","SourceOutputPortId":"-345:data"},{"DestinationInputPortId":"-365:features","SourceOutputPortId":"-360:data"}],"ModuleNodes":[{"Id":"-305","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nbm_ret=close/shift(close,1)-1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-305"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-305","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-309","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":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-309"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-309","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-319","ModuleId":"BigQuantSpace.index_feature_extract.index_feature_extract-v3","ModuleParameters":[{"Name":"before_days","Value":100,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"index","Value":"000001.HIX","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-319"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-319"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-319","OutputType":null},{"Name":"data_2","NodeId":"-319","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-325","ModuleId":"BigQuantSpace.general_feature_extractor_vx1.general_feature_extractor_vx1-v1","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":"-325"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-325"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-325","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-331","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nret=return_0 -1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-331"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-331","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-336","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":"True","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":"-336"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-336"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-336","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-345","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-345"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-345"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-345","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"Comment":"","CommentCollapsed":true},{"Id":"-360","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nrelative_ret=ret-bm_ret","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-360"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-360","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"Comment":"","CommentCollapsed":true},{"Id":"-365","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":"-365"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-365"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-365","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"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='-305' Position='-199.91612339019775,239.4091339111328,200,200'/><NodePosition Node='-309' Position='119.56366729736328,236.49566650390625,200,200'/><NodePosition Node='-319' Position='-87.75227355957031,347.21124267578125,200,200'/><NodePosition Node='-325' Position='231.66558837890625,347.27130126953125,200,200'/><NodePosition Node='-331' Position='424.0030517578125,234.00592041015625,200,200'/><NodePosition Node='-336' Position='231.66558837890625,425.3310241699219,200,200'/><NodePosition Node='-345' Position='56.56428527832031,502.74078369140625,200,200'/><NodePosition Node='-360' Position='361.9598693847656,503.17730712890625,200,200'/><NodePosition Node='-365' Position='182.44598388671875,610.8648681640625,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":true}
    In [16]:
    # 本代码由可视化策略环境自动生成 2020年3月18日 09:47
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    bm_ret=close/shift(close,1)-1"""
    )
    
    m3 = M.instruments.v2(
        start_date='2019-01-01',
        end_date='2019-03-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m4 = M.index_feature_extract.v3(
        input_1=m3.data,
        input_2=m2.data,
        before_days=100,
        index='000001.HIX'
    )
    
    m5 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    ret=return_0 -1"""
    )
    
    m1 = M.general_feature_extractor_vx1.v1(
        instruments=m3.data,
        features=m5.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m6 = M.derived_feature_extractor.v3(
        input_data=m1.data,
        features=m5.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m7 = M.join.v3(
        data1=m4.data_1,
        data2=m6.data,
        on='date',
        how='inner',
        sort=False
    )
    
    m9 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    relative_ret=ret-bm_ret"""
    )
    
    m8 = M.derived_feature_extractor.v3(
        input_data=m7.data,
        features=m9.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    In [18]:
    m8.data.read()
    
    Out[18]:
    date instrument return_0 ret bm_ret relative_ret
    0 2018-10-08 002415.SZA 0.900139 -0.099861 -0.037160 -0.062701
    1 2018-10-08 300177.SZA 0.974152 -0.025848 -0.037160 0.011311
    2 2018-10-08 600527.SHA 0.987234 -0.012766 -0.037160 0.024394
    3 2018-10-08 300003.SZA 0.926853 -0.073147 -0.037160 -0.035987
    4 2018-10-08 300267.SZA 1.029925 0.029925 -0.037160 0.067085
    5 2018-10-08 002392.SZA 0.968391 -0.031609 -0.037160 0.005550
    6 2018-10-08 600756.SHA 0.920198 -0.079802 -0.037160 -0.042642
    7 2018-10-08 002596.SZA 0.940934 -0.059066 -0.037160 -0.021906
    8 2018-10-08 002482.SZA 0.989565 -0.010435 -0.037160 0.026725
    9 2018-10-08 603339.SHA 0.950119 -0.049881 -0.037160 -0.012722
    10 2018-10-08 002632.SZA 0.947945 -0.052055 -0.037160 -0.014895
    11 2018-10-08 002060.SZA 0.976431 -0.023569 -0.037160 0.013591
    12 2018-10-08 600561.SHA 0.976271 -0.023729 -0.037160 0.013431
    13 2018-10-08 601611.SHA 0.975484 -0.024516 -0.037160 0.012643
    14 2018-10-08 603027.SHA 0.954802 -0.045198 -0.037160 -0.008038
    15 2018-10-08 300651.SZA 0.988001 -0.011999 -0.037160 0.025161
    16 2018-10-08 600496.SHA 0.980000 -0.020000 -0.037160 0.017160
    17 2018-10-08 600155.SHA 0.967049 -0.032951 -0.037160 0.004208
    18 2018-10-08 600008.SHA 0.978892 -0.021108 -0.037160 0.016051
    19 2018-10-08 300645.SZA 0.972557 -0.027443 -0.037160 0.009716
    20 2018-10-08 600967.SHA 0.982659 -0.017341 -0.037160 0.019818
    21 2018-10-08 000719.SZA 0.985255 -0.014745 -0.037160 0.022414
    22 2018-10-08 002682.SZA 0.959924 -0.040076 -0.037160 -0.002917
    23 2018-10-08 002223.SZA 0.969876 -0.030124 -0.037160 0.007036
    24 2018-10-08 002385.SZA 0.975543 -0.024457 -0.037160 0.012703
    25 2018-10-08 300630.SZA 0.953481 -0.046519 -0.037160 -0.009360
    26 2018-10-08 600129.SHA 0.971660 -0.028340 -0.037160 0.008820
    27 2018-10-08 600151.SHA 0.983373 -0.016627 -0.037160 0.020532
    28 2018-10-08 002765.SZA 0.949367 -0.050633 -0.037160 -0.013473
    29 2018-10-08 002038.SZA 0.947450 -0.052550 -0.037160 -0.015390
    ... ... ... ... ... ... ...
    345910 2019-03-01 600283.SHA 1.006426 0.006426 0.018042 -0.011616
    345911 2019-03-01 600331.SHA 1.031646 0.031646 0.018042 0.013604
    345912 2019-03-01 600230.SHA 1.014978 0.014978 0.018042 -0.003064
    345913 2019-03-01 600298.SHA 1.029732 0.029732 0.018042 0.011690
    345914 2019-03-01 600329.SHA 0.997974 -0.002026 0.018042 -0.020068
    345915 2019-03-01 600328.SHA 1.000000 0.000000 0.018042 -0.018042
    345916 2019-03-01 600327.SHA 1.008163 0.008163 0.018042 -0.009879
    345917 2019-03-01 600326.SHA 1.025641 0.025641 0.018042 0.007599
    345918 2019-03-01 600325.SHA 0.997297 -0.002703 0.018042 -0.020745
    345919 2019-03-01 600323.SHA 1.005096 0.005096 0.018042 -0.012946
    345920 2019-03-01 600322.SHA 1.011527 0.011527 0.018042 -0.006514
    345921 2019-03-01 600321.SHA 1.022321 0.022321 0.018042 0.004280
    345922 2019-03-01 600320.SHA 1.086162 0.086162 0.018042 0.068120
    345923 2019-03-01 600319.SHA 1.007678 0.007678 0.018042 -0.010364
    345924 2019-03-01 600318.SHA 1.039823 0.039823 0.018042 0.021781
    345925 2019-03-01 600301.SHA 1.018433 0.018433 0.018042 0.000391
    345926 2019-03-01 600300.SHA 0.996904 -0.003096 0.018042 -0.021138
    345927 2019-03-01 600315.SHA 1.012912 0.012912 0.018042 -0.005130
    345928 2019-03-01 600313.SHA 1.000000 0.000000 0.018042 -0.018042
    345929 2019-03-01 600312.SHA 0.991304 -0.008696 0.018042 -0.026738
    345930 2019-03-01 600311.SHA 0.994521 -0.005479 0.018042 -0.023521
    345931 2019-03-01 600310.SHA 1.023913 0.023913 0.018042 0.005871
    345932 2019-03-01 600309.SHA 1.029606 0.029606 0.018042 0.011564
    345933 2019-03-01 600308.SHA 0.997984 -0.002016 0.018042 -0.020058
    345934 2019-03-01 600307.SHA 1.008547 0.008547 0.018042 -0.009495
    345935 2019-03-01 600306.SHA 1.002845 0.002845 0.018042 -0.015197
    345936 2019-03-01 600305.SHA 1.017355 0.017355 0.018042 -0.000686
    345937 2019-03-01 600303.SHA 0.997788 -0.002212 0.018042 -0.020254
    345938 2019-03-01 600316.SHA 1.016755 0.016755 0.018042 -0.001286
    345939 2019-03-01 600302.SHA 1.007737 0.007737 0.018042 -0.010305

    345940 rows × 6 columns