复制链接
克隆策略

    {"description":"实验创建于2022/11/24","graph":{"edges":[{"to_node_id":"-49:input_data","from_node_id":"-1452:data"},{"to_node_id":"-3513:input_data","from_node_id":"-49:data"},{"to_node_id":"-3513:features","from_node_id":"-3200:data"}],"nodes":[{"node_id":"-1452","module_id":"BigQuantSpace.use_datasource.use_datasource-v2","parameters":[{"name":"datasource_id","value":"bar1d_index_CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"start_date","value":"2013-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-11-26","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"1","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-1452"},{"name":"features","node_id":"-1452"}],"output_ports":[{"name":"data","node_id":"-1452"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"-49","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"instrument=='000001.HIX'","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-49"}],"output_ports":[{"name":"data","node_id":"-49"},{"name":"left_data","node_id":"-49"}],"cacheable":true,"seq_num":7,"comment":"","comment_collapsed":true},{"node_id":"-3200","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"volume=volume\n#close 衍生特征\nclose\nma_10=ts_mean('close',10)\nma_20=ts_mean('close',20)\nma_50=ts_mean('close',50)\n#return 衍生特征\nreturn_1=close/ts_delay('close',1)\nreturn_5=close/ts_delay('close',5)\nreturn_10=close/ts_delay('close',10)\nreturn_20=close/ts_delay('close',20)\nr1r5=return_1/return_5\nr5r10=return_5/return_10\nr10r20=return_10/return_20\n# Beta\nbeta_10=beta(10)\nbeta_20=beta(20)\nbeta_50=beta(50)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-3200"}],"output_ports":[{"name":"data","node_id":"-3200"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-3513","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":"True","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"def ts_delay(df,f,d):\n return df[f].shift(d)\ndef ts_std(df,f,d):\n return df[f].rolling(d).std()\ndef ts_mean(df,f,d):\n return df[f].rolling(d).mean()\ndef beta(df,d):\n benchmark = ['000300.SHA'] # 以沪深300为基准计算beta值\n benchmark_df=D.history_data(benchmark,fields=['close'],start_date='2015-01-01',end_date='2020-01-01')\n df[\"close_pct\"]=df['close'].pct_change()\n benchmark_df[\"close_pct\"]=benchmark_df['close'].pct_change()\n return (df['close_pct'].rolling(d).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(d).var()\nbigquant_run = {\n 'beta':beta,\n 'ts_delay':ts_delay,\n 'ts_mean':ts_mean,\n 'ts_std':ts_std,\n}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-3513"},{"name":"features","node_id":"-3513"}],"output_ports":[{"name":"data","node_id":"-3513"}],"cacheable":true,"seq_num":9,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-1452' Position='283,-154,200,200'/><node_position Node='-49' Position='292,-13,200,200'/><node_position Node='-3200' Position='-45,-152,200,200'/><node_position Node='-3513' Position='290,77,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [1]:
    # 本代码由可视化策略环境自动生成 2022年11月26日 17:16
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    def ts_delay(df,f,d):
        return df[f].shift(d)
    def ts_std(df,f,d):
        return df[f].rolling(d).std()
    def ts_mean(df,f,d):
        return df[f].rolling(d).mean()
    def beta(df,d):
        benchmark = ['000300.SHA']  # 以沪深300为基准计算beta值
        benchmark_df=D.history_data(benchmark,fields=['close'],start_date='2015-01-01',end_date='2020-01-01')
        df["close_pct"]=df['close'].pct_change()
        benchmark_df["close_pct"]=benchmark_df['close'].pct_change()
        return (df['close_pct'].rolling(d).cov(benchmark_df['close_pct']))/benchmark_df['close_pct'].rolling(d).var()
    m9_user_functions_bigquant_run = {
        'beta':beta,
        'ts_delay':ts_delay,
        'ts_mean':ts_mean,
        'ts_std':ts_std,
    }
    
    m6 = M.use_datasource.v2(
        datasource_id='bar1d_index_CN_STOCK_A',
        start_date='2013-01-01',
        end_date='2022-11-26',
        before_start_days=1
    )
    
    m7 = M.filter.v3(
        input_data=m6.data,
        expr='instrument==\'000001.HIX\'',
        output_left_data=False
    )
    
    m8 = M.input_features.v1(
        features="""volume=volume
    #close 衍生特征
    close
    ma_10=ts_mean('close',10)
    ma_20=ts_mean('close',20)
    ma_50=ts_mean('close',50)
    #return 衍生特征
    return_1=close/ts_delay('close',1)
    return_5=close/ts_delay('close',5)
    return_10=close/ts_delay('close',10)
    return_20=close/ts_delay('close',20)
    r1r5=return_1/return_5
    r5r10=return_5/return_10
    r10r20=return_10/return_20
    # Beta
    beta_10=beta(10)
    beta_20=beta(20)
    beta_50=beta(50)"""
    )
    
    m9 = M.derived_feature_extractor.v3(
        input_data=m7.data,
        features=m8.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=False,
        user_functions=m9_user_functions_bigquant_run
    )