交易记录和计划交易数据爬取模块简介

用户成长系列
新手专区
标签: #<Tag:0x00007fb3de4fdbb8> #<Tag:0x00007fb3de4fda50>

(lilong) #1

功能简介

随着平台上策略越来越多,很多小伙伴都分享或订阅过策略了。如何获取已有策略的交易记录/计划交易数据,从而进一步分析或者基于已有策略研究新的策略呢?这里介绍两个新的模块:
(1)交易记录爬取模块
爬取某策略的所有历史调仓记录。
可基于获得的历史交易数据,分析该策略历史选股特点。也可加入个人想法生成新的策略,比如加入大盘择时、止盈、止损等约束,生成新的策略并回测。
(2)计划交易爬取模块
爬取某策略计划下单情况。
可基于获得的计划交易数据,生成新的策略并提交模拟交易。
下面分别介绍两个模块的使用方法。

交易记录爬取模块

输入参数
在参数栏填入策略id、用户名和密码。如图所示。
image
其中策略id是策略链接中的"strategy?id"数据。用户名和密码是爬取数据时用来验证身份。填写完成后直接运行即可。
运行结果

克隆策略

    {"Description":"实验创建于2018/11/19","Summary":"","Graph":{"EdgesInternal":[],"ModuleNodes":[{"Id":"-99","ModuleId":"BigQuantSpace.get_history_orders.get_history_orders-v4","ModuleParameters":[{"Name":"strategy_id","Value":"1146","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"username","Value":"******","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"pwd","Value":"******","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-99","OutputType":null},{"Name":"data_2","NodeId":"-99","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":5,"IsPartOfPartialRun":null,"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='-99' Position='-261,-77,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 [1]:
    # 本代码由可视化策略环境自动生成 2018年12月1日 12:09
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    m5 = M.get_history_orders.v4(
        strategy_id='1146',
        username='******',
        pwd='******',
        m_cached=False
    )
    
    [2018-12-01 10:27:17.851351] INFO: bigquant: get_history_orders.v4 开始运行..
    [2018-12-01 10:27:19.987280] INFO: bigquant: get_history_orders.v4 运行完成[2.13593s].
    
    In [7]:
    m5.data_1.read_pickle().keys()
    
    Out[7]:
    dict_keys(['instruments', 'start_date', 'end_date'])
    In [2]:
    m5.data_2.read_df().head()
    
    Out[2]:
    adjust_factor amount amount_after_adjust commission cost direction dt name price price_after_adjust sid value date
    1191 10.153639 92.947347 943 5.000000 5.00 open 北新建材 148.344666 14.61 000786.SZA 13788.24 2017-07-14
    1190 6.503039 508.059081 3303 5.847953 5.85 open 华泰股份 38.367931 5.90 600308.SHA 19493.18 2017-07-14
    1188 6.503039 365.436276 2376 5.000000 5.00 open 华泰股份 37.847687 5.82 600308.SHA 13830.92 2017-07-17
    1189 11.723419 176.603616 2070 5.819889 5.82 open 海欣股份 109.848434 9.37 600851.SHA 19399.63 2017-07-17
    1186 15.968259 182.150504 2908 5.837613 5.84 open 新海宜 106.827652 6.69 002089.SZA 19458.71 2017-07-18

    数据解释
    data_1:dict类型 ,包含了策略调仓的起始时间、结束时间和所有股票;
    data_2:dataframe类型,包含了以下字段:

    • adjust_factor:复权因子
    • amount:成交量(按照策略设置的复权方式)
    • amount_after_adjust:成交量(真实值)
    • commission/cost:佣金
    • direction:买卖方向
    • dt:交易时间
    • name:股票简称
    • price:价格(按照策略设置的复权方式)
    • price_after_adjust:价格(真实值)
    • sid:股票代码
    • value:成交金额
    • date:决策日(成交的前一交易日)

    需要注意的是,为方便回测,date字段是成交的前一交易日。

    计划交易爬取模块

    输入参数
    可以绑定实盘,用于提交模拟交易。证券代码列表模块设置如图所示。


    在计划交易数据爬取模块参数栏填入策略id、用户名和密码,与交易记录获取模块相同,如图所示。
    image
    运行结果

    克隆策略

      {"Description":"实验创建于2018/11/19","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-100:input_1","SourceOutputPortId":"-106:data"}],"ModuleNodes":[{"Id":"-100","ModuleId":"BigQuantSpace.get_plan_orders.get_plan_orders-v1","ModuleParameters":[{"Name":"strategy_id","Value":"2055","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"username","Value":"*****","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"pwd","Value":"*****","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-100"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-100","OutputType":null},{"Name":"data_2","NodeId":"-100","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-106","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"","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":"-106"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-106","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"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='-100' Position='792,-18,200,200'/><NodePosition Node='-106' Position='786,-161,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 [1]:
      # 本代码由可视化策略环境自动生成 2018年12月1日 12:02
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      m7 = M.instruments.v2(
          start_date=T.live_run_param('trading_date', ''),
          end_date=T.live_run_param('trading_date', ''),
          market='CN_STOCK_A',
          instrument_list='',
          max_count=0
      )
      
      m1 = M.get_plan_orders.v1(
          input_1=m7.data,
          strategy_id='2055',
          username='*****',
          pwd='*****'
      )
      
      [2018-12-01 11:46:17.596980] INFO: bigquant: instruments.v2 开始运行..
      [2018-12-01 11:46:17.663189] INFO: bigquant: 命中缓存
      [2018-12-01 11:46:17.664629] INFO: bigquant: instruments.v2 运行完成[0.06769s].
      [2018-12-01 11:46:17.670210] INFO: bigquant: get_plan_orders.v1 开始运行..
      [2018-12-01 11:46:18.446802] INFO: bigquant: get_plan_orders.v1 运行完成[0.776557s].
      
      In [2]:
      m1.data_2.read_df()
      
      Out[2]:
      adjust_factor amount amount_after_adjust date direction dt name price sid
      0 1.495013 -286469.948727 428276 2018-11-30 2018-12-03 15:00:00 农业银行 3.57 601288.SHA
      1 3.251595 13940.455041 45328 2018-11-30 2018-12-03 09:30:00 北京银行 5.93 601169.SHA
      2 3.970253 2679.801294 10639 2018-11-30 2018-12-03 09:30:00 兴业银行 15.94 601166.SHA

      数据解释
      data_2:dataframe类型,包含了以下字段:

      • adjust_factor:复权因子
      • amount:成交量(按照策略设置的复权方式)
      • amount_after_adjust:成交量(真实值)
      • date:决策日(计划交易的前一交易日)
      • direction:买卖方向
      • dt:计划交易时间
      • name:股票简称
      • price:价格(真实价格)
      • sid:股票代码