如何在一个策略里保存数据,并在另一个策略调用该数据?

datasource
标签: #<Tag:0x00007fcf62861710>

(路飞) #1

如题。之所以这样,其实是为了便于数据管理。


(iQuant) #2

其实,在BigQuant策略研究平台,不仅可以保存模块(model),保存库(library),还能保存数据(data).
保存数据应该就能解决您的需求,保存数据是通过DataSource来实现的,想了解DataSource可以参考:datasource

具体使用可以参考:

克隆策略

    {"Description":"实验创建于2018/1/15","Summary":"","Graph":{"EdgesInternal":[],"ModuleNodes":[{"Id":"-214","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 df = pd.read_csv('my_data.csv') # 将数据读取出来\n ds = DataSource.write_df(df) # 存为DataSource\n return Outputs(data_1=ds, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-214"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-214"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-214"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-214","OutputType":null},{"Name":"data_2","NodeId":"-214","OutputType":null},{"Name":"data_3","NodeId":"-214","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"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='-214' Position='154,119,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 [11]:
    # 本代码由可视化策略环境自动生成 2018年1月15日 11:46
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m1_run_bigquant_run(input_1, input_2, input_3):
        df = pd.read_csv('my_data.csv') # 将数据读取出来
        ds = DataSource.write_df(df) # 存为DataSource
        return Outputs(data_1=ds, data_2=None, data_3=None)
    
    m1 = M.cached.v3(
        run=m1_run_bigquant_run
    )
    
    [2018-01-15 11:29:43.147591] INFO: bigquant: cached.v3 开始运行..
    [2018-01-15 11:29:43.169707] INFO: bigquant: cached.v3 运行完成[0.022119s].
    
    In [18]:
    # 查询datasource的id,在BigQuant的策略研究平台,都可以通过该id调用数据,本文举例是在同一个策略里保存和调用,但其实,在不同的策略里,也是可以调用的。
    ds_id = m1.data_1.id
    
    In [28]:
    # 通过id,获取数据
    data = DataSource(id=ds_id).read_df()
    data.head()
    
    Out[28]:
    Unnamed: 0 date close instrument
    0 0 2017-01-03 2752.580566 000002.SZA
    1 1 2017-01-04 2768.514404 000002.SZA
    2 2 2017-01-05 2779.137207 000002.SZA
    3 3 2017-01-06 2740.630127 000002.SZA
    4 4 2017-01-09 2743.285889 000002.SZA

    (zrld) #3

    如果这个策略每天都会更新,另一个策略的读取会不会受到影响?