复制链接
克隆策略

使用本例子介绍如何进行自定义因子构建

本例子希望: 通过talib库构建自定义因子,本案例是构建kdj指标

    {"description":"实验创建于2022/1/24","graph":{"edges":[{"to_node_id":"-449:instruments","from_node_id":"-436:data"},{"to_node_id":"-456:features","from_node_id":"-444:data"},{"to_node_id":"-449:features","from_node_id":"-444:data"},{"to_node_id":"-456:input_data","from_node_id":"-449:data"}],"nodes":[{"node_id":"-436","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2021-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2021-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":"-436"}],"output_ports":[{"name":"data","node_id":"-436"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-444","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"talib_KDJ_j(high_0,low_0,close_0)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-444"}],"output_ports":[{"name":"data","node_id":"-444"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-449","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":"0","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-449"},{"name":"features","node_id":"-449"}],"output_ports":[{"name":"data","node_id":"-449"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-456","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 talib_KDJ(df,high, low, close, fastk_period=9, slowk_period=3, slowd_period=3):\n #计算kd指标\n k, d = talib.STOCH(high, low, close, \n fastk_period=fastk_period, \n slowk_period=slowk_period, \n slowd_period=slowd_period)\n j = 3 * k - 2 *d\n return j \n\n# 因为这俩表达式是我们新自定义的表达式,因此需要声明,以便在输入特征列表可使用\nbigquant_run = {\n 'talib_KDJ_j':talib_KDJ\n}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-456"},{"name":"features","node_id":"-456"}],"output_ports":[{"name":"data","node_id":"-456"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-436' Position='26,81,200,200'/><node_position Node='-444' Position='321,76,200,200'/><node_position Node='-449' Position='218,198,200,200'/><node_position Node='-456' Position='293,306,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [43]:
    # 本代码由可视化策略环境自动生成 2022年1月24日 14:57
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    def talib_KDJ(df,high, low, close, fastk_period=9, slowk_period=3, slowd_period=3):
        #计算kd指标
        k, d = talib.STOCH(high, low, close, 
                                           fastk_period=fastk_period, 
                                           slowk_period=slowk_period, 
                                           slowd_period=slowd_period)
        j = 3 * k - 2 *d
        return j 
    
    # 因为这俩表达式是我们新自定义的表达式,因此需要声明,以便在输入特征列表可使用
    m4_user_functions_bigquant_run = {
        'talib_KDJ_j':talib_KDJ
    }
    
    m1 = M.instruments.v2(
        start_date='2021-01-01',
        end_date='2021-12-31',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features='talib_KDJ_j(high_0,low_0,close_0)'
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m4 = M.derived_feature_extractor.v3(
        input_data=m3.data,
        features=m2.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions=m4_user_functions_bigquant_run
    )