为何无法自定义模块生成字典格式取代证券代码列表模块???

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

(189) #1

证券代码模块我看是字典格式,start_date,end_date和instruments,因此一直筛选过滤到生成一个自定义模块的字典,然后连入数据源,却出错,不知原因??

克隆策略
In [7]:
m5.data.read_df().tail()
Out[7]:
date instrument
620 2019-11-11 510300.HOF
623 2019-11-12 510050.HOF
626 2019-11-13 510050.HOF
629 2019-11-14 510050.HOF
632 2019-11-15 510050.HOF

    {"Description":"实验创建于2019/11/19","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-25:input_data","SourceOutputPortId":"-14:data"},{"DestinationInputPortId":"-14:features","SourceOutputPortId":"-20:data"},{"DestinationInputPortId":"-25:features","SourceOutputPortId":"-20:data"},{"DestinationInputPortId":"-34:input_ds","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-307:input_data","SourceOutputPortId":"-34:sorted_data"},{"DestinationInputPortId":"-101:data1","SourceOutputPortId":"-34:sorted_data"},{"DestinationInputPortId":"-1176:input_1","SourceOutputPortId":"-102:data"},{"DestinationInputPortId":"-102:input_ds","SourceOutputPortId":"-307:data"},{"DestinationInputPortId":"-14:instruments","SourceOutputPortId":"-5:data"},{"DestinationInputPortId":"-124:instruments","SourceOutputPortId":"-1176:data_1"},{"DestinationInputPortId":"-115:features","SourceOutputPortId":"-95:data"},{"DestinationInputPortId":"-124:features","SourceOutputPortId":"-95:data"},{"DestinationInputPortId":"-108:input_ds","SourceOutputPortId":"-115:data"},{"DestinationInputPortId":"-115:input_data","SourceOutputPortId":"-124:data"},{"DestinationInputPortId":"-101:data2","SourceOutputPortId":"-108:data"}],"ModuleNodes":[{"Id":"-14","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_CN_FUND","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":"-14"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-14"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-14","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-20","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\nratio_increase=delta(close, 12)/shift(close, 12)*100\nrank_=rank(ratio_increase) \nratio_ma=(close/mean(close, 13)-1)*100\n\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-20"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-20","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"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":3,"Comment":"","CommentCollapsed":true},{"Id":"-34","ModuleId":"BigQuantSpace.sort.sort-v4","ModuleParameters":[{"Name":"sort_by","Value":"ratio_increase","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"group_by","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"keep_columns","Value":"--","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"ascending","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-34"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"sort_by_ds","NodeId":"-34"}],"OutputPortsInternal":[{"Name":"sorted_data","NodeId":"-34","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-102","ModuleId":"BigQuantSpace.select_columns.select_columns-v3","ModuleParameters":[{"Name":"columns","Value":"date,instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"reverse_select","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"columns_ds","NodeId":"-102"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-102","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-307","ModuleId":"BigQuantSpace.filter.filter-v3","ModuleParameters":[{"Name":"expr","Value":"rank_==1","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":"-307"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-307","OutputType":null},{"Name":"left_data","NodeId":"-307","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-5","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-15","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_FUND","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"510300.HOF\n510050.HOF\n510500.HOF\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-5"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-5","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"Comment":"","CommentCollapsed":true},{"Id":"-1176","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 import time, datetime\n # 示例代码如下。在这里编写您的代码\n df1=input_1.read_df()\n start_date=df1.date.iloc[0].strftime('%Y-%m-%d')\n end_date=df1.date.iloc[-1].strftime('%Y-%m-%d')\n instruments=str(df1.instrument.iloc[-1])\n data={'end_date':end_date,\n 'instruments': instruments,\n 'start_date': start_date}\n \n print(data)\n data_1 = DataSource.write_pickle(data)\n \n \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":"-1176"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-1176"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-1176"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-1176","OutputType":null},{"Name":"data_2","NodeId":"-1176","OutputType":null},{"Name":"data_3","NodeId":"-1176","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"Comment":"","CommentCollapsed":true},{"Id":"-95","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释,注释需单独一行\n# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征\navg_amount=mean(amount, 10)\n\nrisk=where(amount<avg_amount, 1, 0)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-95"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-95","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":9,"Comment":"","CommentCollapsed":true},{"Id":"-1919","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-11-15","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000300.HIX","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-1919"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1919","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":10,"Comment":"","CommentCollapsed":true},{"Id":"-115","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":"-115"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-115"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-115","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":11,"Comment":"","CommentCollapsed":true},{"Id":"-124","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar1d_index_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":"-124"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-124"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-124","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":12,"Comment":"","CommentCollapsed":true},{"Id":"-101","ModuleId":"BigQuantSpace.join.join-v3","ModuleParameters":[{"Name":"on","Value":"date","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":"-101"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"data2","NodeId":"-101"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-101","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":13,"Comment":"","CommentCollapsed":true},{"Id":"-108","ModuleId":"BigQuantSpace.select_columns.select_columns-v3","ModuleParameters":[{"Name":"columns","Value":"date,risk","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"reverse_select","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_ds","NodeId":"-108"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"columns_ds","NodeId":"-108"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-108","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":14,"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='-14' Position='-121,171,200,200'/><NodePosition Node='-20' Position='236,27,200,200'/><NodePosition Node='-25' Position='-119,264,200,200'/><NodePosition Node='-34' Position='-95,357,200,200'/><NodePosition Node='-102' Position='340,344,200,200'/><NodePosition Node='-307' Position='335,235,200,200'/><NodePosition Node='-5' Position='-125,11,200,200'/><NodePosition Node='-1176' Position='413.7646484375,430.661865234375,200,200'/><NodePosition Node='-95' Position='970.6024169921875,37,200,200'/><NodePosition Node='-1919' Position='603,-47,200,200'/><NodePosition Node='-115' Position='754.7646484375,315.014404296875,200,200'/><NodePosition Node='-124' Position='751.91162109375,199.13165283203125,200,200'/><NodePosition Node='-101' Position='401,608,200,200'/><NodePosition Node='-108' Position='738.573486328125,417.2939758300781,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 [12]:
    # 本代码由可视化策略环境自动生成 2019年11月19日 00:44
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m8_run_bigquant_run(input_1, input_2, input_3):
        import time, datetime
        # 示例代码如下。在这里编写您的代码
        df1=input_1.read_df()
        start_date=df1.date.iloc[0].strftime('%Y-%m-%d')
        end_date=df1.date.iloc[-1].strftime('%Y-%m-%d')
        instruments=str(df1.instrument.iloc[-1])
        data={'end_date':end_date,
     'instruments': instruments,
     'start_date': start_date}
      
        print(data)
        data_1 = DataSource.write_pickle(data)
        
        
        return Outputs(data_1=data_1, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m8_post_run_bigquant_run(outputs):
        return outputs
    
    
    m2 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    ratio_increase=delta(close, 12)/shift(close, 12)*100
    rank_=rank(ratio_increase) 
    ratio_ma=(close/mean(close, 13)-1)*100
    
    """
    )
    
    m7 = M.instruments.v2(
        start_date='2019-01-01',
        end_date='2019-11-15',
        market='CN_FUND',
        instrument_list="""510300.HOF
    510050.HOF
    510500.HOF
    """,
        max_count=0
    )
    
    m1 = M.use_datasource.v1(
        instruments=m7.data,
        features=m2.data,
        datasource_id='bar1d_CN_FUND',
        start_date='',
        end_date=''
    )
    
    m3 = M.derived_feature_extractor.v3(
        input_data=m1.data,
        features=m2.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m4 = M.sort.v4(
        input_ds=m3.data,
        sort_by='ratio_increase',
        group_by='date',
        keep_columns='--',
        ascending=False
    )
    
    m6 = M.filter.v3(
        input_data=m4.sorted_data,
        expr='rank_==1',
        output_left_data=False
    )
    
    m5 = M.select_columns.v3(
        input_ds=m6.data,
        columns='date,instrument',
        reverse_select=False,
        m_cached=False
    )
    
    m8 = M.cached.v3(
        input_1=m5.data,
        run=m8_run_bigquant_run,
        post_run=m8_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m9 = M.input_features.v1(
        features="""
    # #号开始的表示注释,注释需单独一行
    # 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
    avg_amount=mean(amount, 10)
    
    risk=where(amount<avg_amount, 1, 0)"""
    )
    
    m12 = M.use_datasource.v1(
        instruments=m8.data_1,
        features=m9.data,
        datasource_id='bar1d_index_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m11 = M.derived_feature_extractor.v3(
        input_data=m12.data,
        features=m9.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=True,
        remove_extra_columns=True,
        user_functions={}
    )
    
    m14 = M.select_columns.v3(
        input_ds=m11.data,
        columns='date,risk',
        reverse_select=False,
        m_cached=False
    )
    
    m13 = M.join.v3(
        data1=m4.sorted_data,
        data2=m14.data,
        on='date',
        how='left',
        sort=False
    )
    
    m10 = M.instruments.v2(
        start_date='2019-01-01',
        end_date='2019-11-15',
        market='CN_STOCK_A',
        instrument_list='000300.HIX',
        max_count=0
    )
    
    列: ['date', 'instrument']
    /data: 200
    
    {'end_date': '2019-11-15', 'instruments': '510050.HOF', 'start_date': '2019-01-18'}
    

    数据源(use_datasource)使用错误,你可以:

    1.一键查看文档

    2.一键搜索答案

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-12-15d89ce7388f> in <module>()
        110     datasource_id='bar1d_index_CN_STOCK_A',
        111     start_date='',
    --> 112     end_date=''
        113 )
    
    TypeError: only list-like objects are allowed to be passed to isin(), you passed a [str]

    (xysgs) #2

    可以运行一下证券代码列表再查看结果,证券代码列表字典中instrument键对应的是一个证券代码的list,把自定义python模块代码中instrument的格式改成list就行了:


    (189) #3

    多谢,我试过改为list,结果是STR自动分开了,你运行一下这里的m12数据源模块就看到了,也不知怎么解释这个古怪原因


    (xysgs) #4

    这样可以了,刚才list函数用错了,应该用list.append


    (189) #5

    多谢,这次可以了,确实是这个问题!