自定义模块的简单示例

策略分享
标签: #<Tag:0x00007f73df275ad8>

(iQuant) #1
克隆策略

    {"Description":"实验创建于2017/9/27","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-24:input_1","SourceOutputPortId":"-32:data_1"}],"ModuleNodes":[{"Id":"-24","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n \n ins = D.instruments()[:len(input_1.read_df())]\n df = D.history_data(ins,'2016-01-01','2017-01-01',['close'])\n data_1 = DataSource.write_df(df)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-24"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-24"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-24"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-24","OutputType":null},{"Name":"data_2","NodeId":"-24","OutputType":null},{"Name":"data_3","NodeId":"-24","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-32","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n df = pd.Series([10,20,30])\n data_1 = DataSource.write_df(df)\n \n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-32"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-32"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-32"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-32","OutputType":null},{"Name":"data_2","NodeId":"-32","OutputType":null},{"Name":"data_3","NodeId":"-32","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"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='-24' Position='371.0507640838623,168.55397033691406,200,200'/><NodePosition Node='-32' Position='-74.1404333114624,-27.090617179870605,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":false}
    In [20]:
    # 本代码由可视化策略环境自动生成 2017年9月28日 14:54
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m6_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df = pd.Series([10,20,30])
        data_1 = DataSource.write_df(df)
         
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    m6 = M.cached.v3(
        run=m6_run_bigquant_run
    )
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m5_run_bigquant_run(input_1, input_2, input_3):
     
        ins = D.instruments()[:len(input_1.read_df())]
        df = D.history_data(ins,'2016-01-01','2017-01-01',['close'])
        data_1 = DataSource.write_df(df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    m5 = M.cached.v3(
        input_1=m6.data_1,
        run=m5_run_bigquant_run
    )
    
    [2017-09-28 14:54:07.328846] INFO: bigquant: cached.v3 开始运行..
    [2017-09-28 14:54:07.333237] INFO: bigquant: 命中缓存
    [2017-09-28 14:54:07.334827] INFO: bigquant: cached.v3 运行完成[0.005999s].
    [2017-09-28 14:54:07.345949] INFO: bigquant: cached.v3 开始运行..
    [2017-09-28 14:54:07.349429] INFO: bigquant: 命中缓存
    [2017-09-28 14:54:07.350685] INFO: bigquant: cached.v3 运行完成[0.004719s].
    
    In [22]:
    m5.data_1.read_df()
    
    Out[22]:
    close date instrument
    0 974.312988 2016-01-04 000001.SZA
    1 3113.541504 2016-01-04 000002.SZA
    2 167.878143 2016-01-04 000004.SZA
    3 980.332520 2016-01-05 000001.SZA
    4 3113.541504 2016-01-05 000002.SZA
    5 157.190186 2016-01-05 000004.SZA
    6 991.511780 2016-01-06 000001.SZA
    7 3113.541504 2016-01-06 000002.SZA
    8 159.222107 2016-01-06 000004.SZA
    9 940.775269 2016-01-07 000001.SZA
    10 3113.541504 2016-01-07 000002.SZA
    11 143.291779 2016-01-07 000004.SZA
    12 956.254211 2016-01-08 000001.SZA
    13 3113.541504 2016-01-08 000002.SZA
    14 144.185822 2016-01-08 000004.SZA
    15 925.296326 2016-01-11 000001.SZA
    16 3113.541504 2016-01-11 000002.SZA
    17 129.759109 2016-01-11 000004.SZA
    18 929.596008 2016-01-12 000001.SZA
    19 3113.541504 2016-01-12 000002.SZA
    20 128.255478 2016-01-12 000004.SZA
    21 920.996643 2016-01-13 000001.SZA
    22 3113.541504 2016-01-13 000002.SZA
    23 122.159691 2016-01-13 000004.SZA
    24 926.156250 2016-01-14 000001.SZA
    25 3113.541504 2016-01-14 000002.SZA
    26 127.036324 2016-01-14 000004.SZA
    27 899.498108 2016-01-15 000001.SZA
    28 3113.541504 2016-01-15 000002.SZA
    29 119.680733 2016-01-15 000004.SZA
    ... ... ... ...
    702 963.775940 2016-12-19 000001.SZA
    703 2801.710205 2016-12-19 000002.SZA
    704 178.809921 2016-12-19 000004.SZA
    705 954.347656 2016-12-20 000001.SZA
    706 2699.467529 2016-12-20 000002.SZA
    707 178.403549 2016-12-20 000004.SZA
    708 959.585571 2016-12-21 000001.SZA
    709 2719.385010 2016-12-21 000002.SZA
    710 183.280182 2016-12-21 000004.SZA
    711 957.490417 2016-12-22 000001.SZA
    712 2736.646729 2016-12-22 000002.SZA
    713 181.085693 2016-12-22 000004.SZA
    714 951.204956 2016-12-23 000001.SZA
    715 2695.484131 2016-12-23 000002.SZA
    716 175.396286 2016-12-23 000004.SZA
    717 955.395264 2016-12-26 000001.SZA
    718 2741.958008 2016-12-26 000002.SZA
    719 177.753326 2016-12-26 000004.SZA
    720 951.204956 2016-12-27 000001.SZA
    721 2844.200439 2016-12-27 000002.SZA
    722 181.166962 2016-12-27 000004.SZA
    723 949.109802 2016-12-28 000001.SZA
    724 2814.988281 2016-12-28 000002.SZA
    725 179.541428 2016-12-28 000004.SZA
    726 951.204956 2016-12-29 000001.SZA
    727 2767.186768 2016-12-29 000002.SZA
    728 181.654633 2016-12-29 000004.SZA
    729 953.300110 2016-12-30 000001.SZA
    730 2728.679688 2016-12-30 000002.SZA
    731 182.467407 2016-12-30 000004.SZA

    732 rows × 3 columns


    (chaoskey) #2

    谢谢,定义成局部函数,倒是也可以实现我的功能。


    (iQuant) #3

    不想要局部函数的话,可以在上方增加一个cell,然后定义了直接拿来用就是。


    (chaoskey) #4

    对呀! 我为何没想到呢。 谢谢你的提醒。


    (神龙斗士) #5

    @chaoskey 建议使用局部函数,让各个模块代码和功能尽可能独立,没有外部依赖


    (chaoskey) #6

    谢谢。 接受建议。