请教怎样才能灵活使用data参数?

新手专区
标签: #<Tag:0x00007fb146bd3bd0>

(189) #1

schedule_function这一周期执行调度函数,在符合date_rule, time_rule下周期性执行,需在 initialize 函数中调用。如果initialize中调用了schedule_function函数,那么算法会按照date_rule和time_rule周期性执行func函数,如果没有调用schedule_function函数,则算法会每个单位时间调用handle_data函数。
————————————————————————
那么问题来了:
1、handle_data函数是可以调用data来取得当前市价或收盘价等等字段,schedule_function函数是否也是类似的可以调用data或者说它的形参里面是否包含data?
2、除了回测trade模块之外,其他地方如何调用data中的即时数据,比如说,我用今天的分时或分钟图上的close或者当前价,观察它是否突破了最近日线的10日均线(这个日线的均值数据用的是昨天为首的10天数据,又或者9天数据再加上当前的close);单独用python写我是写的出来,但是做成可视化的话,不知道怎么搞模块???


(iQuant) #2

收到您的提问,已提交至策略工程师,会尽快为您回复。


(达达) #3
  1. 文档有介绍,定时函数的输入参数有data和context

  2. 作为回测引擎的外部函数,可以考虑把您要的技术指标通过表达式引擎实现在外部抽取出来,和预测数据/条件数据通过连接数据模块拼在一起之后,传给回测模块,然后在模块中抽取历史数据


(189) #4

多谢,比如我写了下面一个可视化的简单策略,到了数据连接就出错了,原因很可能是被连接的数据index有问题,但不知怎么解决?

另外还有一个很重要的问题是:如果我用表达式引擎在输入特征列表里表达buy_condition=close>mean(close,12),sell_condition=close>mean(close,12)等等,这个close和mean(close,12)里的close是两码事,一个来自当日分钟或者TICK,另外一个来自日线,如何做出比较?似乎这时没法使用表达式引擎?

