【模板案例】分组计算

策略分享
meetup
标签: #<Tag:0x00007f8c5d6c3630> #<Tag:0x00007f8c5d6c34f0>

(iQuant) #1

10月15日Meetup模板案例:分组计算

克隆策略

    {"Description":"实验创建于2020/10/15","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-30:features","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-62:features","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-62:input_data","SourceOutputPortId":"-30:data"},{"DestinationInputPortId":"-30:instruments","SourceOutputPortId":"-36:data"},{"DestinationInputPortId":"-86:input_1","SourceOutputPortId":"-62:data"}],"ModuleNodes":[{"Id":"-25","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nfactor=pe_ttm_0\nret=shift(return_0-1,-1)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-25"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-25","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-30","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","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":"-30"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-30"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-30","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-36","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":"-36"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-36","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-62","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":"-62"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-86","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1,N):\n # 示例代码如下。在这里编写您的代码\n \n df = input_1.read_df().sort_values(by=['date','factor'],ascending=True)\n df['ranker'] = df.groupby('date',group_keys=False)['factor'].rank(pct=True)\n groups = [ 0.1 * k for k in range(11)]\n df['group'] = df.groupby('date',group_keys=False)['ranker'].apply(lambda x:pd.cut(x, groups,labels=[k for k in range(1,11)]))\n grouped_processed_df = df.groupby(['date','group'])['ret'].agg(np.mean).fillna(0).reset_index()\n \n ret_df = pd.pivot_table(grouped_processed_df,values='ret',index='date',columns='group').fillna(0)\n \n ret_filter = ret_df.iloc[0::N]\n \n results = (1+ret_filter).cumprod()/(1+ret_filter.iloc[0])\n \n data_2 = DataSource.write_pickle(['factor'])\n data_1 = DataSource.write_df(results)\n return Outputs(data_1=data_1,data_2=data_2)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n results = outputs.data_1.read()\n factor = outputs.data_2.read_pickle()[0]\n T.plot(results[[1,2,3,4,5]], \n options={\n 'legend':{'enabled': True},\n 'chart': {'type': 'line'},\n 'title': {'text': factor+' :因子分组收益'}})\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"input_1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{\"N\":1}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-86"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-86"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-86","OutputType":null},{"Name":"data_2","NodeId":"-86","OutputType":null},{"Name":"data_3","NodeId":"-86","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":1,"IsPartOfPartialRun":null,"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='-25' Position='198,103,200,200'/><NodePosition Node='-30' Position='81,210,200,200'/><NodePosition Node='-36' Position='-99,114,200,200'/><NodePosition Node='-62' Position='144,283,200,200'/><NodePosition Node='-86' Position='231.83181762695312,389.9818115234375,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 [1]:
    # 本代码由可视化策略环境自动生成 2020年10月15日 16:51
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m1_run_bigquant_run(input_1,N):
        # 示例代码如下。在这里编写您的代码
        
        df = input_1.read_df().sort_values(by=['date','factor'],ascending=True)
        df['ranker'] = df.groupby('date',group_keys=False)['factor'].rank(pct=True)
        groups = [ 0.1 * k for k in range(11)]
        df['group'] = df.groupby('date',group_keys=False)['ranker'].apply(lambda x:pd.cut(x, groups,labels=[k for k in range(1,11)]))
        grouped_processed_df = df.groupby(['date','group'])['ret'].agg(np.mean).fillna(0).reset_index()
        
        ret_df = pd.pivot_table(grouped_processed_df,values='ret',index='date',columns='group').fillna(0)
        
        ret_filter = ret_df.iloc[0::N]
        
        results = (1+ret_filter).cumprod()/(1+ret_filter.iloc[0])
        
        data_2 = DataSource.write_pickle(['factor'])
        data_1 = DataSource.write_df(results)
        return Outputs(data_1=data_1,data_2=data_2)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m1_post_run_bigquant_run(outputs):
        results = outputs.data_1.read()
        factor = outputs.data_2.read_pickle()[0]
        T.plot(results[[1,2,3,4,5]], 
               options={
                   'legend':{'enabled': True},
                   'chart': {'type': 'line'},
                   'title': {'text': factor+' :因子分组收益'}})
        return outputs
    
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    factor=pe_ttm_0
    ret=shift(return_0-1,-1)"""
    )
    
    m4 = M.instruments.v2(
        start_date='2019-01-01',
        end_date='2019-03-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m3 = M.general_feature_extractor.v7(
        instruments=m4.data,
        features=m2.data,
        start_date='',
        end_date='',
        before_start_days=90
    )
    
    m5 = 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={}
    )
    
    m1 = M.cached.v3(
        input_1=m5.data,
        run=m1_run_bigquant_run,
        post_run=m1_post_run_bigquant_run,
        input_ports='input_1',
        params='{"N":1}',
        output_ports='',
        m_cached=False
    )
    

    BigQuant AI量化专家Meetup(更新至11月19日)