克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-758:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-8:data"},{"DestinationInputPortId":"-758:features","SourceOutputPortId":"-635:data"},{"DestinationInputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-84:input_data","SourceOutputPortId":"-643:data_1"},{"DestinationInputPortId":"-786:input_1","SourceOutputPortId":"-758:data"},{"DestinationInputPortId":"-643:input_1","SourceOutputPortId":"-786:data_1"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-8","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01 09:30:00","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2015-03-01 15:00:00","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,"Comment":"","CommentCollapsed":true},{"Id":"-635","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nclose\nopen\nhigh\nlow\nvolume\namount","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,"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()\n import talib\n\n def cal_kama(df):\n # 取出close_0列的数据转化为float\n high = np.array([float(x) for x in df['high']])\n low = np.array([float(x) for x in df['low']])\n close = np.array([float(x) for x in df['close']])\n # 调用talib计算MACD指标\n df['ATR'] = talib.ATR(high, low, close, timeperiod=14)\n return df[['date','instrument','ATR']]\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,"Comment":"使用TALIB计算ATR","CommentCollapsed":false},{"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,"Comment":"","CommentCollapsed":true},{"Id":"-758","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1m_000001.SZA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-758"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-758"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-758","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-786","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 = input_1.read_df().set_index('date')\n \n def resample(df,period):\n # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases\n Xmin_df=pd.DataFrame()\n Xmin_df['open'] = df['open'].resample(period, how='first')\n Xmin_df['high'] = df['high'].resample(period, how='max')\n Xmin_df['low'] = df['low'].resample(period, how='min')\n Xmin_df['close'] = df['close'].resample(period, how='last')\n Xmin_df['volume'] = df['volume'].resample(period, how='sum')\n Xmin_df['amount'] = df['amount'].resample(period, how='sum')\n Xmin_df.dropna(inplace=True)\n return Xmin_df\n \n resample_df = df.groupby('instrument').apply(lambda x:resample(x, '5min')).reset_index()\n \n data_1 = DataSource.write_df(resample_df)\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":"-786"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-786"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-786"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-786","OutputType":null},{"Name":"data_2","NodeId":"-786","OutputType":null},{"Name":"data_3","NodeId":"-786","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"5min周期转换","CommentCollapsed":false}],"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='116.51336669921875,15.50445556640625,200,200'/><NodePosition Node='-635' Position='467.04296875,12.974822998046875,200,200'/><NodePosition Node='-643' Position='307.580078125,361.1222381591797,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-84' Position='295.0429992675781,479.0718688964844,200,200'/><NodePosition Node='-758' Position='287.9931640625,131.9949493408203,200,200'/><NodePosition Node='-786' Position='287.9932556152344,238.16380310058594,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]:
    # 本代码由可视化策略环境自动生成 2019年1月31日 18:28
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m3_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df = input_1.read_df().set_index('date')
        
        def resample(df,period):
            # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
            Xmin_df=pd.DataFrame()
            Xmin_df['open'] = df['open'].resample(period, how='first')
            Xmin_df['high'] = df['high'].resample(period, how='max')
            Xmin_df['low'] = df['low'].resample(period, how='min')
            Xmin_df['close'] = df['close'].resample(period, how='last')
            Xmin_df['volume'] = df['volume'].resample(period, how='sum')
            Xmin_df['amount'] = df['amount'].resample(period, how='sum')
            Xmin_df.dropna(inplace=True)
            return Xmin_df
        
        resample_df = df.groupby('instrument').apply(lambda x:resample(x, '5min')).reset_index()
        
        data_1 = DataSource.write_df(resample_df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m3_post_run_bigquant_run(outputs):
        return outputs
    
    # 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()
        import talib
    
        def cal_kama(df):
            # 取出close_0列的数据转化为float
            high = np.array([float(x) for x in df['high']])
            low = np.array([float(x) for x in df['low']])
            close = np.array([float(x) for x in df['close']])
             # 调用talib计算MACD指标
            df['ATR'] = talib.ATR(high, low, close, timeperiod=14)
            return df[['date','instrument','ATR']]
        
        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='2015-01-01 09:30:00',
        end_date='2015-03-01 15:00:00',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m20 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    close
    open
    high
    low
    volume
    amount"""
    )
    
    m2 = M.use_datasource.v1(
        instruments=m1.data,
        features=m20.data,
        datasource_id='bar1m_000001.SZA',
        start_date='',
        end_date=''
    )
    
    m3 = M.cached.v3(
        input_1=m2.data,
        run=m3_run_bigquant_run,
        post_run=m3_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m21 = M.cached.v3(
        input_1=m3.data_1,
        run=m21_run_bigquant_run,
        post_run=m21_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m13 = M.dropnan.v1(
        input_data=m21.data_1
    )
    
    [2019-01-31 18:28:55.284505] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-31 18:28:55.300762] INFO: bigquant: 命中缓存
    [2019-01-31 18:28:55.302390] INFO: bigquant: instruments.v2 运行完成[0.017945s].
    [2019-01-31 18:28:55.307044] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-31 18:28:55.314426] INFO: bigquant: 命中缓存
    [2019-01-31 18:28:55.315434] INFO: bigquant: input_features.v1 运行完成[0.00841s].
    [2019-01-31 18:28:55.407648] INFO: bigquant: use_datasource.v1 开始运行..
    [2019-01-31 18:28:55.412887] INFO: bigquant: 命中缓存
    [2019-01-31 18:28:55.413917] INFO: bigquant: use_datasource.v1 运行完成[0.006298s].
    [2019-01-31 18:28:55.420665] INFO: bigquant: cached.v3 开始运行..
    [2019-01-31 18:28:55.425964] INFO: bigquant: 命中缓存
    [2019-01-31 18:28:55.426938] INFO: bigquant: cached.v3 运行完成[0.006268s].
    [2019-01-31 18:28:55.432310] INFO: bigquant: cached.v3 开始运行..
    [2019-01-31 18:28:55.437675] INFO: bigquant: 命中缓存
    [2019-01-31 18:28:55.438594] INFO: bigquant: cached.v3 运行完成[0.006292s].
    [2019-01-31 18:28:55.444776] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-31 18:28:55.449540] INFO: bigquant: 命中缓存
    [2019-01-31 18:28:55.450570] INFO: bigquant: dropnan.v1 运行完成[0.005807s].
    
    In [2]:
    m13.data.read_df().tail()
    
    Out[2]:
    date instrument ATR
    1745 2015-02-27 14:40:00 000001.SZA 0.030817
    1746 2015-02-27 14:45:00 000001.SZA 0.030759
    1747 2015-02-27 14:50:00 000001.SZA 0.030705
    1748 2015-02-27 14:55:00 000001.SZA 0.029940
    1749 2015-02-27 15:00:00 000001.SZA 0.027802