TALIB库定义技术指标_自适应均线


(iQuant) #1
克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-580:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-587:input_data","SourceOutputPortId":"-580:data"},{"DestinationInputPortId":"-643:input_1","SourceOutputPortId":"-580:data"},{"DestinationInputPortId":"-659:data2","SourceOutputPortId":"-587:data"},{"DestinationInputPortId":"-580:features","SourceOutputPortId":"-635:data"},{"DestinationInputPortId":"-587:features","SourceOutputPortId":"-635:data"},{"DestinationInputPortId":"-659:data1","SourceOutputPortId":"-643:data_1"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"-659:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2010-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-01-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":"287d2cb0-f53c-4101-bdf8-104b137c8601-8"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-580","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":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-580"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-580"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-580","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":15,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-587","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":"-587"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-587"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-587","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":16,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-635","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nclose_0\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-635"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-635","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":20,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-643","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 input_df = input_1.read_df().reset_index(drop='True')\n import talib\n\n def cal_kama(df):\n # 取出close_0列的数据转化为float\n close = [float(x) for x in df['close_0']]\n # 调用talib计算MACD指标\n df['KAMA'] = talib.KAMA(np.array(close), timeperiod=5)\n return df[['date','instrument','KAMA']] \n \n result = input_df.groupby('instrument').apply(cal_kama)\n \n data_1 = DataSource.write_df(result)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","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":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-643"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-643"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-643"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-643","OutputType":null},{"Name":"data_2","NodeId":"-643","OutputType":null},{"Name":"data_3","NodeId":"-643","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":21,"IsPartOfPartialRun":null,"Comment":"使用TALIB计算自适应均线KAMA","CommentCollapsed":false},{"Id":"-659","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"inner","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-659"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-659"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-659","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":22,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-84","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"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='287d2cb0-f53c-4101-bdf8-104b137c8601-8' Position='226,52,200,200'/><NodePosition Node='-580' Position='377,151,200,200'/><NodePosition Node='-587' Position='540,270,200,200'/><NodePosition Node='-635' Position='571,55,200,200'/><NodePosition Node='-643' Position='218,245.89407348632812,200,200'/><NodePosition Node='-659' Position='386,368,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='399,447,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 [2]:
    # 本代码由可视化策略环境自动生成 2019年1月31日 17:56
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m21_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        input_df = input_1.read_df().reset_index(drop='True')
        import talib
    
        def cal_kama(df):
            # 取出close_0列的数据转化为float
            close = [float(x) for x in df['close_0']]
             # 调用talib计算MACD指标
            df['KAMA'] = talib.KAMA(np.array(close), timeperiod=5)
            return df[['date','instrument','KAMA']]    
        
        result = input_df.groupby('instrument').apply(cal_kama)
        
        data_1 = DataSource.write_df(result)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m21_post_run_bigquant_run(outputs):
        return outputs
    
    
    m1 = M.instruments.v2(
        start_date='2010-01-01',
        end_date='2015-01-01',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m20 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    close_0
    """
    )
    
    m15 = M.general_feature_extractor.v7(
        instruments=m1.data,
        features=m20.data,
        start_date='',
        end_date='',
        before_start_days=0
    )
    
    m21 = M.cached.v3(
        input_1=m15.data,
        run=m21_run_bigquant_run,
        post_run=m21_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m16 = M.derived_feature_extractor.v3(
        input_data=m15.data,
        features=m20.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False
    )
    
    m22 = M.join.v3(
        data1=m21.data_1,
        data2=m16.data,
        on='date,instrument',
        how='inner',
        sort=False
    )
    
    m13 = M.dropnan.v1(
        input_data=m22.data
    )
    
    [2019-01-31 17:56:51.193812] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-31 17:56:51.201006] INFO: bigquant: 命中缓存
    [2019-01-31 17:56:51.202113] INFO: bigquant: instruments.v2 运行完成[0.008329s].
    [2019-01-31 17:56:51.204912] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-31 17:56:51.209483] INFO: bigquant: 命中缓存
    [2019-01-31 17:56:51.210381] INFO: bigquant: input_features.v1 运行完成[0.005477s].
    [2019-01-31 17:56:51.215595] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-31 17:56:51.219919] INFO: bigquant: 命中缓存
    [2019-01-31 17:56:51.220930] INFO: bigquant: general_feature_extractor.v7 运行完成[0.005337s].
    [2019-01-31 17:56:51.224645] INFO: bigquant: cached.v3 开始运行..
    [2019-01-31 17:56:59.480688] INFO: bigquant: cached.v3 运行完成[8.255999s].
    [2019-01-31 17:56:59.488493] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-31 17:56:59.494433] INFO: bigquant: 命中缓存
    [2019-01-31 17:56:59.495480] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.006994s].
    [2019-01-31 17:56:59.500676] INFO: bigquant: join.v3 开始运行..
    [2019-01-31 17:57:02.010445] INFO: join: /y_2010, 行数=431567/431567, 耗时=1.276989s
    [2019-01-31 17:57:03.146173] INFO: join: /y_2011, 行数=511455/511455, 耗时=1.120019s
    [2019-01-31 17:57:04.439341] INFO: join: /y_2012, 行数=565675/565675, 耗时=1.276905s
    [2019-01-31 17:57:06.005996] INFO: join: /y_2013, 行数=564168/564168, 耗时=1.532349s
    [2019-01-31 17:57:07.347811] INFO: join: /y_2014, 行数=569948/569948, 耗时=1.320967s
    [2019-01-31 17:57:07.466793] INFO: join: 最终行数: 2642813
    [2019-01-31 17:57:07.470146] INFO: bigquant: join.v3 运行完成[7.969442s].
    [2019-01-31 17:57:07.476212] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-31 17:57:08.091627] INFO: dropnan: /y_2010, 421499/431567
    [2019-01-31 17:57:08.589460] INFO: dropnan: /y_2011, 509994/511455
    [2019-01-31 17:57:09.075821] INFO: dropnan: /y_2012, 564864/565675
    [2019-01-31 17:57:09.550413] INFO: dropnan: /y_2013, 564108/564168
    [2019-01-31 17:57:10.068404] INFO: dropnan: /y_2014, 569351/569948
    [2019-01-31 17:57:10.097166] INFO: dropnan: 行数: 2629816/2642813
    [2019-01-31 17:57:10.119084] INFO: bigquant: dropnan.v1 运行完成[2.642811s].
    
    In [3]:
    m13.data.read_df().tail()
    
    Out[3]:
    close_0 date instrument KAMA
    569943 34.900002 2014-12-25 603998.SHA 35.162839
    569944 34.560001 2014-12-26 603998.SHA 35.147484
    569945 35.189999 2014-12-29 603998.SHA 35.156138
    569946 37.459999 2014-12-30 603998.SHA 35.576622
    569947 35.259998 2014-12-31 603998.SHA 35.571593

    【宽客学院】借助Talib库构建技术分析指标因子