在网络上复制的resnet18模型和官方DQN策略的结合,主要是替换了官方的全连接模型改用resnet,不过出错啦,求大神能让它运行

策略分享
标签: #<Tag:0x00007fc069ea9120>

(tkyz) #1
克隆策略

    {"Description":"实验创建于2019/7/10","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-213:options_data","SourceOutputPortId":"-3071:data"},{"DestinationInputPortId":"-213:instruments","SourceOutputPortId":"-2172:data"},{"DestinationInputPortId":"-2180:input_2","SourceOutputPortId":"-2172:data"},{"DestinationInputPortId":"-2180:input_1","SourceOutputPortId":"-49:data"},{"DestinationInputPortId":"-3071:input_data","SourceOutputPortId":"-2180: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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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":"000004.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,"IsPartOfPartialRun":null,"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":"000004.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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2180","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\nimport keras\nfrom keras.models import Sequential\nfrom keras.models import load_model\nfrom keras.layers import Dense\nfrom keras.optimizers import Adam\nfrom keras.layers import Input\nfrom keras.layers import Conv2D, MaxPool2D, Dense, BatchNormalization, Activation, add, GlobalAvgPool2D\nfrom keras.models import Model\nfrom keras import regularizers\nfrom keras.utils import plot_model\nfrom keras import backend as K\n\ndef conv2d_bn(x, nb_filter, kernel_size, strides=(1, 1), padding='same'):\n \"\"\"\n conv2d -> batch normalization -> relu activation\n \"\"\"\n x = Conv2D(nb_filter, kernel_size=kernel_size,\n strides=strides,\n padding=padding,\n kernel_regularizer=regularizers.l2(0.0001))(x)\n x = BatchNormalization()(x)\n x = Activation('relu')(x)\n return x\n \ndef shortcut(input_s, residual):\n \"\"\"\n shortcut连接,也就是identity mapping部分。\n \"\"\"\n \n input_shape = K.int_shape(input_s)\n residual_shape = K.int_shape(residual)\n stride_height = int(round(input_shape[1] / residual_shape[1]))\n stride_width = int(round(input_shape[2] / residual_shape[2]))\n equal_channels = input_shape[3] == residual_shape[3]\n \n identity = input_s\n # 如果维度不同,则使用1x1卷积进行调整\n if stride_width > 1 or stride_height > 1 or not equal_channels:\n identity = Conv2D(filters=residual_shape[3],\n kernel_size=(1, 1),\n strides=(stride_width, stride_height),\n padding=\"valid\",\n kernel_regularizer=regularizers.l2(0.0001))(input_s)\n \n return add([identity, residual])\n \ndef basic_block(nb_filter, strides=(1, 1)):\n \"\"\"\n 基本的ResNet building block,适用于ResNet-18和ResNet-34.\n \"\"\"\n def f(input_s):\n \n conv1 = conv2d_bn(input_s, nb_filter, kernel_size=(3, 3), strides=strides)\n residual = conv2d_bn(conv1, nb_filter, kernel_size=(3, 3))\n \n return shortcut(input_s, residual)\n \n return f\n \ndef residual_block(nb_filter, repetitions, is_first_layer=False):\n \"\"\"\n 构建每层的residual模块,对应论文参数统计表中的conv2_x -> conv5_x\n \"\"\"\n def f(input_s):\n for i in range(repetitions):\n strides = (1, 1)\n if i == 0 and not is_first_layer:\n strides = (2, 2)\n input_s = basic_block(nb_filter, strides)(input_s)\n return input_s\n \n return f\n#--------------------- \n#版权声明:本文为CSDN博主「z小白」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。\n#原文链接:https://blog.csdn.net/zzc15806/article/details/83540661''\n# returns the sigmoid\n\ndef sigmoid(x):\n return 1 / (1 + math.exp(-x))\n\n# returns an an n-day state representation ending at time t\ndef getState(data, t, n):\n d = t - n + 1\n block = data[d:t + 1] #if d >= 0 else -d * data[0] + data[0:t + 1] # pad with t0\n res = []\n #print(d,t+1,block)\n for i in range(n - 1):\n res.append(sigmoid(block[i + 1][0] - block[i][0]))\n\n return np.array([res])\n\nclass Agent(object):\n def __init__(self,window_size,epsilon_decay,learning_rate,gamma):\n self.action_num=2\n #self.model=self.build_model(window_size)\n self.model=self.build_model()\n self.epsilon=1.0\n self.epsilon_min=0.001\n self.epsilon_decay=epsilon_decay\n self.learning_rate=learning_rate\n self.memory=[]\n self.gamma=gamma\n self.inventory=[]\n self.window_size=window_size\n \n# def build_model(self,window_size):\n# model = Sequential()\n# model.add(Dense(units=64, input_dim=window_size, activation=\"relu\"))\n# model.add(Dense(units=32, activation=\"relu\"))\n# model.add(Dense(units=8, activation=\"relu\"))\n # model.add(Dense(self.action_num, activation=\"linear\"))\n # model.compile(loss=\"mse\", optimizer=Adam(lr=0.001))\n \n #return model\n def build_model(self,input_shape=(224,224,3), nclass=1000):\n\n input_ = Input(shape=input_shape)\n \n conv1 = conv2d_bn(input_, 64, kernel_size=(7, 7), strides=(2, 2))\n pool1 = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same')(conv1)\n \n conv2 = residual_block(64, 2, is_first_layer=True)(pool1)\n conv3 = residual_block(128, 2, is_first_layer=True)(conv2)\n conv4 = residual_block(256, 2, is_first_layer=True)(conv3)\n conv5 = residual_block(512, 2, is_first_layer=True)(conv4)\n \n pool2 = GlobalAvgPool2D()(conv5)\n output_ = Dense(nclass, activation='softmax')(pool2)\n \n model = Model(inputs=input_, outputs=output_)\n model.summary()\n \n return model\n\n \n def choose_act(self,state):\n trade_off=np.random.rand()\n if trade_off>self.epsilon:\n q_value=self.model.predict(state)\n action=np.argmax(q_value[0])\n else:\n action=np.random.randint(2)\n return action\n \n \n def replay(self,batch_size):\n \n data=random.sample(self.memory,batch_size) # 这一步是需要有的。经验回放的原则\n \n for state,action,reward,new_state in data:\n target=reward\n #if done==False:\n \n q_value=self.model.predict(state)\n target=reward+self.gamma*np.amax(self.model.predict(new_state)[0])\n q_value[0][action]=target\n \n self.model.fit(state,q_value,epochs=1,verbose=0)\n if self.epsilon>self.epsilon_min:\n self.epsilon=self.epsilon*self.epsilon_decay\n\ndef predict(data,agent,window_size):\n \n pre_action=[] \n state=getState(data, window_size, window_size+1)\n for i in range(window_size, data.shape[0]-1):\n next_state=getState(data, i+1, window_size+1)\n action=agent.choose_act(state)\n pre_action.append(action)\n \n state=next_state\n return(np.array(pre_action))\n \ndef bigquant_run(input_1, input_2,batch_size=32,window_size=5,total_episode=5,epsilon_decay=0.995,learning_rate=0.01,gamma=0.95):\n \n agent=Agent(window_size,epsilon_decay,learning_rate,gamma)\n \n stock =input_1.read_pickle()['instruments']\n print('本次验证的股票标的为: %s'%stock[0])\n \n data1_info=input_1.read_pickle()\n data1=DataSource('bar1d_CN_STOCK_A').read(instruments=data1_info['instruments'],start_date=data1_info['start_date'],end_date=data1_info['end_date'],fields=['close'])\n #data=data.drop(['label'],axis=1)\n #data=data.drop(['label'],axis=1)\n data1=data1.drop(['instrument'],axis=1)\n data1=data1.drop(['date'],axis=1)\n data1=np.array(data1)\n \n output_reward=[]\n \n for e in range(total_episode):\n print('episode:',e)\n t0 = datetime.datetime.now()\n \n total_reward=0\n cash=2000\n hold_volume=100\n \n #state=data.iloc[[0]]\n state=getState(data1, window_size, window_size+1) \n old_portpolio=cash+hold_volume*data1[5][0]\n init_portpolio=cash+hold_volume*data1[5][0]\n for i in range(window_size,data1.shape[0]-1):\n next_state=getState(data1, i+1, window_size+1)\n action=agent.choose_act(state)\n reward=0\n if action==0:#buy\n #agent.inventory.append(float(state['m:close']))\n cash=cash-data1[i][0]\n hold_volume=hold_volume+1\n \n elif action==1 and len(agent.inventory)>0: #sell\n #bought_price=agent.inventory.pop()\n #if float(state['m:close'])-bought_price>0:\n # reward=float(state['m:close'])-bought_price\n #total_reward=total_reward+reward\n cash=cash+data1[i][0]\n hold_volume=hold_volume-1\n new_portpolio=cash+hold_volume*data1[i][0]\n reward=new_portpolio-old_portpolio\n \n agent.memory.append((state,action,reward,next_state))\n state=next_state\n old_portpolio=new_portpolio\n \n if len(agent.memory)>batch_size:\n agent.replay(batch_size)\n \n #output_reward.append(total_reward)\n t1 = datetime.datetime.now()\n print('episode time: ', (t1-t0).seconds, 'current_value:',old_portpolio,' init_value: ',init_portpolio,' value if not act:',2000+100*data1[i][0])\n print('reward:',old_portpolio-(2000+100*data1[i][0]))\n #print(output_reward) \n \n \n \n data2_info=input_2.read_pickle()\n data_2=DataSource('bar1d_CN_STOCK_A').read(instruments=data2_info['instruments'],start_date=data2_info['start_date'],end_date=data2_info['end_date'],fields=['close'])\n print('buy and hold benchmark return : ',list(data_2['close'])[-1] / list(data_2['close'])[0] -1)\n \n data_3=data_2.drop(['instrument'],axis=1)\n data_3=data_3.drop(['date'],axis=1)\n data_3=np.array(data_3)\n pre_action=predict(data_3,agent,window_size)\n pre_data=pd.DataFrame({'date':data_2['date'][window_size+1:],'pre_action': pre_action})\n print('prediction action result : ',pre_data.groupby('pre_action').count())\n \n data_1_ds = DataSource.write_df(pre_data)\n \n \n return Outputs(data_1=data_1_ds, 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":"input_1, input_2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{\n 'batch_size': 32,\n 'window_size': 5,\n 'total_episode': 5,\n 'epsilon_decay': 0.995,\n 'learning_rate': 0.01,\n 'gamma': 0.95\n}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"data_1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-2180"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-2180"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-2180"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-2180","OutputType":null},{"Name":"data_2","NodeId":"-2180","OutputType":null},{"Name":"data_3","NodeId":"-2180","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"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='-3071' Position='-210,437,200,200'/><NodePosition Node='-213' Position='-96,566,200,200'/><NodePosition Node='-2172' Position='20,187,200,200'/><NodePosition Node='-49' Position='-336,117,200,200'/><NodePosition Node='-2180' Position='-129,320,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 [2]:
    # 本代码由可视化策略环境自动生成 2019年8月12日 23:38
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    import keras
    from keras.models import Sequential
    from keras.models import load_model
    from keras.layers import Dense
    from keras.optimizers import Adam
    from keras.layers import Input
    from keras.layers import Conv2D, MaxPool2D, Dense, BatchNormalization, Activation, add, GlobalAvgPool2D
    from keras.models import Model
    from keras import regularizers
    from keras.utils import plot_model
    from keras import backend as K
    
    def conv2d_bn(x, nb_filter, kernel_size, strides=(1, 1), padding='same'):
        """
        conv2d -> batch normalization -> relu activation
        """
        x = Conv2D(nb_filter, kernel_size=kernel_size,
                              strides=strides,
                              padding=padding,
                              kernel_regularizer=regularizers.l2(0.0001))(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        return x
     
    def shortcut(input_s, residual):
        """
        shortcut连接,也就是identity mapping部分。
        """
     
        input_shape = K.int_shape(input_s)
        residual_shape = K.int_shape(residual)
        stride_height = int(round(input_shape[1] / residual_shape[1]))
        stride_width = int(round(input_shape[2] / residual_shape[2]))
        equal_channels = input_shape[3] == residual_shape[3]
     
        identity = input_s
        # 如果维度不同,则使用1x1卷积进行调整
        if stride_width > 1 or stride_height > 1 or not equal_channels:
            identity = Conv2D(filters=residual_shape[3],
                               kernel_size=(1, 1),
                               strides=(stride_width, stride_height),
                               padding="valid",
                               kernel_regularizer=regularizers.l2(0.0001))(input_s)
     
        return add([identity, residual])
     
    def basic_block(nb_filter, strides=(1, 1)):
        """
        基本的ResNet building block,适用于ResNet-18和ResNet-34.
        """
        def f(input_s):
     
            conv1 = conv2d_bn(input_s, nb_filter, kernel_size=(3, 3), strides=strides)
            residual = conv2d_bn(conv1, nb_filter, kernel_size=(3, 3))
     
            return shortcut(input_s, residual)
     
        return f
     
    def residual_block(nb_filter, repetitions, is_first_layer=False):
        """
        构建每层的residual模块,对应论文参数统计表中的conv2_x -> conv5_x
        """
        def f(input_s):
            for i in range(repetitions):
                strides = (1, 1)
                if i == 0 and not is_first_layer:
                    strides = (2, 2)
                input_s = basic_block(nb_filter, strides)(input_s)
            return input_s
     
        return f
    #--------------------- 
    #版权声明:本文为CSDN博主「z小白」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
    #原文链接:https://blog.csdn.net/zzc15806/article/details/83540661''
    # returns the sigmoid
    
    def sigmoid(x):
        return 1 / (1 + math.exp(-x))
    
    # returns an an n-day state representation ending at time t
    def getState(data, t, n):
        d = t - n + 1
        block = data[d:t + 1] #if d >= 0 else -d * data[0] + data[0:t + 1] # pad with t0
        res = []
        #print(d,t+1,block)
        for i in range(n - 1):
            res.append(sigmoid(block[i + 1][0] - block[i][0]))
    
        return np.array([res])
    
    class Agent(object):
        def __init__(self,window_size,epsilon_decay,learning_rate,gamma):
            self.action_num=2
            #self.model=self.build_model(window_size)
            self.model=self.build_model()
            self.epsilon=1.0
            self.epsilon_min=0.001
            self.epsilon_decay=epsilon_decay
            self.learning_rate=learning_rate
            self.memory=[]
            self.gamma=gamma
            self.inventory=[]
            self.window_size=window_size
        
    #    def build_model(self,window_size):
    #        model = Sequential()
    #        model.add(Dense(units=64, input_dim=window_size, activation="relu"))
    #        model.add(Dense(units=32, activation="relu"))
    #        model.add(Dense(units=8, activation="relu"))
          #  model.add(Dense(self.action_num, activation="linear"))
          #  model.compile(loss="mse", optimizer=Adam(lr=0.001))
            
            #return model
        def build_model(self,input_shape=(224,224,3), nclass=1000):
    
            input_ = Input(shape=input_shape)
     
            conv1 = conv2d_bn(input_, 64, kernel_size=(7, 7), strides=(2, 2))
            pool1 = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same')(conv1)
     
            conv2 = residual_block(64, 2, is_first_layer=True)(pool1)
            conv3 = residual_block(128, 2, is_first_layer=True)(conv2)
            conv4 = residual_block(256, 2, is_first_layer=True)(conv3)
            conv5 = residual_block(512, 2, is_first_layer=True)(conv4)
     
            pool2 = GlobalAvgPool2D()(conv5)
            output_ = Dense(nclass, activation='softmax')(pool2)
     
            model = Model(inputs=input_, outputs=output_)
            model.summary()
     
            return model
    
        
        def choose_act(self,state):
            trade_off=np.random.rand()
            if trade_off>self.epsilon:
                q_value=self.model.predict(state)
                action=np.argmax(q_value[0])
            else:
                action=np.random.randint(2)
            return action
                
        
        def replay(self,batch_size):
            
            data=random.sample(self.memory,batch_size) # 这一步是需要有的。经验回放的原则
            
            for state,action,reward,new_state in data:
                target=reward
                #if done==False:
                
                q_value=self.model.predict(state)
                target=reward+self.gamma*np.amax(self.model.predict(new_state)[0])
                q_value[0][action]=target
                
                self.model.fit(state,q_value,epochs=1,verbose=0)
            if self.epsilon>self.epsilon_min:
                self.epsilon=self.epsilon*self.epsilon_decay
    
    def predict(data,agent,window_size):
         
        pre_action=[]  
        state=getState(data, window_size, window_size+1)
        for i in range(window_size, data.shape[0]-1):
            next_state=getState(data, i+1, window_size+1)
            action=agent.choose_act(state)
            pre_action.append(action)
           
            state=next_state
        return(np.array(pre_action))
                
    def m6_run_bigquant_run(input_1, input_2,batch_size=32,window_size=5,total_episode=5,epsilon_decay=0.995,learning_rate=0.01,gamma=0.95):
        
        agent=Agent(window_size,epsilon_decay,learning_rate,gamma)
       
        stock =input_1.read_pickle()['instruments']
        print('本次验证的股票标的为: %s'%stock[0])
        
        data1_info=input_1.read_pickle()
        data1=DataSource('bar1d_CN_STOCK_A').read(instruments=data1_info['instruments'],start_date=data1_info['start_date'],end_date=data1_info['end_date'],fields=['close'])
        #data=data.drop(['label'],axis=1)
        #data=data.drop(['label'],axis=1)
        data1=data1.drop(['instrument'],axis=1)
        data1=data1.drop(['date'],axis=1)
        data1=np.array(data1)
       
        output_reward=[]
        
        for e in range(total_episode):
            print('episode:',e)
            t0 = datetime.datetime.now()
            
            total_reward=0
            cash=2000
            hold_volume=100
            
            #state=data.iloc[[0]]
            state=getState(data1, window_size, window_size+1)  
            old_portpolio=cash+hold_volume*data1[5][0]
            init_portpolio=cash+hold_volume*data1[5][0]
            for i in range(window_size,data1.shape[0]-1):
                next_state=getState(data1, i+1, window_size+1)
                action=agent.choose_act(state)
                reward=0
                if action==0:#buy
                    #agent.inventory.append(float(state['m:close']))
                    cash=cash-data1[i][0]
                    hold_volume=hold_volume+1
                    
                elif action==1 and len(agent.inventory)>0: #sell
                    #bought_price=agent.inventory.pop()
                    #if float(state['m:close'])-bought_price>0:
                    #   reward=float(state['m:close'])-bought_price
                    #total_reward=total_reward+reward
                    cash=cash+data1[i][0]
                    hold_volume=hold_volume-1
                new_portpolio=cash+hold_volume*data1[i][0]
                reward=new_portpolio-old_portpolio
                   
                agent.memory.append((state,action,reward,next_state))
                state=next_state
                old_portpolio=new_portpolio
                
                if len(agent.memory)>batch_size:
                    agent.replay(batch_size)
                    
            #output_reward.append(total_reward)
            t1 =  datetime.datetime.now()
            print('episode time: ', (t1-t0).seconds, 'current_value:',old_portpolio,' init_value: ',init_portpolio,' value if not act:',2000+100*data1[i][0])
            print('reward:',old_portpolio-(2000+100*data1[i][0]))
        #print(output_reward) 
        
        
        
        data2_info=input_2.read_pickle()
        data_2=DataSource('bar1d_CN_STOCK_A').read(instruments=data2_info['instruments'],start_date=data2_info['start_date'],end_date=data2_info['end_date'],fields=['close'])
        print('buy and hold benchmark return : ',list(data_2['close'])[-1]  / list(data_2['close'])[0] -1)
        
        data_3=data_2.drop(['instrument'],axis=1)
        data_3=data_3.drop(['date'],axis=1)
        data_3=np.array(data_3)
        pre_action=predict(data_3,agent,window_size)
        pre_data=pd.DataFrame({'date':data_2['date'][window_size+1:],'pre_action': pre_action})
        print('prediction action result : ',pre_data.groupby('pre_action').count())
     
        data_1_ds = DataSource.write_df(pre_data)
        
        
        return Outputs(data_1=data_1_ds, data_2=None, data_3=None)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m6_post_run_bigquant_run(outputs):
        return outputs
    
    # 回测引擎:初始化函数,只执行一次
    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='000004.SZA',
        max_count=0
    )
    
    m1 = M.instruments.v2(
        start_date='2010-01-01',
        end_date='2016-01-01',
        market='CN_STOCK_A',
        instrument_list='000004.SZA',
        max_count=0
    )
    
    m6 = M.cached.v3(
        input_1=m1.data,
        input_2=m4.data,
        run=m6_run_bigquant_run,
        post_run=m6_post_run_bigquant_run,
        input_ports='input_1, input_2',
        params="""{
        'batch_size': 32,
        'window_size': 5,
        'total_episode': 5,
        'epsilon_decay': 0.995,
        'learning_rate': 0.01,
        'gamma': 0.95
    }""",
        output_ports='data_1'
    )
    
    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'
    )
    
    __________________________________________________________________________________________________
    Layer (type)                    Output Shape         Param #     Connected to                     
    ==================================================================================================
    input_2 (InputLayer)            (None, 224, 224, 3)  0                                            
    __________________________________________________________________________________________________
    conv2d_21 (Conv2D)              (None, 112, 112, 64) 9472        input_2[0][0]                    
    __________________________________________________________________________________________________
    batch_normalization_18 (BatchNo (None, 112, 112, 64) 256         conv2d_21[0][0]                  
    __________________________________________________________________________________________________
    activation_18 (Activation)      (None, 112, 112, 64) 0           batch_normalization_18[0][0]     
    __________________________________________________________________________________________________
    max_pooling2d_2 (MaxPooling2D)  (None, 56, 56, 64)   0           activation_18[0][0]              
    __________________________________________________________________________________________________
    conv2d_22 (Conv2D)              (None, 56, 56, 64)   36928       max_pooling2d_2[0][0]            
    __________________________________________________________________________________________________
    batch_normalization_19 (BatchNo (None, 56, 56, 64)   256         conv2d_22[0][0]                  
    __________________________________________________________________________________________________
    activation_19 (Activation)      (None, 56, 56, 64)   0           batch_normalization_19[0][0]     
    __________________________________________________________________________________________________
    conv2d_23 (Conv2D)              (None, 56, 56, 64)   36928       activation_19[0][0]              
    __________________________________________________________________________________________________
    batch_normalization_20 (BatchNo (None, 56, 56, 64)   256         conv2d_23[0][0]                  
    __________________________________________________________________________________________________
    activation_20 (Activation)      (None, 56, 56, 64)   0           batch_normalization_20[0][0]     
    __________________________________________________________________________________________________
    add_9 (Add)                     (None, 56, 56, 64)   0           max_pooling2d_2[0][0]            
                                                                     activation_20[0][0]              
    __________________________________________________________________________________________________
    conv2d_24 (Conv2D)              (None, 56, 56, 64)   36928       add_9[0][0]                      
    __________________________________________________________________________________________________
    batch_normalization_21 (BatchNo (None, 56, 56, 64)   256         conv2d_24[0][0]                  
    __________________________________________________________________________________________________
    activation_21 (Activation)      (None, 56, 56, 64)   0           batch_normalization_21[0][0]     
    __________________________________________________________________________________________________
    conv2d_25 (Conv2D)              (None, 56, 56, 64)   36928       activation_21[0][0]              
    __________________________________________________________________________________________________
    batch_normalization_22 (BatchNo (None, 56, 56, 64)   256         conv2d_25[0][0]                  
    __________________________________________________________________________________________________
    activation_22 (Activation)      (None, 56, 56, 64)   0           batch_normalization_22[0][0]     
    __________________________________________________________________________________________________
    add_10 (Add)                    (None, 56, 56, 64)   0           add_9[0][0]                      
                                                                     activation_22[0][0]              
    __________________________________________________________________________________________________
    conv2d_26 (Conv2D)              (None, 56, 56, 128)  73856       add_10[0][0]                     
    __________________________________________________________________________________________________
    batch_normalization_23 (BatchNo (None, 56, 56, 128)  512         conv2d_26[0][0]                  
    __________________________________________________________________________________________________
    activation_23 (Activation)      (None, 56, 56, 128)  0           batch_normalization_23[0][0]     
    __________________________________________________________________________________________________
    conv2d_27 (Conv2D)              (None, 56, 56, 128)  147584      activation_23[0][0]              
    __________________________________________________________________________________________________
    batch_normalization_24 (BatchNo (None, 56, 56, 128)  512         conv2d_27[0][0]                  
    __________________________________________________________________________________________________
    conv2d_28 (Conv2D)              (None, 56, 56, 128)  8320        add_10[0][0]                     
    __________________________________________________________________________________________________
    activation_24 (Activation)      (None, 56, 56, 128)  0           batch_normalization_24[0][0]     
    __________________________________________________________________________________________________
    add_11 (Add)                    (None, 56, 56, 128)  0           conv2d_28[0][0]                  
                                                                     activation_24[0][0]              
    __________________________________________________________________________________________________
    conv2d_29 (Conv2D)              (None, 56, 56, 128)  147584      add_11[0][0]                     
    __________________________________________________________________________________________________
    batch_normalization_25 (BatchNo (None, 56, 56, 128)  512         conv2d_29[0][0]                  
    __________________________________________________________________________________________________
    activation_25 (Activation)      (None, 56, 56, 128)  0           batch_normalization_25[0][0]     
    __________________________________________________________________________________________________
    conv2d_30 (Conv2D)              (None, 56, 56, 128)  147584      activation_25[0][0]              
    __________________________________________________________________________________________________
    batch_normalization_26 (BatchNo (None, 56, 56, 128)  512         conv2d_30[0][0]                  
    __________________________________________________________________________________________________
    activation_26 (Activation)      (None, 56, 56, 128)  0           batch_normalization_26[0][0]     
    __________________________________________________________________________________________________
    add_12 (Add)                    (None, 56, 56, 128)  0           add_11[0][0]                     
                                                                     activation_26[0][0]              
    __________________________________________________________________________________________________
    conv2d_31 (Conv2D)              (None, 56, 56, 256)  295168      add_12[0][0]                     
    __________________________________________________________________________________________________
    batch_normalization_27 (BatchNo (None, 56, 56, 256)  1024        conv2d_31[0][0]                  
    __________________________________________________________________________________________________
    activation_27 (Activation)      (None, 56, 56, 256)  0           batch_normalization_27[0][0]     
    __________________________________________________________________________________________________
    conv2d_32 (Conv2D)              (None, 56, 56, 256)  590080      activation_27[0][0]              
    __________________________________________________________________________________________________
    batch_normalization_28 (BatchNo (None, 56, 56, 256)  1024        conv2d_32[0][0]                  
    __________________________________________________________________________________________________
    conv2d_33 (Conv2D)              (None, 56, 56, 256)  33024       add_12[0][0]                     
    __________________________________________________________________________________________________
    activation_28 (Activation)      (None, 56, 56, 256)  0           batch_normalization_28[0][0]     
    __________________________________________________________________________________________________
    add_13 (Add)                    (None, 56, 56, 256)  0           conv2d_33[0][0]                  
                                                                     activation_28[0][0]              
    __________________________________________________________________________________________________
    conv2d_34 (Conv2D)              (None, 56, 56, 256)  590080      add_13[0][0]                     
    __________________________________________________________________________________________________
    batch_normalization_29 (BatchNo (None, 56, 56, 256)  1024        conv2d_34[0][0]                  
    __________________________________________________________________________________________________
    activation_29 (Activation)      (None, 56, 56, 256)  0           batch_normalization_29[0][0]     
    __________________________________________________________________________________________________
    conv2d_35 (Conv2D)              (None, 56, 56, 256)  590080      activation_29[0][0]              
    __________________________________________________________________________________________________
    batch_normalization_30 (BatchNo (None, 56, 56, 256)  1024        conv2d_35[0][0]                  
    __________________________________________________________________________________________________
    activation_30 (Activation)      (None, 56, 56, 256)  0           batch_normalization_30[0][0]     
    __________________________________________________________________________________________________
    add_14 (Add)                    (None, 56, 56, 256)  0           add_13[0][0]                     
                                                                     activation_30[0][0]              
    __________________________________________________________________________________________________
    conv2d_36 (Conv2D)              (None, 56, 56, 512)  1180160     add_14[0][0]                     
    __________________________________________________________________________________________________
    batch_normalization_31 (BatchNo (None, 56, 56, 512)  2048        conv2d_36[0][0]                  
    __________________________________________________________________________________________________
    activation_31 (Activation)      (None, 56, 56, 512)  0           batch_normalization_31[0][0]     
    __________________________________________________________________________________________________
    conv2d_37 (Conv2D)              (None, 56, 56, 512)  2359808     activation_31[0][0]              
    __________________________________________________________________________________________________
    batch_normalization_32 (BatchNo (None, 56, 56, 512)  2048        conv2d_37[0][0]                  
    __________________________________________________________________________________________________
    conv2d_38 (Conv2D)              (None, 56, 56, 512)  131584      add_14[0][0]                     
    __________________________________________________________________________________________________
    activation_32 (Activation)      (None, 56, 56, 512)  0           batch_normalization_32[0][0]     
    __________________________________________________________________________________________________
    add_15 (Add)                    (None, 56, 56, 512)  0           conv2d_38[0][0]                  
                                                                     activation_32[0][0]              
    __________________________________________________________________________________________________
    conv2d_39 (Conv2D)              (None, 56, 56, 512)  2359808     add_15[0][0]                     
    __________________________________________________________________________________________________
    batch_normalization_33 (BatchNo (None, 56, 56, 512)  2048        conv2d_39[0][0]                  
    __________________________________________________________________________________________________
    activation_33 (Activation)      (None, 56, 56, 512)  0           batch_normalization_33[0][0]     
    __________________________________________________________________________________________________
    conv2d_40 (Conv2D)              (None, 56, 56, 512)  2359808     activation_33[0][0]              
    __________________________________________________________________________________________________
    batch_normalization_34 (BatchNo (None, 56, 56, 512)  2048        conv2d_40[0][0]                  
    __________________________________________________________________________________________________
    activation_34 (Activation)      (None, 56, 56, 512)  0           batch_normalization_34[0][0]     
    __________________________________________________________________________________________________
    add_16 (Add)                    (None, 56, 56, 512)  0           add_15[0][0]                     
                                                                     activation_34[0][0]              
    __________________________________________________________________________________________________
    global_average_pooling2d_2 (Glo (None, 512)          0           add_16[0][0]                     
    __________________________________________________________________________________________________
    dense_2 (Dense)                 (None, 1000)         513000      global_average_pooling2d_2[0][0] 
    ==================================================================================================
    Total params: 11,700,328
    Trainable params: 11,692,520
    Non-trainable params: 7,808
    __________________________________________________________________________________________________
    本次验证的股票标的为: 000004.SZA
    episode: 0
    
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-2-b8640afeec14> in <module>()
        324     'gamma': 0.95
        325 }""",
    --> 326     output_ports='data_1'
        327 )
        328 
    
    <ipython-input-2-b8640afeec14> in m6_run_bigquant_run(input_1, input_2, batch_size, window_size, total_episode, epsilon_decay, learning_rate, gamma)
        230 
        231             if len(agent.memory)>batch_size:
    --> 232                 agent.replay(batch_size)
        233 
        234         #output_reward.append(total_reward)
    
    <ipython-input-2-b8640afeec14> in replay(self, batch_size)
        157             #if done==False:
        158 
    --> 159             q_value=self.model.predict(state)
        160             target=reward+self.gamma*np.amax(self.model.predict(new_state)[0])
        161             q_value[0][action]=target
    
    ValueError: Error when checking : expected input_2 to have 4 dimensions, but got array with shape (1, 5)

    (xgl891) #2

    官方策略里的state数据形状是1*5的,但是你的模型的输入层可能要求数据是4维的,所以不匹配。你可以改一下输入层的参数试一下


    (tkyz) #3

    新手,不知道怎么改


    (xgl891) #4

    你的build_model()函数里有一个参数是input_shape, 它本来是(224,224,3),应该是因为这个模型原本的训练数据是224*224的图像,有3个颜色通道,所以要用224*224*3的input_shape, 这里的输入数据state是1*5的,所以你可以把input_shape参数改成(1,5)或者(5,1)或者(5), 具体是哪个我也不是很确定,但是思路大概是这样。不过改了这个之后可能还会有问题,因为你后面的卷积层都是二维的,但是输入数据本身是1维的,你可以改成1维卷积试试,之前社区有过一篇用一维卷积的文章可以参考一下,或者可以把数据改成二维的,不过这个可能性就比较多了


    (tkyz) #5

    好的,谢谢


    (tkyz) #6
    克隆策略

      {"Description":"实验创建于2019/7/10","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-213:options_data","SourceOutputPortId":"-3071:data"},{"DestinationInputPortId":"-213:instruments","SourceOutputPortId":"-2172:data"},{"DestinationInputPortId":"-2180:input_2","SourceOutputPortId":"-2172:data"},{"DestinationInputPortId":"-2180:input_1","SourceOutputPortId":"-49:data"},{"DestinationInputPortId":"-3071:input_data","SourceOutputPortId":"-2180: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,"IsPartOfPartialRun":null,"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,"IsPartOfPartialRun":null,"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":"000004.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,"IsPartOfPartialRun":null,"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":"000004.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,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-2180","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\nimport keras\nfrom keras.models import Sequential\nfrom keras.models import load_model\nfrom keras.layers import Dense\nfrom keras.optimizers import Adam\nfrom keras.layers import Input\nfrom keras.layers import Conv1D, MaxPool1D, Dense, BatchNormalization, Activation, add, GlobalAvgPool1D\nfrom keras.models import Model\nfrom keras import regularizers\nfrom keras.utils import plot_model\nfrom keras import backend as K\n\ndef conv1d_bn(x, nb_filter, kernel_size, strides=1, padding='same'):\n \"\"\"\n conv2d -> batch normalization -> relu activation\n \"\"\"\n x = Conv1D(nb_filter, kernel_size=kernel_size,\n strides=strides,\n padding=padding,\n kernel_regularizer=regularizers.l2(0.0001))(x)\n x = BatchNormalization()(x)\n x = Activation('relu')(x)\n return x\n \ndef shortcut(input_s, residual):\n \"\"\"\n shortcut连接,也就是identity mapping部分。\n \"\"\"\n \n input_shape = K.int_shape(input_s)\n residual_shape = K.int_shape(residual)\n stride_height = int(round(input_shape[1] / residual_shape[1]))\n # stride_width = int(round(input_shape[2] / residual_shape[2]))\n equal_channels = input_shape[1] == residual_shape[1]\n \n identity = input_s\n # 如果维度不同,则使用1x1卷积进行调整\n if stride_height > 1 or not equal_channels:\n identity = Conv1D(filters=residual_shape[1],\n kernel_size=1,\n strides=stride_height,\n padding=\"valid\",\n kernel_regularizer=regularizers.l2(0.0001))(input_s)\n \n return add([identity, residual])\n \ndef basic_block(nb_filter, strides=1):\n \"\"\"\n 基本的ResNet building block,适用于ResNet-18和ResNet-34.\n \"\"\"\n def f(input_s):\n \n conv1 = conv1d_bn(input_s, nb_filter, kernel_size=1, strides=strides)\n residual = conv1d_bn(conv1, nb_filter, kernel_size=1)\n \n return shortcut(input_s, residual)\n \n return f\n \ndef residual_block(nb_filter, repetitions, is_first_layer=False):\n \"\"\"\n 构建每层的residual模块,对应论文参数统计表中的conv2_x -> conv5_x\n \"\"\"\n def f(input_s):\n for i in range(repetitions):\n strides = 1\n if i == 0 and not is_first_layer:\n strides = 2\n input_s = basic_block(nb_filter, strides)(input_s)\n return input_s\n \n return f\n#--------------------- \n#版权声明:本文为CSDN博主「z小白」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。\n#原文链接:https://blog.csdn.net/zzc15806/article/details/83540661''\n# returns the sigmoid\n\ndef sigmoid(x):\n return 1 / (1 + math.exp(-x))\n\n# returns an an n-day state representation ending at time t\ndef getState(data, t, n):\n d = t - n + 1\n block = data[d:t + 1] #if d >= 0 else -d * data[0] + data[0:t + 1] # pad with t0\n res = []\n #print(d,t+1,block)\n for i in range(n - 1):\n res.append(sigmoid(block[i + 1][0] - block[i][0]))\n\n return np.array([res])\n\nclass Agent(object):\n def __init__(self,window_size,epsilon_decay,learning_rate,gamma):\n self.action_num=2\n #self.model=self.build_model(window_size)\n self.model=self.build_model()\n self.epsilon=1.0\n self.epsilon_min=0.001\n self.epsilon_decay=epsilon_decay\n self.learning_rate=learning_rate\n self.memory=[]\n self.gamma=gamma\n self.inventory=[]\n self.window_size=window_size\n \n# def build_model(self,window_size):\n# model = Sequential()\n# model.add(Dense(units=64, input_dim=window_size, activation=\"relu\"))\n# model.add(Dense(units=32, activation=\"relu\"))\n# model.add(Dense(units=8, activation=\"relu\"))\n # model.add(Dense(self.action_num, activation=\"linear\"))\n # model.compile(loss=\"mse\", optimizer=Adam(lr=0.001))\n \n #return model\n def build_model(self,input_shape=(1,5), nclass=1000):\n\n input_ = Input(shape=input_shape)\n \n conv1 = conv1d_bn(input_, 64, kernel_size=1, strides=1)\n pool1 = MaxPool1D(pool_size=1, strides=2, padding='same')(conv1)\n \n conv2 = residual_block(64, 2, is_first_layer=True)(pool1)\n # conv3 = residual_block(128, 2, is_first_layer=True)(conv2)\n # conv4 = residual_block(256, 2, is_first_layer=True)(conv3)\n # conv5 = residual_block(512, 2, is_first_layer=True)(conv4)\n \n pool2 = GlobalAvgPool1D()(conv2)\n output_ = Dense(nclass, activation='softmax')(pool2)\n \n model = Model(inputs=input_, outputs=output_)\n model.summary()\n \n return model\n\n \n def choose_act(self,state):\n trade_off=np.random.rand()\n if trade_off>self.epsilon:\n q_value=self.model.predict(state)\n action=np.argmax(q_value[0])\n else:\n action=np.random.randint(2)\n return action\n \n \n def replay(self,batch_size):\n \n data=random.sample(self.memory,batch_size) # 这一步是需要有的。经验回放的原则\n \n for state,action,reward,new_state in data:\n target=reward\n #if done==False:\n \n q_value=self.model.predict(state)\n target=reward+self.gamma*np.amax(self.model.predict(new_state)[0])\n q_value[0][action]=target\n \n self.model.fit(state,q_value,epochs=1,verbose=0)\n if self.epsilon>self.epsilon_min:\n self.epsilon=self.epsilon*self.epsilon_decay\n\ndef predict(data,agent,window_size):\n \n pre_action=[] \n state=getState(data, window_size, window_size+1)\n for i in range(window_size, data.shape[0]-1):\n next_state=getState(data, i+1, window_size+1)\n action=agent.choose_act(state)\n pre_action.append(action)\n \n state=next_state\n return(np.array(pre_action))\n \ndef bigquant_run(input_1, input_2,batch_size=32,window_size=5,total_episode=5,epsilon_decay=0.995,learning_rate=0.01,gamma=0.95):\n \n agent=Agent(window_size,epsilon_decay,learning_rate,gamma)\n \n stock =input_1.read_pickle()['instruments']\n print('本次验证的股票标的为: %s'%stock[0])\n \n data1_info=input_1.read_pickle()\n data1=DataSource('bar1d_CN_STOCK_A').read(instruments=data1_info['instruments'],start_date=data1_info['start_date'],end_date=data1_info['end_date'],fields=['close'])\n #data=data.drop(['label'],axis=1)\n #data=data.drop(['label'],axis=1)\n data1=data1.drop(['instrument'],axis=1)\n data1=data1.drop(['date'],axis=1)\n data1=np.array(data1)\n \n output_reward=[]\n \n for e in range(total_episode):\n print('episode:',e)\n t0 = datetime.datetime.now()\n \n total_reward=0\n cash=2000\n hold_volume=100\n \n #state=data.iloc[[0]]\n state=getState(data1, window_size, window_size+1) \n old_portpolio=cash+hold_volume*data1[5][0]\n init_portpolio=cash+hold_volume*data1[5][0]\n for i in range(window_size,data1.shape[0]-1):\n next_state=getState(data1, i+1, window_size+1)\n action=agent.choose_act(state)\n reward=0\n if action==0:#buy\n #agent.inventory.append(float(state['m:close']))\n cash=cash-data1[i][0]\n hold_volume=hold_volume+1\n \n elif action==1 and len(agent.inventory)>0: #sell\n #bought_price=agent.inventory.pop()\n #if float(state['m:close'])-bought_price>0:\n # reward=float(state['m:close'])-bought_price\n #total_reward=total_reward+reward\n cash=cash+data1[i][0]\n hold_volume=hold_volume-1\n new_portpolio=cash+hold_volume*data1[i][0]\n reward=new_portpolio-old_portpolio\n \n agent.memory.append((state,action,reward,next_state))\n state=next_state\n old_portpolio=new_portpolio\n \n if len(agent.memory)>batch_size:\n agent.replay(batch_size)\n \n #output_reward.append(total_reward)\n t1 = datetime.datetime.now()\n print('episode time: ', (t1-t0).seconds, 'current_value:',old_portpolio,' init_value: ',init_portpolio,' value if not act:',2000+100*data1[i][0])\n print('reward:',old_portpolio-(2000+100*data1[i][0]))\n #print(output_reward) \n \n \n \n data2_info=input_2.read_pickle()\n data_2=DataSource('bar1d_CN_STOCK_A').read(instruments=data2_info['instruments'],start_date=data2_info['start_date'],end_date=data2_info['end_date'],fields=['close'])\n print('buy and hold benchmark return : ',list(data_2['close'])[-1] / list(data_2['close'])[0] -1)\n \n data_3=data_2.drop(['instrument'],axis=1)\n data_3=data_3.drop(['date'],axis=1)\n data_3=np.array(data_3)\n pre_action=predict(data_3,agent,window_size)\n pre_data=pd.DataFrame({'date':data_2['date'][window_size+1:],'pre_action': pre_action})\n print('prediction action result : ',pre_data.groupby('pre_action').count())\n \n data_1_ds = DataSource.write_df(pre_data)\n \n \n return Outputs(data_1=data_1_ds, 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":"input_1, input_2","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{\n 'batch_size': 32,\n 'window_size': 5,\n 'total_episode': 5,\n 'epsilon_decay': 0.995,\n 'learning_rate': 0.01,\n 'gamma': 0.95\n}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"data_1","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-2180"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-2180"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-2180"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-2180","OutputType":null},{"Name":"data_2","NodeId":"-2180","OutputType":null},{"Name":"data_3","NodeId":"-2180","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"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='-3071' Position='-210,437,200,200'/><NodePosition Node='-213' Position='-96,566,200,200'/><NodePosition Node='-2172' Position='20,187,200,200'/><NodePosition Node='-49' Position='-336,117,200,200'/><NodePosition Node='-2180' Position='-130,320,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 [2]:
      # 本代码由可视化策略环境自动生成 2019年8月13日 13:37
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
      import keras
      from keras.models import Sequential
      from keras.models import load_model
      from keras.layers import Dense
      from keras.optimizers import Adam
      from keras.layers import Input
      from keras.layers import Conv1D, MaxPool1D, Dense, BatchNormalization, Activation, add, GlobalAvgPool1D
      from keras.models import Model
      from keras import regularizers
      from keras.utils import plot_model
      from keras import backend as K
      
      def conv1d_bn(x, nb_filter, kernel_size, strides=1, padding='same'):
          """
          conv2d -> batch normalization -> relu activation
          """
          x = Conv1D(nb_filter, kernel_size=kernel_size,
                                strides=strides,
                                padding=padding,
                                kernel_regularizer=regularizers.l2(0.0001))(x)
          x = BatchNormalization()(x)
          x = Activation('relu')(x)
          return x
       
      def shortcut(input_s, residual):
          """
          shortcut连接,也就是identity mapping部分。
          """
       
          input_shape = K.int_shape(input_s)
          residual_shape = K.int_shape(residual)
          stride_height = int(round(input_shape[1] / residual_shape[1]))
         # stride_width = int(round(input_shape[2] / residual_shape[2]))
          equal_channels = input_shape[1] == residual_shape[1]
       
          identity = input_s
          # 如果维度不同,则使用1x1卷积进行调整
          if stride_height > 1 or not equal_channels:
              identity = Conv1D(filters=residual_shape[1],
                                 kernel_size=1,
                                 strides=stride_height,
                                 padding="valid",
                                 kernel_regularizer=regularizers.l2(0.0001))(input_s)
       
          return add([identity, residual])
       
      def basic_block(nb_filter, strides=1):
          """
          基本的ResNet building block,适用于ResNet-18和ResNet-34.
          """
          def f(input_s):
       
              conv1 = conv1d_bn(input_s, nb_filter, kernel_size=1, strides=strides)
              residual = conv1d_bn(conv1, nb_filter, kernel_size=1)
       
              return shortcut(input_s, residual)
       
          return f
       
      def residual_block(nb_filter, repetitions, is_first_layer=False):
          """
          构建每层的residual模块,对应论文参数统计表中的conv2_x -> conv5_x
          """
          def f(input_s):
              for i in range(repetitions):
                  strides = 1
                  if i == 0 and not is_first_layer:
                      strides = 2
                  input_s = basic_block(nb_filter, strides)(input_s)
              return input_s
       
          return f
      #--------------------- 
      #版权声明:本文为CSDN博主「z小白」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
      #原文链接:https://blog.csdn.net/zzc15806/article/details/83540661''
      # returns the sigmoid
      
      def sigmoid(x):
          return 1 / (1 + math.exp(-x))
      
      # returns an an n-day state representation ending at time t
      def getState(data, t, n):
          d = t - n + 1
          block = data[d:t + 1] #if d >= 0 else -d * data[0] + data[0:t + 1] # pad with t0
          res = []
          #print(d,t+1,block)
          for i in range(n - 1):
              res.append(sigmoid(block[i + 1][0] - block[i][0]))
      
          return np.array([res])
      
      class Agent(object):
          def __init__(self,window_size,epsilon_decay,learning_rate,gamma):
              self.action_num=2
              #self.model=self.build_model(window_size)
              self.model=self.build_model()
              self.epsilon=1.0
              self.epsilon_min=0.001
              self.epsilon_decay=epsilon_decay
              self.learning_rate=learning_rate
              self.memory=[]
              self.gamma=gamma
              self.inventory=[]
              self.window_size=window_size
          
      #    def build_model(self,window_size):
      #        model = Sequential()
      #        model.add(Dense(units=64, input_dim=window_size, activation="relu"))
      #        model.add(Dense(units=32, activation="relu"))
      #        model.add(Dense(units=8, activation="relu"))
            #  model.add(Dense(self.action_num, activation="linear"))
            #  model.compile(loss="mse", optimizer=Adam(lr=0.001))
              
              #return model
          def build_model(self,input_shape=(1,5), nclass=1000):
      
              input_ = Input(shape=input_shape)
       
              conv1 = conv1d_bn(input_, 64, kernel_size=1, strides=1)
              pool1 = MaxPool1D(pool_size=1, strides=2, padding='same')(conv1)
       
              conv2 = residual_block(64, 2, is_first_layer=True)(pool1)
            #  conv3 = residual_block(128, 2, is_first_layer=True)(conv2)
             # conv4 = residual_block(256, 2, is_first_layer=True)(conv3)
            #  conv5 = residual_block(512, 2, is_first_layer=True)(conv4)
       
              pool2 = GlobalAvgPool1D()(conv2)
              output_ = Dense(nclass, activation='softmax')(pool2)
       
              model = Model(inputs=input_, outputs=output_)
              model.summary()
       
              return model
      
          
          def choose_act(self,state):
              trade_off=np.random.rand()
              if trade_off>self.epsilon:
                  q_value=self.model.predict(state)
                  action=np.argmax(q_value[0])
              else:
                  action=np.random.randint(2)
              return action
                  
          
          def replay(self,batch_size):
              
              data=random.sample(self.memory,batch_size) # 这一步是需要有的。经验回放的原则
              
              for state,action,reward,new_state in data:
                  target=reward
                  #if done==False:
                  
                  q_value=self.model.predict(state)
                  target=reward+self.gamma*np.amax(self.model.predict(new_state)[0])
                  q_value[0][action]=target
                  
                  self.model.fit(state,q_value,epochs=1,verbose=0)
              if self.epsilon>self.epsilon_min:
                  self.epsilon=self.epsilon*self.epsilon_decay
      
      def predict(data,agent,window_size):
           
          pre_action=[]  
          state=getState(data, window_size, window_size+1)
          for i in range(window_size, data.shape[0]-1):
              next_state=getState(data, i+1, window_size+1)
              action=agent.choose_act(state)
              pre_action.append(action)
             
              state=next_state
          return(np.array(pre_action))
                  
      def m6_run_bigquant_run(input_1, input_2,batch_size=32,window_size=5,total_episode=5,epsilon_decay=0.995,learning_rate=0.01,gamma=0.95):
          
          agent=Agent(window_size,epsilon_decay,learning_rate,gamma)
         
          stock =input_1.read_pickle()['instruments']
          print('本次验证的股票标的为: %s'%stock[0])
          
          data1_info=input_1.read_pickle()
          data1=DataSource('bar1d_CN_STOCK_A').read(instruments=data1_info['instruments'],start_date=data1_info['start_date'],end_date=data1_info['end_date'],fields=['close'])
          #data=data.drop(['label'],axis=1)
          #data=data.drop(['label'],axis=1)
          data1=data1.drop(['instrument'],axis=1)
          data1=data1.drop(['date'],axis=1)
          data1=np.array(data1)
         
          output_reward=[]
          
          for e in range(total_episode):
              print('episode:',e)
              t0 = datetime.datetime.now()
              
              total_reward=0
              cash=2000
              hold_volume=100
              
              #state=data.iloc[[0]]
              state=getState(data1, window_size, window_size+1)  
              old_portpolio=cash+hold_volume*data1[5][0]
              init_portpolio=cash+hold_volume*data1[5][0]
              for i in range(window_size,data1.shape[0]-1):
                  next_state=getState(data1, i+1, window_size+1)
                  action=agent.choose_act(state)
                  reward=0
                  if action==0:#buy
                      #agent.inventory.append(float(state['m:close']))
                      cash=cash-data1[i][0]
                      hold_volume=hold_volume+1
                      
                  elif action==1 and len(agent.inventory)>0: #sell
                      #bought_price=agent.inventory.pop()
                      #if float(state['m:close'])-bought_price>0:
                      #   reward=float(state['m:close'])-bought_price
                      #total_reward=total_reward+reward
                      cash=cash+data1[i][0]
                      hold_volume=hold_volume-1
                  new_portpolio=cash+hold_volume*data1[i][0]
                  reward=new_portpolio-old_portpolio
                     
                  agent.memory.append((state,action,reward,next_state))
                  state=next_state
                  old_portpolio=new_portpolio
                  
                  if len(agent.memory)>batch_size:
                      agent.replay(batch_size)
                      
              #output_reward.append(total_reward)
              t1 =  datetime.datetime.now()
              print('episode time: ', (t1-t0).seconds, 'current_value:',old_portpolio,' init_value: ',init_portpolio,' value if not act:',2000+100*data1[i][0])
              print('reward:',old_portpolio-(2000+100*data1[i][0]))
          #print(output_reward) 
          
          
          
          data2_info=input_2.read_pickle()
          data_2=DataSource('bar1d_CN_STOCK_A').read(instruments=data2_info['instruments'],start_date=data2_info['start_date'],end_date=data2_info['end_date'],fields=['close'])
          print('buy and hold benchmark return : ',list(data_2['close'])[-1]  / list(data_2['close'])[0] -1)
          
          data_3=data_2.drop(['instrument'],axis=1)
          data_3=data_3.drop(['date'],axis=1)
          data_3=np.array(data_3)
          pre_action=predict(data_3,agent,window_size)
          pre_data=pd.DataFrame({'date':data_2['date'][window_size+1:],'pre_action': pre_action})
          print('prediction action result : ',pre_data.groupby('pre_action').count())
       
          data_1_ds = DataSource.write_df(pre_data)
          
          
          return Outputs(data_1=data_1_ds, data_2=None, data_3=None)
      
      # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
      def m6_post_run_bigquant_run(outputs):
          return outputs
      
      # 回测引擎:初始化函数,只执行一次
      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='000004.SZA',
          max_count=0
      )
      
      m1 = M.instruments.v2(
          start_date='2010-01-01',
          end_date='2016-01-01',
          market='CN_STOCK_A',
          instrument_list='000004.SZA',
          max_count=0
      )
      
      m6 = M.cached.v3(
          input_1=m1.data,
          input_2=m4.data,
          run=m6_run_bigquant_run,
          post_run=m6_post_run_bigquant_run,
          input_ports='input_1, input_2',
          params="""{
          'batch_size': 32,
          'window_size': 5,
          'total_episode': 5,
          'epsilon_decay': 0.995,
          'learning_rate': 0.01,
          'gamma': 0.95
      }""",
          output_ports='data_1'
      )
      
      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'
      )
      
      __________________________________________________________________________________________________
      Layer (type)                    Output Shape         Param #     Connected to                     
      ==================================================================================================
      input_2 (InputLayer)            (None, 1, 5)         0                                            
      __________________________________________________________________________________________________
      conv1d_4 (Conv1D)               (None, 1, 64)        384         input_2[0][0]                    
      __________________________________________________________________________________________________
      batch_normalization_4 (BatchNor (None, 1, 64)        256         conv1d_4[0][0]                   
      __________________________________________________________________________________________________
      activation_4 (Activation)       (None, 1, 64)        0           batch_normalization_4[0][0]      
      __________________________________________________________________________________________________
      max_pooling1d_2 (MaxPooling1D)  (None, 1, 64)        0           activation_4[0][0]               
      __________________________________________________________________________________________________
      conv1d_5 (Conv1D)               (None, 1, 64)        4160        max_pooling1d_2[0][0]            
      __________________________________________________________________________________________________
      batch_normalization_5 (BatchNor (None, 1, 64)        256         conv1d_5[0][0]                   
      __________________________________________________________________________________________________
      activation_5 (Activation)       (None, 1, 64)        0           batch_normalization_5[0][0]      
      __________________________________________________________________________________________________
      conv1d_6 (Conv1D)               (None, 1, 64)        4160        activation_5[0][0]               
      __________________________________________________________________________________________________
      batch_normalization_6 (BatchNor (None, 1, 64)        256         conv1d_6[0][0]                   
      __________________________________________________________________________________________________
      activation_6 (Activation)       (None, 1, 64)        0           batch_normalization_6[0][0]      
      __________________________________________________________________________________________________
      add_1 (Add)                     (None, 1, 64)        0           max_pooling1d_2[0][0]            
                                                                       activation_6[0][0]               
      __________________________________________________________________________________________________
      conv1d_7 (Conv1D)               (None, 1, 64)        4160        add_1[0][0]                      
      __________________________________________________________________________________________________
      batch_normalization_7 (BatchNor (None, 1, 64)        256         conv1d_7[0][0]                   
      __________________________________________________________________________________________________
      activation_7 (Activation)       (None, 1, 64)        0           batch_normalization_7[0][0]      
      __________________________________________________________________________________________________
      conv1d_8 (Conv1D)               (None, 1, 64)        4160        activation_7[0][0]               
      __________________________________________________________________________________________________
      batch_normalization_8 (BatchNor (None, 1, 64)        256         conv1d_8[0][0]                   
      __________________________________________________________________________________________________
      activation_8 (Activation)       (None, 1, 64)        0           batch_normalization_8[0][0]      
      __________________________________________________________________________________________________
      add_2 (Add)                     (None, 1, 64)        0           add_1[0][0]                      
                                                                       activation_8[0][0]               
      __________________________________________________________________________________________________
      global_average_pooling1d_1 (Glo (None, 64)           0           add_2[0][0]                      
      __________________________________________________________________________________________________
      dense_1 (Dense)                 (None, 1000)         65000       global_average_pooling1d_1[0][0] 
      ==================================================================================================
      Total params: 83,304
      Trainable params: 82,664
      Non-trainable params: 640
      __________________________________________________________________________________________________
      本次验证的股票标的为: 000004.SZA
      episode: 0
      
      ---------------------------------------------------------------------------
      ValueError                                Traceback (most recent call last)
      <ipython-input-2-52a53fbb62e9> in <module>()
          324     'gamma': 0.95
          325 }""",
      --> 326     output_ports='data_1'
          327 )
          328 
      
      <ipython-input-2-52a53fbb62e9> in m6_run_bigquant_run(input_1, input_2, batch_size, window_size, total_episode, epsilon_decay, learning_rate, gamma)
          230 
          231             if len(agent.memory)>batch_size:
      --> 232                 agent.replay(batch_size)
          233 
          234         #output_reward.append(total_reward)
      
      <ipython-input-2-52a53fbb62e9> in replay(self, batch_size)
          157             #if done==False:
          158 
      --> 159             q_value=self.model.predict(state)
          160             target=reward+self.gamma*np.amax(self.model.predict(new_state)[0])
          161             q_value[0][action]=target
      
      ValueError: Error when checking : expected input_2 to have 3 dimensions, but got array with shape (1, 5)

      降到3维啦,不知道怎么改啦

      (xgl891) #7

      agent 里面的replay函数for state,action,reward,new_state in data:里面添一句
      state=state.reshape(1,1,5)
      new_state=new_state.reshape(1,1,5)
      之后再用predict和fit


      (tkyz) #8


      加了之后又出现这样的错误


      (xgl891) #9

      看这报错的意思好像是建模型的时候少了一句model.compile(loss=“mse”, optimizer=Adam(lr=0.001))类似这样的语句


      (tkyz) #10

      image


      加在这吗?又出同样的错误啦


      (xgl891) #11

      这个同理啦,凡是有模型输入输出的地方都会有这个问题,在输入之前对state或者next_state加一句reshape就好了,后面测试集predict的时候估计也有这个问题。如果要一劳永逸一点的话就直接在getstate那个函数输出state的时候reshape一下


      (tkyz) #12

      能运行啦,不过预测输出全是1