克隆策略

    {"description":"实验创建于2021/9/1","graph":{"edges":[{"to_node_id":"-149:instruments","from_node_id":"-136:data"},{"to_node_id":"-149:features","from_node_id":"-144:data"},{"to_node_id":"-160:input_data","from_node_id":"-149:data"},{"to_node_id":"-160:features","from_node_id":"-155:data"}],"nodes":[{"node_id":"-136","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2017-01-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2018-01-01","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"600000.SHA\n600004.SHA\n600010.SHA\n600012.SHA\n000012.SZA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-136"}],"output_ports":[{"name":"data","node_id":"-136"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-144","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nreturn_5\nclose_0\nmf_net_amount_10\nmarket_cap_float_0\nclose_0/close_5 - 1","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-144"}],"output_ports":[{"name":"data","node_id":"-144"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-149","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":90,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-149"},{"name":"features","node_id":"-149"}],"output_ports":[{"name":"data","node_id":"-149"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-155","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nrelative_ret(close_0)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-155"}],"output_ports":[{"name":"data","node_id":"-155"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-160","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":"{\n 'relative_ret': relative_ret\n}\n\ndef relative_ret(df, close_0):\n return df.groupby('instrument', group_keys=False).apply(calcu_relative_ret)\n\ndef calcu_relative_ret(df):\n # 先获取中证800指数数据\n start_date = min(m7.data.read_df().date).strftime('%Y-%m-%d')\n end_date = max(m7.data.read_df().date).strftime('%Y-%m-%d')\n hs800_df = D.history_data(\n '000906.SHA',\n start_date=(pd.to_datetime(start_date) - datetime.timedelta(days=10)).strftime('%Y-%m-%d'), # 多取几天的数据\n end_date=end_date)[['date', 'close']].rename(columns={'close': 'hs800_close'})\n \n # 与个股数据合并\n df = df[['date', 'close_0']].reset_index().merge(hs800_df, on='date', how='left').set_index('index')\n \n # 返回超额收益率\n return df['close_0'].pct_change() - df['hs800_close'].pct_change()","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-160"},{"name":"features","node_id":"-160"}],"output_ports":[{"name":"data","node_id":"-160"}],"cacheable":true,"seq_num":5,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-136' Position='443.2216796875,34.53517532348633,200,200'/><node_position Node='-144' Position='771.0216064453125,34.53517532348633,200,200'/><node_position Node='-149' Position='509.22161865234375,163.2351837158203,200,200'/><node_position Node='-155' Position='841.2579956054688,157.9261016845703,200,200'/><node_position Node='-160' Position='688.4116821289062,275.6260986328125,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [44]:
    # 本代码由可视化策略环境自动生成 2021年9月1日 11:29
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m1 = M.instruments.v2(
        start_date='2017-01-01',
        end_date='2018-01-01',
        market='CN_STOCK_A',
        instrument_list="""600000.SHA
    600004.SHA
    600010.SHA
    600012.SHA
    000012.SZA""",
        max_count=0
    )
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    return_5
    close_0
    mf_net_amount_10
    market_cap_float_0
    close_0/close_5 - 1"""
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m4 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    relative_ret(close_0)"""
    )
    
    m5 = M.derived_feature_extractor.v3(
        input_data=m3.data,
        features=m4.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={
        'relative_ret':  relative_ret
    }
    
    def relative_ret(df, close_0):
        return df.groupby('instrument', group_keys=False).apply(calcu_relative_ret)
    
    def calcu_relative_ret(df):
        # 先获取中证800指数数据
        start_date = min(m7.data.read_df().date).strftime('%Y-%m-%d')
        end_date = max(m7.data.read_df().date).strftime('%Y-%m-%d')
        hs800_df = D.history_data(
        '000906.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': 'hs800_close'})
        
        # 与个股数据合并
        df = df[['date', 'close_0']].reset_index().merge(hs800_df, on='date', how='left').set_index('index')
        
        # 返回超额收益率
        return df['close_0'].pct_change() - df['hs800_close'].pct_change()
    )
    
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-44-2544e875d2da> in <module>
         49     remove_extra_columns=False,
         50     user_functions= {
    ---> 51     'relative_ret':  relative_ret
         52 }
         53 
    
    NameError: name 'relative_ret' is not defined
    In [ ]:
    m3.data.read_df()