克隆社区的DQN策略,为啥出现这错误呢

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

(wygwsg) #1
克隆策略

    {"Description":"实验创建于2019/7/10","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-213:options_data","SourceOutputPortId":"-3071:data"},{"DestinationInputPortId":"-213:instruments","SourceOutputPortId":"-2172:data"},{"DestinationInputPortId":"-56:input_2","SourceOutputPortId":"-2172:data"},{"DestinationInputPortId":"-56:input_1","SourceOutputPortId":"-49:data"},{"DestinationInputPortId":"-3071:input_data","SourceOutputPortId":"-56:data_1"}],"ModuleNodes":[{"Id":"-3071","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-3071"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-3071","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"Comment":"","CommentCollapsed":true},{"Id":"-213","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n context.pre_act = context.options['data'].read_df()\n # 系统已经设置了默认的交易手续费和滑点,要修改手续()费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n \n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n sid = context.symbol(context.instruments[0])\n \n action = context.pre_act[context.pre_act['date'] == data.current_dt.strftime('%Y-%m-%d')]\n \n # 持仓\n cur_position = context.portfolio.positions[sid].amount \n \n if len(action['pre_action'])>0:\n\n if int(action['pre_action'])==0 and cur_position ==0:\n #cur_position=cur_position+1\n context.order_target_percent(sid, 1)\n elif int(action['pre_action'])==1 and cur_position > 0:\n context.order_target_percent(sid, 0)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"100000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"000300.SHA","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-213"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-213"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-213"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-213"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-213"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-213","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"Comment":"","CommentCollapsed":true},{"Id":"-2172","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2016-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2017-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"002425.SZA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-2172"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2172","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-49","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2010-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2016-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"002425.SZA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-49"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-49","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-56","ModuleId":"BigQuantSpace.DQN_v2.DQN_v2-v3","ModuleParameters":[{"Name":"batch_size","Value":32,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"window_size","Value":"6","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"total_episode","Value":"5","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"epsilon_decay","Value":0.995,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"learning_rate","Value":0.01,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"gamma","Value":"0.85","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-56"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-56"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-56","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"Comment":"","CommentCollapsed":true},{"Id":"-557","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":"-557"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-557"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-557","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"Comment":"","CommentCollapsed":true},{"Id":"-2317","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":"-2317"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-2317"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-2317","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"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='-3071' Position='-210,437,200,200'/><NodePosition Node='-213' Position='-96,566,200,200'/><NodePosition Node='-2172' Position='427,106,200,200'/><NodePosition Node='-49' Position='-519,59,200,200'/><NodePosition Node='-56' Position='-204,334,200,200'/><NodePosition Node='-557' Position='-47.96185302734375,102.0417709350586,200,200'/><NodePosition Node='-2317' Position='-34.391754150390625,-14.779595732688904,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 [6]:
    # 本代码由可视化策略环境自动生成 2020年3月19日 22:28
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:初始化函数,只执行一次
    def m3_initialize_bigquant_run(context):
        # 加载预测数据
        context.pre_act = context.options['data'].read_df()
        # 系统已经设置了默认的交易手续费和滑点,要修改手续()费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
         
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m3_handle_data_bigquant_run(context, data):
        sid = context.symbol(context.instruments[0])
        
        action = context.pre_act[context.pre_act['date'] == data.current_dt.strftime('%Y-%m-%d')]
        
        # 持仓
        cur_position = context.portfolio.positions[sid].amount 
        
        if len(action['pre_action'])>0:
    
            if int(action['pre_action'])==0 and cur_position ==0:
                #cur_position=cur_position+1
                context.order_target_percent(sid, 1)
            elif int(action['pre_action'])==1 and cur_position > 0:
                context.order_target_percent(sid, 0)
    
    # 回测引擎:准备数据,只执行一次
    def m3_prepare_bigquant_run(context):
        pass
    
    # 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
    def m3_before_trading_start_bigquant_run(context, data):
        pass
    
    
    m4 = M.instruments.v2(
        start_date='2016-01-01',
        end_date='2017-01-01',
        market='CN_STOCK_A',
        instrument_list='002425.SZA',
        max_count=0
    )
    
    m1 = M.instruments.v2(
        start_date='2010-01-01',
        end_date='2016-01-01',
        market='CN_STOCK_A',
        instrument_list='002425.SZA',
        max_count=0
    )
    
    m6 = M.DQN_v2.v3(
        input_1=m1.data,
        input_2=m4.data,
        batch_size=32,
        window_size=6,
        total_episode=5,
        epsilon_decay=0.995,
        learning_rate=0.01,
        gamma=0.85
    )
    
    m2 = M.dropnan.v1(
        input_data=m6.data_1
    )
    
    m3 = M.trade.v4(
        instruments=m4.data,
        options_data=m2.data,
        start_date='',
        end_date='',
        initialize=m3_initialize_bigquant_run,
        handle_data=m3_handle_data_bigquant_run,
        prepare=m3_prepare_bigquant_run,
        before_trading_start=m3_before_trading_start_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=100000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark='000300.SHA'
    )
    
    m5 = M.use_datasource.v1(
        datasource_id='bar1d_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    
    m7 = M.use_datasource.v1(
        datasource_id='bar1d_CN_STOCK_A',
        start_date='',
        end_date=''
    )
    

    DQN_v2(DQN_v2)使用错误,你可以:

    1.一键查看文档

    2.一键搜索答案

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-6-d362ebcd064c> in <module>()
         61     epsilon_decay=0.995,
         62     learning_rate=0.01,
    ---> 63     gamma=0.85
         64 )
         65 
    
    AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

    (iQuant) #2

    tf升级了,参考这个例子

    克隆策略

      {"Description":"实验创建于2020/3/20","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-5:input_1","SourceOutputPortId":"-44:data"},{"DestinationInputPortId":"-5:input_2","SourceOutputPortId":"-59:data"}],"ModuleNodes":[{"Id":"-5","ModuleId":"BigQuantSpace.DQN_tf2.DQN_tf2-v1","ModuleParameters":[{"Name":"batch_size","Value":32,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"window_size","Value":5,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"total_episode","Value":"2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"epsilon_decay","Value":0.995,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"learning_rate","Value":0.01,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"gamma","Value":0.95,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-5"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-5"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-5","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"Comment":"","CommentCollapsed":true},{"Id":"-44","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-01-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2018-03-31","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000001.SZA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-44"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-44","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"Comment":"","CommentCollapsed":true},{"Id":"-59","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2019-08-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-10-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"000001.SZA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-59"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-59","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"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='-57.40476608276367,184.89437866210938,200,200'/><NodePosition Node='-44' Position='-228.36128044128418,41.09837341308594,200,200'/><NodePosition Node='-59' Position='158.64449310302734,43.60509490966797,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]:
      # 本代码由可视化策略环境自动生成 2020年3月20日 14:52
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      m4 = M.instruments.v2(
          start_date='2018-01-01',
          end_date='2018-03-31',
          market='CN_STOCK_A',
          instrument_list='000001.SZA',
          max_count=0
      )
      
      m6 = M.instruments.v2(
          start_date='2019-08-01',
          end_date='2019-10-01',
          market='CN_STOCK_A',
          instrument_list='000001.SZA',
          max_count=0
      )
      
      m1 = M.DQN_tf2.v1(
          input_1=m4.data,
          input_2=m6.data,
          batch_size=32,
          window_size=5,
          total_episode=2,
          epsilon_decay=0.995,
          learning_rate=0.01,
          gamma=0.95
      )
      
      本次验证的股票标的为: 000001.SZA
      episode: 0
      episode time:  156 current_value: 114175.97534179688  init_value:  141051.904296875  value if not act: 119471.2158203125
      reward: -5295.240478515625
      episode: 1
      episode time:  398 current_value: 113575.33068847656  init_value:  141051.904296875  value if not act: 119471.2158203125
      reward: -5895.8851318359375
      buy and hold benchmark return :  0.10567377658188781
      prediction action result :              date
      pre_action      
      0             29
      1              7
      

      DQN个股择时策略研究遇到的问题
      (wygwsg) #3

      嗯,我自己换成了tf2的模块,好像是在跑,但一直看不到结果,它跑一个episode需要多久啊


      (billy0211) #4

      用了这个模块后,我换一只股票代码,依旧出现同样的问题