克隆策略
In [87]:
m12.data.read_df().tail(30)
Out[87]:
date instrument close_current close mean(close,12)
5 2019-10-15 601318.SHA NaN 216.987823 NaN
6 2019-10-16 601318.SHA NaN 217.901962 NaN
7 2019-10-17 601318.SHA NaN 220.644379 NaN
8 2019-10-18 601318.SHA NaN 215.905289 NaN
9 2019-10-21 601318.SHA NaN 216.675095 NaN
10 2019-10-22 601318.SHA NaN 217.204330 NaN
11 2019-10-23 601318.SHA NaN 215.303879 215.933356
12 2019-10-24 601318.SHA NaN 216.025574 216.294201
13 2019-10-25 601318.SHA NaN 211.695435 216.258116
14 2019-10-28 601318.SHA NaN 213.235046 216.286182
15 2019-10-29 601318.SHA NaN 212.176559 215.945384
16 2019-10-30 601318.SHA NaN 211.575165 215.444211
17 2019-10-31 601318.SHA NaN 211.984116 215.027236
18 2019-11-01 601318.SHA NaN 216.602921 214.918982
19 2019-11-04 601318.SHA NaN 217.204330 214.632312
20 2019-11-05 601318.SHA NaN 216.651031 214.694457
21 2019-11-06 601318.SHA NaN 216.506699 214.680424
22 2019-11-07 601318.SHA NaN 215.953400 214.576180
23 2019-11-08 601318.SHA NaN 214.510025 214.510025
24 2019-11-11 601318.SHA NaN 212.200623 214.191279
25 2019-11-12 601318.SHA NaN 212.224686 214.235383
26 2019-11-13 601318.SHA NaN 211.695435 214.107082
27 2019-11-14 601318.SHA NaN 211.767609 214.073003
28 2019-11-15 601318.SHA NaN 211.454880 214.062979
29 2019-11-18 601318.SHA NaN 213.403442 214.181257
30 2019-11-19 601318.SHA NaN 213.836456 213.950718
31 2019-11-20 601318.SHA NaN 211.815720 213.501667
32 2019-11-21 601318.SHA NaN 207.702087 212.755922
33 2019-11-22 601318.SHA NaN 204.213928 211.731524
34 2019-11-25 601318.SHA NaN 205.681366 210.875521

    {"Description":"实验创建于2019/11/27","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-18:features","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-25:features","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-18:instruments","SourceOutputPortId":"-9:data"},{"DestinationInputPortId":"-25:input_data","SourceOutputPortId":"-18:data"},{"DestinationInputPortId":"-78:data1","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-42:instruments","SourceOutputPortId":"-33:data"},{"DestinationInputPortId":"-58:input_data","SourceOutputPortId":"-42:data"},{"DestinationInputPortId":"-42:features","SourceOutputPortId":"-53:data"},{"DestinationInputPortId":"-58:features","SourceOutputPortId":"-53:data"},{"DestinationInputPortId":"-67:input_data","SourceOutputPortId":"-58:data"},{"DestinationInputPortId":"-107:input_1","SourceOutputPortId":"-67:data"},{"DestinationInputPortId":"-78:data2","SourceOutputPortId":"-107:data_1"}],"ModuleNodes":[{"Id":"-5","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nmean(close,12)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-5"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-9","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-25","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"601318.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-9"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-9","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-18","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":"-18"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-18"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-18","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-25","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":"-25"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-25"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-25","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-33","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-25","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"601318.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-33"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-33","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-42","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1m_601318_SHA","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":"-42"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-42"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-42","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-53","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nclose\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-53"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-53","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"-58","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":"-58"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-58"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-58","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"Comment":"","CommentCollapsed":true},{"Id":"-67","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"date.dt.hour==11 & date.dt.minute==30","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_left_data","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-67"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-67","OutputType":null},{"Name":"left_data","NodeId":"-67","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"Comment":"","CommentCollapsed":true},{"Id":"-78","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"left","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-78"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-78"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-78","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"Comment":"","CommentCollapsed":true},{"Id":"-107","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()\n df['close_current']=df.close\n df['date']=df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))\n df.drop('close',axis=1, inplace=True)\n print(df.tail())\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},{"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":"-107"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-107"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-107"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-107","OutputType":null},{"Name":"data_2","NodeId":"-107","OutputType":null},{"Name":"data_3","NodeId":"-107","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"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='-5' Position='419,65,200,200'/><NodePosition Node='-9' Position='99,69,200,200'/><NodePosition Node='-18' Position='143,221,200,200'/><NodePosition Node='-25' Position='199,364,200,200'/><NodePosition Node='-33' Position='718.1641235351562,108.16413879394531,200,200'/><NodePosition Node='-42' Position='726,229,200,200'/><NodePosition Node='-53' Position='778,9,200,200'/><NodePosition Node='-58' Position='676,312,200,200'/><NodePosition Node='-67' Position='644,400,200,200'/><NodePosition Node='-78' Position='219.07574462890625,523.6262817382812,200,200'/><NodePosition Node='-107' Position='577.80810546875,479.6565856933594,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 [89]:
    # 本代码由可视化策略环境自动生成 2019年11月28日 00:50
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m13_run_bigquant_run(input_1, input_2, input_3):
        # 示例代码如下。在这里编写您的代码
        df = input_1.read_df()
        df['close_current']=df.close
        df['date']=df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))
        df.drop('close',axis=1, inplace=True)
        print(df.tail())
        data_1 = DataSource.write_df(df)
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m13_post_run_bigquant_run(outputs):
        return outputs
    
    
    m1 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    mean(close,12)"""
    )
    
    m2 = M.instruments.v2(
        start_date='2019-10-01',
        end_date='2019-11-25',
        market='CN_STOCK_A',
        instrument_list='601318.SHA',
        max_count=0
    )
    
    m3 = M.use_datasource.v1(
        instruments=m2.data,
        features=m1.data,
        datasource_id='bar1d_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m4 = M.derived_feature_extractor.v3(
        input_data=m3.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m5 = M.instruments.v2(
        start_date='2019-10-01',
        end_date='2019-11-25',
        market='CN_STOCK_A',
        instrument_list='601318.SHA',
        max_count=0
    )
    
    m8 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    close
    """
    )
    
    m6 = M.use_datasource.v1(
        instruments=m5.data,
        features=m8.data,
        datasource_id='bar1m_601318_SHA',
        start_date='',
        end_date=''
    )
    
    m9 = M.derived_feature_extractor.v3(
        input_data=m6.data,
        features=m8.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m10 = M.filter.v3(
        input_data=m9.data,
        expr='date.dt.hour==11 & date.dt.minute==30',
        output_left_data=False
    )
    
    m13 = M.cached.v3(
        input_1=m10.data,
        run=m13_run_bigquant_run,
        post_run=m13_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m12 = M.join.v3(
        data1=m4.data,
        data2=m13.data_1,
        on='date,instrument',
        how='left',
        sort=False
    )
    
                date  instrument  close_current
    6599  2019-11-15  601318.SHA      88.040001
    6839  2019-11-18  601318.SHA      88.410004
    7079  2019-11-19  601318.SHA      88.730003
    7319  2019-11-21  601318.SHA      86.680000
    7559  2019-11-22  601318.SHA      85.389999
    

    (达达) #5

    用别名处理重名,日期修改为Timestamp格式可以连接

    克隆策略

      {"Description":"实验创建于2019/11/27","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-18:features","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-25:features","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-18:instruments","SourceOutputPortId":"-9:data"},{"DestinationInputPortId":"-25:input_data","SourceOutputPortId":"-18:data"},{"DestinationInputPortId":"-78:data1","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-42:instruments","SourceOutputPortId":"-33:data"},{"DestinationInputPortId":"-58:input_data","SourceOutputPortId":"-42:data"},{"DestinationInputPortId":"-42:features","SourceOutputPortId":"-53:data"},{"DestinationInputPortId":"-58:features","SourceOutputPortId":"-53:data"},{"DestinationInputPortId":"-67:input_data","SourceOutputPortId":"-58:data"},{"DestinationInputPortId":"-107:input_1","SourceOutputPortId":"-67:data"},{"DestinationInputPortId":"-78:data2","SourceOutputPortId":"-107:data_1"}],"ModuleNodes":[{"Id":"-5","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nmean(close,12)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-5"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-9","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-25","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"601318.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-9"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-9","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-18","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":"-18"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-18"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-18","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-25","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":"-25"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-25"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-25","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-33","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-25","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"601318.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-33"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-33","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-42","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1m_601318_SHA","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":"-42"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-42"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-42","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-53","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nmin_close=close\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-53"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-53","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"-58","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":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-58"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-58"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-58","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"Comment":"","CommentCollapsed":true},{"Id":"-67","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"date.dt.hour==11 & date.dt.minute==30","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_left_data","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-67"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-67","OutputType":null},{"Name":"left_data","NodeId":"-67","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"Comment":"","CommentCollapsed":true},{"Id":"-78","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"how","Value":"left","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"sort","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data1","NodeId":"-78"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-78"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-78","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"Comment":"","CommentCollapsed":true},{"Id":"-107","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()\n df['date']=df['date'].apply(lambda x:pd.Timestamp(x.strftime('%Y-%m-%d')))\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},{"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":"-107"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-107"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-107"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-107","OutputType":null},{"Name":"data_2","NodeId":"-107","OutputType":null},{"Name":"data_3","NodeId":"-107","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":13,"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='-5' Position='419,65,200,200'/><NodePosition Node='-9' Position='99,69,200,200'/><NodePosition Node='-18' Position='143,221,200,200'/><NodePosition Node='-25' Position='199,364,200,200'/><NodePosition Node='-33' Position='718,108,200,200'/><NodePosition Node='-42' Position='726,229,200,200'/><NodePosition Node='-53' Position='1014.5372924804688,110.12823486328125,200,200'/><NodePosition Node='-58' Position='676,313.7140197753906,200,200'/><NodePosition Node='-67' Position='645.7139892578125,400,200,200'/><NodePosition Node='-78' Position='445.2530212402344,617.2720947265625,200,200'/><NodePosition Node='-107' Position='577,479,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 [7]:
      # 本代码由可视化策略环境自动生成 2019年11月28日 09:29
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
      def m13_run_bigquant_run(input_1, input_2, input_3):
          # 示例代码如下。在这里编写您的代码
          df = input_1.read_df()
          df['date']=df['date'].apply(lambda x:pd.Timestamp(x.strftime('%Y-%m-%d')))
          data_1 = DataSource.write_df(df)
          return Outputs(data_1=data_1, data_2=None, data_3=None)
      
      # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
      def m13_post_run_bigquant_run(outputs):
          return outputs
      
      
      m1 = M.input_features.v1(
          features="""
      # #号开始的表示注释,注释需单独一行
      # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
      mean(close,12)"""
      )
      
      m2 = M.instruments.v2(
          start_date='2019-10-01',
          end_date='2019-11-25',
          market='CN_STOCK_A',
          instrument_list='601318.SHA',
          max_count=0
      )
      
      m3 = M.use_datasource.v1(
          instruments=m2.data,
          features=m1.data,
          datasource_id='bar1d_CN_STOCK_A',
          start_date='',
          end_date=''
      )
      
      m4 = M.derived_feature_extractor.v3(
          input_data=m3.data,
          features=m1.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=False,
          remove_extra_columns=False,
          user_functions={}
      )
      
      m5 = M.instruments.v2(
          start_date='2019-10-01',
          end_date='2019-11-25',
          market='CN_STOCK_A',
          instrument_list='601318.SHA',
          max_count=0
      )
      
      m8 = M.input_features.v1(
          features="""
      # #号开始的表示注释,注释需单独一行
      # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
      min_close=close
      """
      )
      
      m6 = M.use_datasource.v1(
          instruments=m5.data,
          features=m8.data,
          datasource_id='bar1m_601318_SHA',
          start_date='',
          end_date=''
      )
      
      m9 = M.derived_feature_extractor.v3(
          input_data=m6.data,
          features=m8.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=True,
          remove_extra_columns=True,
          user_functions={}
      )
      
      m10 = M.filter.v3(
          input_data=m9.data,
          expr='date.dt.hour==11 & date.dt.minute==30',
          output_left_data=False
      )
      
      m13 = M.cached.v3(
          input_1=m10.data,
          run=m13_run_bigquant_run,
          post_run=m13_post_run_bigquant_run,
          input_ports='',
          params='{}',
          output_ports='',
          m_cached=False
      )
      
      m12 = M.join.v3(
          data1=m4.data,
          data2=m13.data_1,
          on='date,instrument',
          how='left',
          sort=False
      )
      
      In [8]:
      m12.data.read_df()
      
      Out[8]:
      date instrument min_close close mean(close,12)
      0 2019-10-08 601318.SHA 88.519997 211.695435 NaN
      1 2019-10-09 601318.SHA 87.879997 212.128448 NaN
      2 2019-10-10 601318.SHA 87.980003 212.898254 NaN
      3 2019-10-11 601318.SHA 89.300003 216.266144 NaN
      4 2019-10-14 601318.SHA 90.750000 217.589233 NaN
      5 2019-10-15 601318.SHA 90.169998 216.987823 NaN
      6 2019-10-17 601318.SHA 92.050003 220.644379 NaN
      7 2019-10-18 601318.SHA 90.680000 215.905289 NaN
      8 2019-10-21 601318.SHA 90.019997 216.675095 NaN
      9 2019-10-22 601318.SHA 89.760002 217.204330 NaN
      10 2019-10-23 601318.SHA 89.860001 215.303879 215.933356
      11 2019-10-24 601318.SHA 89.300003 216.025574 216.294201
      12 2019-10-25 601318.SHA 88.120003 211.695435 216.258116
      13 2019-10-28 601318.SHA 88.599998 213.235046 216.286182
      14 2019-10-29 601318.SHA 88.699997 212.176559 215.945384
      15 2019-10-30 601318.SHA 88.080002 211.575165 215.444211
      16 2019-10-31 601318.SHA 88.599998 211.984116 215.027236
      17 2019-11-01 601318.SHA 89.629997 216.602921 214.918982
      18 2019-11-04 601318.SHA 90.430000 217.204330 214.632312
      19 2019-11-05 601318.SHA 90.620003 216.651031 214.694457
      20 2019-11-06 601318.SHA 89.910004 216.506699 214.680424
      21 2019-11-07 601318.SHA 89.389999 215.953400 214.576180
      22 2019-11-08 601318.SHA 89.949997 214.510025 214.510025
      23 2019-11-11 601318.SHA 88.400002 212.200623 214.191279
      24 2019-11-12 601318.SHA 87.919998 212.224686 214.235383
      25 2019-11-13 601318.SHA 88.099998 211.695435 214.107082
      26 2019-11-14 601318.SHA 87.970001 211.767609 214.073003
      27 2019-11-15 601318.SHA 88.040001 211.454880 214.062979
      28 2019-11-18 601318.SHA 88.410004 213.403442 214.181257
      29 2019-11-19 601318.SHA 88.730003 213.836456 213.950718
      30 2019-11-21 601318.SHA 86.680000 207.702087 212.755922
      31 2019-11-22 601318.SHA 85.389999 204.213928 211.731524
      32 2019-10-16 601318.SHA NaN 217.901962 NaN
      33 2019-11-20 601318.SHA NaN 211.815720 213.501667
      34 2019-11-25 601318.SHA NaN 205.681366 210.875521

      (189) #6

      多谢,我不知道你有没留意到最后3行的NAN值,以及mean(close,12)倒数第三行也有一个NaN值??同时一个是真实价格,一个是后复权价格,这个分钟图上面或者TICK数据上面,是否都是统一的以真实价格显示?


      (189) #7

      这10月16日这行也是很奇怪的,很突兀的出现在上下日期完全不相关的地方


      (达达) #8

      价格你可以用close_real=close/adjust_factor转化日线数据为真实价格,日期错位可能是分钟因子没数据,你用inner试试,然后自己用排序模块排序