自定义模块教程-日期增加天数

自定义模块
标签: #<Tag:0x00007fb3dc659888>

(iQuant) #1

参考 自定义模块教程-开发一个修改数据列名的模块为例-超级详细版 开发自定义模块

克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-380:input_1","SourceOutputPortId":"-185:data"},{"DestinationInputPortId":"-27:input_ds","SourceOutputPortId":"-185:data"}],"ModuleNodes":[{"Id":"-185","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"2018-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-02-01","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-185"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-185"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-185","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-380","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"def bigquant_run(input_ds, add_days, date_column_name):\n add_days = float(add_days)\n dataset_ds = DataSource()\n output_store = dataset_ds.open_df_store()\n\n for key, df in input_ds.iter_df():\n if date_column_name not in df.columns:\n print('错误:找不到日期列:%s' % date_column_name)\n return\n df[date_column_name] += datetime.timedelta(days=add_days)\n df.to_hdf(output_store, key)\n row_count = len(df)\n print('%s: %s' % (key, len(df)))\n\n dataset_ds.close_df_store()\n return Outputs(data=dataset_ds)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"input_ds","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{\n 'add_days': 10,\n 'date_column_name': 'date'\n}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"data","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-380"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-380"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-380"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-380","OutputType":null},{"Name":"data_2","NodeId":"-380","OutputType":null},{"Name":"data_3","NodeId":"-380","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-27","ModuleId":"BigQuantSpace.date_add_days.date_add_days-v2","ModuleParameters":[{"Name":"add_days","Value":"1","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"date_column_name","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-27"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-27","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"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='-185' Position='1114,4,200,200'/><NodePosition Node='-380' Position='954,181,200,200'/><NodePosition Node='-27' Position='1301.360595703125,222.743896484375,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 [19]:
    # 本代码由可视化策略环境自动生成 2018年8月8日 14:47
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m4 = M.use_datasource.v1(
        datasource_id='bar1d_CN_STOCK_A',
        start_date='2018-01-01',
        end_date='2018-02-01'
    )
    
    def m2_run_bigquant_run(input_ds, add_days, date_column_name):
        add_days = float(add_days)
        dataset_ds = DataSource()
        output_store = dataset_ds.open_df_store()
    
        for key, df in input_ds.iter_df():
            if date_column_name not in df.columns:
                print('错误:找不到日期列:%s' % date_column_name)
                return
            df[date_column_name] += datetime.timedelta(days=add_days)
            df.to_hdf(output_store, key)
            row_count = len(df)
            print('%s: %s' % (key, len(df)))
    
        dataset_ds.close_df_store()
        return Outputs(data=dataset_ds)
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m2_post_run_bigquant_run(outputs):
        return outputs
    
    m2 = M.cached.v3(
        input_1=m4.data,
        run=m2_run_bigquant_run,
        post_run=m2_post_run_bigquant_run,
        input_ports='input_ds',
        params="""{
        'add_days': 10,
        'date_column_name': 'date'
    }""",
        output_ports='data'
    )
    
    m3 = M.date_add_days.v2(
        input_ds=m4.data,
        add_days=1,
        date_column_name='date'
    )
    
    [2018-08-08 14:47:14.281762] INFO: bigquant: use_datasource.v1 开始运行..
    [2018-08-08 14:47:14.288652] INFO: bigquant: 命中缓存
    [2018-08-08 14:47:14.289660] INFO: bigquant: use_datasource.v1 运行完成[0.0079s].
    [2018-08-08 14:47:14.293554] INFO: bigquant: date_add_days.v2 开始运行..
    [2018-08-08 14:47:14.299207] INFO: bigquant: 命中缓存
    [2018-08-08 14:47:14.300853] INFO: bigquant: date_add_days.v2 运行完成[0.00727s].