【模板案例】周线计算指标

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

(iQuant) #1

7月30日Meetup 策略模板:

周线计算指标

克隆策略

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-51:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-35:input_1","SourceOutputPortId":"-51:data"},{"DestinationInputPortId":"-85:input_2","SourceOutputPortId":"-51:data"},{"DestinationInputPortId":"-85:input_1","SourceOutputPortId":"-35:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2015-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"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-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":true},{"Id":"-51","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_STOCK_A","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":"-51"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-51"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-51","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-35","ModuleId":"BigQuantSpace.resample_df.resample_df-v15","ModuleParameters":[{"Name":"columns","Value":"[\"open\", \"high\", \"low\", \"close\",\"amount\",\"volume\"]","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"resample_period","Value":"W-FRI","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how_key","Value":"{'date': 'last', \n'volume': 'sum',\n 'amount': 'sum',\n 'close': 'last',\n'high': 'max',\n'low': 'min',\n'open': 'first'}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-35"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-35","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-85","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, col_name):\n # 示例代码如下。在这里编写您的代码\n df_resample = input_1.read()\n df_origin = input_2.read()\n \n new_column = 'rolling_'+col_name\n def cal(x):\n return pd.Series([[x.values[j-i-1] for i in range(5) if j-i >0 ] for j in range(len(x))],index=x.index)\n df_resample[new_column] = df_resample.groupby('instrument', group_keys=False)[col_name].apply(lambda x: cal(x))\n \n start_ = df_resample.date.min()\n \n df_merge = df_origin[df_origin.date>=start_].merge(df_resample[['date','instrument',new_column]], \n on=['date','instrument'], how='left')\n \n df_merge[new_column] = df_merge.groupby('instrument')[new_column].ffill()\n \n df_merge[new_column] = df_merge[col_name].apply(lambda x:[x]) + df_merge[new_column]\n df_merge['bolling_up'] = df_merge[new_column].apply(lambda x:np.mean(x)+np.std(x))\n data_1 = DataSource.write_df(df_merge)\n return Outputs(data_1=data_1)\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":"input_1,input_2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{'col_name':'close'}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"data_1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-85"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-85"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-85"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-85","OutputType":null},{"Name":"data_2","NodeId":"-85","OutputType":null},{"Name":"data_3","NodeId":"-85","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"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-62' Position='608,49,200,200'/><NodePosition Node='-51' Position='758,149,200,200'/><NodePosition Node='-35' Position='546.1370849609375,239.86294555664062,200,200'/><NodePosition Node='-85' Position='737,327,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 [55]:
    # 本代码由可视化策略环境自动生成 2020年7月30日 17:44
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m2_run_bigquant_run(input_1, input_2, col_name):
        # 示例代码如下。在这里编写您的代码
        df_resample = input_1.read()
        df_origin = input_2.read()
        
        new_column = 'rolling_'+col_name
        def cal(x):
            return pd.Series([[x.values[j-i-1] for i in range(5) if j-i >0 ] for j in range(len(x))],index=x.index)
        df_resample[new_column] = df_resample.groupby('instrument', group_keys=False)[col_name].apply(lambda x: cal(x))
        
        start_ = df_resample.date.min()
        
        df_merge = df_origin[df_origin.date>=start_].merge(df_resample[['date','instrument',new_column]], 
                          on=['date','instrument'], how='left')
        
        df_merge[new_column] = df_merge.groupby('instrument')[new_column].ffill()
        
        df_merge[new_column] = df_merge[col_name].apply(lambda x:[x]) + df_merge[new_column]
        df_merge['bolling_up'] = df_merge[new_column].apply(lambda x:np.mean(x)+np.std(x))
        data_1 = DataSource.write_df(df_merge)
        return Outputs(data_1=data_1)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m2_post_run_bigquant_run(outputs):
        return outputs
    
    
    m9 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2015-01-01'),
        end_date=T.live_run_param('trading_date', '2017-01-01'),
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m3 = M.use_datasource.v1(
        instruments=m9.data,
        datasource_id='bar1d_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m1 = M.resample_df.v15(
        input_1=m3.data,
        columns=["open", "high", "low", "close","amount","volume"],
        resample_period='W-FRI',
        how_key={'date': 'last', 
    'volume': 'sum',
     'amount': 'sum',
     'close': 'last',
    'high': 'max',
    'low': 'min',
    'open': 'first'}
    )
    
    m2 = M.cached.v3(
        input_1=m1.data,
        input_2=m3.data,
        run=m2_run_bigquant_run,
        post_run=m2_post_run_bigquant_run,
        input_ports='input_1,input_2',
        params='{\'col_name\':\'close\'}',
        output_ports='data_1'
    )
    

    BigQuant AI量化专家Meetup(7月30日场回放及案例模板)