数据源分钟数据,我想把所有股票当天的15分钟收盘价相加得出一个当天日数据,再新建一个日时间列对应相应的新数据,应该怎么弄?

策略分享
新手专区
标签: #<Tag:0x00007f8c60cd84a0> #<Tag:0x00007f8c60cd8360>

(tkbq77) #1
克隆策略

    {"Description":"实验创建于2020/10/24","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-1385:features","SourceOutputPortId":"-1391:data"},{"DestinationInputPortId":"-1385:instruments","SourceOutputPortId":"-1395:data"}],"ModuleNodes":[{"Id":"-1385","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar15m_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"2020-10-20","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2020-10-23","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1385"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1385"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1385","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-1391","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"close","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-1391"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1391","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-1395","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2020-10-20","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2020-10-23","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":"-1395"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1395","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"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='-1385' Position='253.453125,290,200,200'/><NodePosition Node='-1391' Position='417.453125,130,200,200'/><NodePosition Node='-1395' Position='97.453125,171,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]:
    # 本代码由可视化策略环境自动生成 2020年10月24日 16:06
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m2 = M.input_features.v1(
        features='close'
    )
    
    m3 = M.instruments.v2(
        start_date='2020-10-20',
        end_date='2020-10-23',
        market='CN_STOCK_A',
        instrument_list='',
        max_count=0
    )
    
    m1 = M.use_datasource.v1(
        instruments=m3.data,
        features=m2.data,
        datasource_id='bar15m_CN_STOCK_A',
        start_date='2020-10-20',
        end_date='2020-10-23'
    )
    
    In [15]:
    m1.data.read_all_df()
    
    Out[15]:
    date instrument close
    0 2020-10-20 09:45:00 603527.SHA 9.060000
    1 2020-10-20 09:45:00 603538.SHA 37.849998
    2 2020-10-20 09:45:00 603528.SHA 11.700000
    3 2020-10-20 09:45:00 603535.SHA 49.500000
    4 2020-10-20 09:45:00 603559.SHA 17.990000
    5 2020-10-20 09:45:00 603551.SHA 15.970000
    6 2020-10-20 09:45:00 603556.SHA 14.280000
    7 2020-10-20 09:45:00 603555.SHA 2.320000
    8 2020-10-20 09:45:00 603536.SHA 20.629999
    9 2020-10-20 09:45:00 603587.SHA 22.590000
    10 2020-10-20 09:45:00 603613.SHA 95.550003
    11 2020-10-20 09:45:00 603557.SHA 14.380000
    12 2020-10-20 09:45:00 603629.SHA 28.780001
    13 2020-10-20 09:45:00 603619.SHA 11.300000
    14 2020-10-20 09:45:00 603588.SHA 14.790000
    15 2020-10-20 09:45:00 603590.SHA 40.110001
    16 2020-10-20 09:45:00 603630.SHA 16.320000
    17 2020-10-20 09:45:00 603628.SHA 9.210000
    18 2020-10-20 09:45:00 603626.SHA 17.150000
    19 2020-10-20 09:45:00 603618.SHA 5.590000
    20 2020-10-20 09:45:00 603617.SHA 12.190000
    21 2020-10-20 09:45:00 603616.SHA 6.530000
    22 2020-10-20 09:45:00 603615.SHA 11.710000
    23 2020-10-20 09:45:00 603565.SHA 23.879999
    24 2020-10-20 09:45:00 603520.SHA 76.959999
    25 2020-10-20 09:45:00 603558.SHA 10.060000
    26 2020-10-20 09:45:00 603533.SHA 26.930000
    27 2020-10-20 09:45:00 603728.SHA 21.950001
    28 2020-10-20 09:45:00 603530.SHA 22.299999
    29 2020-10-20 09:45:00 688588.SHA 30.889999
    ... ... ... ...
    258482 2020-10-23 15:00:00 002002.SZA 3.760000
    258483 2020-10-23 15:00:00 601368.SHA 5.400000
    258484 2020-10-23 15:00:00 300111.SZA 4.120000
    258485 2020-10-23 15:00:00 300110.SZA 5.500000
    258486 2020-10-23 15:00:00 601369.SHA 6.530000
    258487 2020-10-23 15:00:00 002003.SZA 6.570000
    258488 2020-10-23 15:00:00 300109.SZA 18.500000
    258489 2020-10-23 15:00:00 300108.SZA 4.770000
    258490 2020-10-23 15:00:00 603948.SHA 21.290001
    258491 2020-10-23 15:00:00 300125.SZA 14.230000
    258492 2020-10-23 15:00:00 300116.SZA 3.600000
    258493 2020-10-23 15:00:00 300117.SZA 5.510000
    258494 2020-10-23 15:00:00 601328.SHA 4.650000
    258495 2020-10-23 15:00:00 300124.SZA 62.180000
    258496 2020-10-23 15:00:00 300123.SZA 13.600000
    258497 2020-10-23 15:00:00 001896.SZA 3.760000
    258498 2020-10-23 15:00:00 601330.SHA 9.640000
    258499 2020-10-23 15:00:00 300122.SZA 143.699997
    258500 2020-10-23 15:00:00 300121.SZA 9.490000
    258501 2020-10-23 15:00:00 001979.SZA 15.300000
    258502 2020-10-23 15:00:00 001914.SZA 24.480000
    258503 2020-10-23 15:00:00 603950.SHA 26.459999
    258504 2020-10-23 15:00:00 300120.SZA 13.640000
    258505 2020-10-23 15:00:00 300119.SZA 21.870001
    258506 2020-10-23 15:00:00 601336.SHA 67.699997
    258507 2020-10-23 15:00:00 001965.SZA 6.870000
    258508 2020-10-23 15:00:00 300118.SZA 16.920000
    258509 2020-10-23 15:00:00 601339.SHA 4.930000
    258510 2020-10-23 15:00:00 601333.SHA 2.360000
    258511 2020-10-23 15:00:00 688981.SHA 57.220001

    258512 rows × 3 columns


    (adhaha111) #4
    克隆策略

      {"Description":"实验创建于2020/10/24","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-38:input_2","SourceOutputPortId":"-1385:data"},{"DestinationInputPortId":"-1385:features","SourceOutputPortId":"-1391:data"},{"DestinationInputPortId":"-1385:instruments","SourceOutputPortId":"-1395:data"}],"ModuleNodes":[{"Id":"-1385","ModuleId":"BigQuantSpace.use_datasource.use_datasource-v1","ModuleParameters":[{"Name":"datasource_id","Value":"bar15m_CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"start_date","Value":"2020-10-20","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2020-10-23","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1385"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-1385"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1385","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-1391","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"close\ndate_ymd = date","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-1391"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1391","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-1395","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2020-10-20","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2020-10-23","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":"-1395"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1395","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-38","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_2.read_df()\n df[\"date_ymd\"] = df[\"date\"].apply(lambda x: x.strftime(\"%Y-%m-%d\"))\n data_1 = df.groupby([\"date_ymd\", \"instrument\"]).sum()\n return Outputs(data_1=DataSource.write_df(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":"","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":"-38"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-38"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-38"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-38","OutputType":null},{"Name":"data_2","NodeId":"-38","OutputType":null},{"Name":"data_3","NodeId":"-38","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"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='-1385' Position='513.421875,374.34718322753906,200,200'/><NodePosition Node='-1391' Position='666.8784790039062,249.14036560058594,200,200'/><NodePosition Node='-1395' Position='339.49810791015625,247.96678161621094,200,200'/><NodePosition Node='-38' Position='514.1041870117188,465.0754699707031,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 [41]:
      # 本代码由可视化策略环境自动生成 2020年10月26日 09:18
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
      def m4_run_bigquant_run(input_1, input_2, input_3):
          # 示例代码如下。在这里编写您的代码
          df = input_2.read_df()
          df["date_ymd"] = df["date"].apply(lambda x: x.strftime("%Y-%m-%d"))
          data_1 = df.groupby(["date_ymd", "instrument"]).sum()
          return Outputs(data_1=DataSource.write_df(data_1))
      
      # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
      def m4_post_run_bigquant_run(outputs):
          return outputs
      
      
      m2 = M.input_features.v1(
          features="""close
      date_ymd = date"""
      )
      
      m3 = M.instruments.v2(
          start_date='2020-10-20',
          end_date='2020-10-23',
          market='CN_STOCK_A',
          instrument_list='',
          max_count=0
      )
      
      m1 = M.use_datasource.v1(
          instruments=m3.data,
          features=m2.data,
          datasource_id='bar15m_CN_STOCK_A',
          start_date='2020-10-20',
          end_date='2020-10-23'
      )
      
      m4 = M.cached.v3(
          input_2=m1.data,
          run=m4_run_bigquant_run,
          post_run=m4_post_run_bigquant_run,
          input_ports='',
          params='{}',
          output_ports=''
      )
      
      In [26]:
      df = m4.data_1.read()
      df[(df["date"] == "2020-10-20") & (df["instrument"] == "000001.SZA")]
      
      Out[26]:
      date instrument close
      In [33]:
      df["date_ymd"] = df["date"].strftime("%Y-%m-%d")
      
      ---------------------------------------------------------------------------
      AttributeError                            Traceback (most recent call last)
      <ipython-input-33-bd8840774ebd> in <module>()
      ----> 1 df["date_ymd"] = df["date"].strftime("%Y-%m-%d")
      
      AttributeError: 'Series' object has no attribute 'strftime'