复制链接
克隆策略

    {"description":"实验创建于2021/12/21","graph":{"edges":[{"to_node_id":"-419:features","from_node_id":"-406:data"},{"to_node_id":"-419:instruments","from_node_id":"-410:data"},{"to_node_id":"-173:input_1","from_node_id":"-419:data"}],"nodes":[{"node_id":"-173","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3, SW_type, SW_name):\n basic_data = input_1.read()\n start_date = str(basic_data.date.min())\n end_date = str(basic_data.date.max())\n SW_data = DataSource('basic_info_IndustrySw').read()\n SW_data['code'] = SW_data.code.astype('int')\n SW_data_2014_1 = SW_data[(SW_data.version==2014)&(SW_data.industry_sw_level==1)][['code','name']].rename(columns={'code':'industry_sw_level1_0','name':'name_SW1'})\n SW_data_2014_2 = SW_data[(SW_data.version==2014)&(SW_data.industry_sw_level==2)][['code','name']].rename(columns={'code':'industry_sw_level2_0','name':'name_SW2'})\n SW_data_2014_3 = SW_data[(SW_data.version==2014)&(SW_data.industry_sw_level==3)][['code','name']].rename(columns={'code':'industry_sw_level3_0','name':'name_SW3'})\n SW_data_2021_1 = SW_data[(SW_data.version==2021)&(SW_data.industry_sw_level==1)][['code','name']].rename(columns={'code':'industry_sw_level1_0','name':'name_SW1'})\n SW_data_2021_2 = SW_data[(SW_data.version==2021)&(SW_data.industry_sw_level==2)][['code','name']].rename(columns={'code':'industry_sw_level2_0','name':'name_SW2'})\n SW_data_2021_3 = SW_data[(SW_data.version==2021)&(SW_data.industry_sw_level==3)][['code','name']].rename(columns={'code':'industry_sw_level3_0','name':'name_SW3'})\n # 提取所需数据\n# basic_data = DataSource('features_CN_STOCK_A_G300').read(start_date=start_date,end_date=end_date,fields=['industry_sw_level1_0','industry_sw_level2_0','industry_sw_level3_0','market_cap_float_0','daily_return_0'])\n# basic_data = basic_data[basic_data.industry_sw_level1_0 !=0]\n basic_data['daily_return_0'] = basic_data['daily_return_0']-1\n\n if end_date < '2021-12-13':\n basic_data = basic_data.merge(SW_data_2014_1,how='left',on='industry_sw_level1_0')\n basic_data = basic_data.merge(SW_data_2014_2,how='left',on='industry_sw_level2_0')\n basic_data = basic_data.merge(SW_data_2014_3,how='left',on='industry_sw_level3_0')\n else:\n basic_data_2014 = basic_data[basic_data.date < '2021-12-13']\n basic_data_2014 = basic_data_2014.merge(SW_data_2014_1,how='left',on='industry_sw_level1_0')\n basic_data_2014 = basic_data_2014.merge(SW_data_2014_2,how='left',on='industry_sw_level2_0')\n basic_data_2014 = basic_data_2014.merge(SW_data_2014_3,how='left',on='industry_sw_level3_0')\n basic_data_2021 = basic_data[basic_data.date >= '2021-12-13']\n basic_data_2021 = basic_data_2021.merge(SW_data_2021_1,how='left',on='industry_sw_level1_0')\n basic_data_2021 = basic_data_2021.merge(SW_data_2021_2,how='left',on='industry_sw_level2_0')\n basic_data_2021 = basic_data_2021.merge(SW_data_2021_3,how='left',on='industry_sw_level3_0')\n basic_data = pd.concat([basic_data_2014,basic_data_2021])\n \n def calculate_daily_ret(df):\n df['ret'] = df['daily_return_0'] * (df['market_cap_float_0']/df['market_cap_float_0'].sum())\n return df\n \n SW_type = SW_type\n SW_name = SW_name\n \n SW_daily_ret = basic_data.groupby([SW_type,'date']).apply(calculate_daily_ret)\n \n SW_daily_ret = SW_daily_ret[SW_daily_ret[SW_type]==SW_name]\n\n SW_daily_ret = SW_daily_ret[['date','ret']].groupby('date').sum()\n \n #第一天收益为0\n SW_daily_ret['ret'][0:1]=0\n \n SW_daily_ret['cum_ret'] = (SW_daily_ret['ret']+1).cumprod()-1\n \n SW_index_dict = {'SW_type':SW_type,'SW_name':SW_name}\n \n data_1 = DataSource.write_df(SW_daily_ret)\n data_2 = DataSource.write_pickle(SW_index_dict)\n return Outputs(data_1=data_1, data_2=data_2, data_3=None)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n daily_ret = outputs.data_1.read()\n index_info = outputs.data_2.read()\n# daily_ret.set_index(\"date\", inplace=True)\n # 画图\n options={\"series\": [{\"color\": \"#FF0000\"}]}\n T.plot(df=daily_ret[[\"cum_ret\"]], title=index_info['SW_name']+\"行业指数\", options=options, double_precision=6)\n \n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{\n 'SW_type':'name_SW1','SW_name':'基础化工'\n}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"data_1,data_2","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-173"},{"name":"input_2","node_id":"-173"},{"name":"input_3","node_id":"-173"}],"output_ports":[{"name":"data_1","node_id":"-173"},{"name":"data_2","node_id":"-173"},{"name":"data_3","node_id":"-173"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-406","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"industry_sw_level1_0\nindustry_sw_level2_0\nindustry_sw_level3_0\nmarket_cap_float_0\ndaily_return_0\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-406"}],"output_ports":[{"name":"data","node_id":"-406"}],"cacheable":true,"seq_num":2,"comment":"","comment_collapsed":true},{"node_id":"-410","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-20","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":"-410"}],"output_ports":[{"name":"data","node_id":"-410"}],"cacheable":true,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-419","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":"-419"},{"name":"features","node_id":"-419"}],"output_ports":[{"name":"data","node_id":"-419"}],"cacheable":true,"seq_num":4,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-173' Position='196.55307006835938,175.4047555923462,200,200'/><node_position Node='-406' Position='422.133056640625,-126.18618774414062,200,200'/><node_position Node='-410' Position='-1.664825439453125,-122.78453063964844,200,200'/><node_position Node='-419' Position='178.91815185546875,16.959073066711426,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [8]:
    # 本代码由可视化策略环境自动生成 2021年12月22日 11:16
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m1_run_bigquant_run(input_1, input_2, input_3, SW_type, SW_name):
        basic_data = input_1.read()
        start_date = str(basic_data.date.min())
        end_date = str(basic_data.date.max())
        SW_data = DataSource('basic_info_IndustrySw').read()
        SW_data['code'] = SW_data.code.astype('int')
        SW_data_2014_1 = SW_data[(SW_data.version==2014)&(SW_data.industry_sw_level==1)][['code','name']].rename(columns={'code':'industry_sw_level1_0','name':'name_SW1'})
        SW_data_2014_2 = SW_data[(SW_data.version==2014)&(SW_data.industry_sw_level==2)][['code','name']].rename(columns={'code':'industry_sw_level2_0','name':'name_SW2'})
        SW_data_2014_3 = SW_data[(SW_data.version==2014)&(SW_data.industry_sw_level==3)][['code','name']].rename(columns={'code':'industry_sw_level3_0','name':'name_SW3'})
        SW_data_2021_1 = SW_data[(SW_data.version==2021)&(SW_data.industry_sw_level==1)][['code','name']].rename(columns={'code':'industry_sw_level1_0','name':'name_SW1'})
        SW_data_2021_2 = SW_data[(SW_data.version==2021)&(SW_data.industry_sw_level==2)][['code','name']].rename(columns={'code':'industry_sw_level2_0','name':'name_SW2'})
        SW_data_2021_3 = SW_data[(SW_data.version==2021)&(SW_data.industry_sw_level==3)][['code','name']].rename(columns={'code':'industry_sw_level3_0','name':'name_SW3'})
        # 提取所需数据
    #     basic_data = DataSource('features_CN_STOCK_A_G300').read(start_date=start_date,end_date=end_date,fields=['industry_sw_level1_0','industry_sw_level2_0','industry_sw_level3_0','market_cap_float_0','daily_return_0'])
    #     basic_data = basic_data[basic_data.industry_sw_level1_0 !=0]
        basic_data['daily_return_0'] = basic_data['daily_return_0']-1
    
        if end_date < '2021-12-13':
            basic_data = basic_data.merge(SW_data_2014_1,how='left',on='industry_sw_level1_0')
            basic_data = basic_data.merge(SW_data_2014_2,how='left',on='industry_sw_level2_0')
            basic_data = basic_data.merge(SW_data_2014_3,how='left',on='industry_sw_level3_0')
        else:
            basic_data_2014 = basic_data[basic_data.date < '2021-12-13']
            basic_data_2014 = basic_data_2014.merge(SW_data_2014_1,how='left',on='industry_sw_level1_0')
            basic_data_2014 = basic_data_2014.merge(SW_data_2014_2,how='left',on='industry_sw_level2_0')
            basic_data_2014 = basic_data_2014.merge(SW_data_2014_3,how='left',on='industry_sw_level3_0')
            basic_data_2021 = basic_data[basic_data.date >= '2021-12-13']
            basic_data_2021 = basic_data_2021.merge(SW_data_2021_1,how='left',on='industry_sw_level1_0')
            basic_data_2021 = basic_data_2021.merge(SW_data_2021_2,how='left',on='industry_sw_level2_0')
            basic_data_2021 = basic_data_2021.merge(SW_data_2021_3,how='left',on='industry_sw_level3_0')
            basic_data = pd.concat([basic_data_2014,basic_data_2021])
        
        def calculate_daily_ret(df):
            df['ret'] = df['daily_return_0'] * (df['market_cap_float_0']/df['market_cap_float_0'].sum())
            return df
        
        SW_type = SW_type
        SW_name = SW_name
        
        SW_daily_ret = basic_data.groupby([SW_type,'date']).apply(calculate_daily_ret)
        
        SW_daily_ret = SW_daily_ret[SW_daily_ret[SW_type]==SW_name]
    
        SW_daily_ret = SW_daily_ret[['date','ret']].groupby('date').sum()
        
        #第一天收益为0
        SW_daily_ret['ret'][0:1]=0
        
        SW_daily_ret['cum_ret'] = (SW_daily_ret['ret']+1).cumprod()-1
        
        SW_index_dict = {'SW_type':SW_type,'SW_name':SW_name}
        
        data_1 = DataSource.write_df(SW_daily_ret)
        data_2 = DataSource.write_pickle(SW_index_dict)
        return Outputs(data_1=data_1, data_2=data_2, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m1_post_run_bigquant_run(outputs):
        daily_ret = outputs.data_1.read()
        index_info = outputs.data_2.read()
    #     daily_ret.set_index("date", inplace=True)
        # 画图
        options={"series": [{"color": "#FF0000"}]}
        T.plot(df=daily_ret[["cum_ret"]], title=index_info['SW_name']+"行业指数", options=options, double_precision=6)
        
        return outputs
    
    
    m2 = M.input_features.v1(
        features="""industry_sw_level1_0
    industry_sw_level2_0
    industry_sw_level3_0
    market_cap_float_0
    daily_return_0
    """
    )
    
    m3 = M.instruments.v2(
        start_date='2021-01-01',
        end_date='2021-12-20',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m4 = M.general_feature_extractor.v7(
        instruments=m3.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m1 = M.cached.v3(
        input_1=m4.data,
        run=m1_run_bigquant_run,
        post_run=m1_post_run_bigquant_run,
        input_ports='',
        params="""{
        'SW_type':'name_SW1','SW_name':'基础化工'
    }""",
        output_ports='data_1,data_2'
    )