def save_eval_result(self): tools.listToCsv(self.result_folder+'log.csv',self.train_ave_profit_list,self.ave_Q_list,self.ave_reward_list,self.test_ave_profit_list,self.epsilon_list) #2軸使用 fig, axis1 = plt.subplots() axis2 = axis1.twinx() axis1.set_ylabel('ave_max_Q') axis2.set_ylabel('epsilon') axis1.plot(self.ave_Q_list, label = "ave_max_Q") axis1.legend(loc = 'upper left') axis2.plot(self.epsilon_list, label = 'epsilon', color = 'g') axis2.legend() filename = self.result_folder + "log_ave_max_Q.png" plt.grid(which='major') plt.savefig(filename) plt.close() #2軸使用 fig, axis1 = plt.subplots() axis2 = axis1.twinx() axis1.set_ylabel('ave_reward') axis2.set_ylabel('epsilon') axis1.plot(self.ave_reward_list, label = "ave_reward") reward_ema = ta.EMA(np.array(self.ave_reward_list,dtype='f8'),timeperiod=10) axis1.plot(reward_ema, color = 'r') axis1.legend(loc = 'upper left') axis2.plot(self.epsilon_list, label = 'epsilon', color = 'g') axis2.legend() filename = self.result_folder + "log_ave_reward.png" plt.grid(which='major') plt.savefig(filename) plt.close() #2軸使用 fig, axis1 = plt.subplots() axis2 = axis1.twinx() axis1.set_ylabel('ave_train_profit') axis2.set_ylabel('epsilon') axis1.plot(self.train_ave_profit_list, label = "ave_train_profit") train_ema = ta.EMA(np.array(self.train_ave_profit_list,dtype='f8'),timeperiod=10) axis1.plot(train_ema, color = 'r') axis1.legend(loc = 'upper left') axis2.plot(self.epsilon_list, label = 'epsilon', color = 'g') axis2.legend() filename = self.result_folder + "log_ave_train_profit.png" plt.grid(which='major') plt.savefig(filename) plt.close() #2軸使用 fig, axis1 = plt.subplots() axis2 = axis1.twinx() axis1.set_ylabel('ave_test_profit') axis2.set_ylabel('epsilon') axis1.plot(self.test_ave_profit_list, label = "ave_test_profit") test_ema = ta.EMA(np.array(self.test_ave_profit_list,dtype='f8'),timeperiod=10) axis1.plot(test_ema, color = 'r') axis1.legend(loc = 'upper left') axis2.plot(self.epsilon_list, label = 'epsilon', color = 'g') axis2.legend() filename = self.result_folder + "log_ave_test_profit.png" plt.savefig(filename) plt.close()
Agent.DQN.reset_optimizer() stock_agent = env_stockmarket.Stock_agent(Agent, args.action_split_number) try: testdata, testprice = market.get_testData(f, args.input_num) #testdata, testprice = market.get_trainData(f,END_TRAIN_DAY,args.input_num) except: print 'skip', f continue profit_ratio, proper, order, stocks, price, Q_list, ave_buyprice_list, reward_list = stock_agent.trading_test( args.input_num, testprice, testdata) profit_list.append(profit_ratio) tools.listToCsv(folder + str(f).replace(".CSV", "") + '.csv', price, proper, order, stocks, ave_buyprice_list, reward_list) buy_order, sell_order = tools.order2buysell(order, price) #2軸使用 fig, axis1 = plt.subplots() axis2 = axis1.twinx() axis1.set_ylabel('price') axis1.set_ylabel('buy') axis1.set_ylabel('sell') axis2.set_ylabel('property') axis1.plot(price, label="price") axis1.plot(buy_order, 'o', label='buy point') axis1.plot(sell_order, '^', label='sell point') axis1.legend(loc='upper left')
def log_result(): # Logger global train_loss_list, mean_error_list train_count = 0 train_cur_loss = 0 train_cur_accuracy = 0 begin_at = time.time() val_begin_at = None while True: result = res_q.get() if result == 'end': print(file=sys.stderr) break elif result == 'train': print(file=sys.stderr) train = True if val_begin_at is not None: begin_at += time.time() - val_begin_at val_begin_at = None continue elif result == 'val': print(file=sys.stderr) train = False val_count = val_loss = val_accuracy = 0 val_begin_at = time.time() continue loss, accuracy = result if train: train_count += 1 duration = time.time() - begin_at throughput = train_count * args.batchsize / duration sys.stderr.write( '\rtrain {} updates ({} samples) time: {} ({} images/sec)' .format(train_count, train_count * args.batchsize, datetime.timedelta(seconds=duration), throughput)) train_cur_loss += loss train_cur_accuracy += accuracy if train_count % 10 == 0: mean_loss = train_cur_loss / 10 train_loss_list.append(mean_loss) mean_error = 1 - train_cur_accuracy / 10 mean_error_list.append(mean_error) tools.listToCsv(folder+'trainloss.csv',train_loss_list,mean_error_list) print(file=sys.stderr) print(json.dumps({'type': 'train', 'iteration': train_count, 'error': mean_error, 'loss': mean_loss})) sys.stdout.flush() train_cur_loss = 0 train_cur_accuracy = 0 else: val_count += args.batchsize duration = time.time() - val_begin_at throughput = val_count / duration sys.stderr.write( '\rval {} batches ({} samples) time: {} ({} images/sec)' .format(val_count / args.batchsize, val_count, datetime.timedelta(seconds=duration), throughput)) val_loss += loss val_accuracy += accuracy if val_count == 10000: mean_loss = val_loss * args.batchsize / 10000 mean_error = 1 - val_accuracy * args.batchsize / 10000 print(file=sys.stderr) print(json.dumps({'type': 'val', 'iteration': train_count, 'error': mean_error, 'loss': mean_loss})) sys.stdout.flush()
for f in files: print f stock_agent = env_stockmarket.Stock_agent(Agent) try: testdata,testprice = market.get_testData(f,args.input_num) #testdata, testprice = market.get_trainData(f,END_TRAIN_DAY,args.input_num) except: print 'skip',f continue profit_ratio, proper, order, stocks, price = stock_agent.trading_test(args.input_num,testprice,testdata) profit_list.append(profit_ratio) tools.listToCsv(folder+str(f).replace(".CSV", "")+'.csv', price, proper, order,stocks) buy_order, sell_order = tools.order2buysell(order,price) #2軸使用 fig, axis1 = plt.subplots() axis2 = axis1.twinx() axis1.set_ylabel('price') axis1.set_ylabel('buy') axis1.set_ylabel('sell') axis2.set_ylabel('property') axis1.plot(price, label = "price") axis1.plot(buy_order,'o',label='buy') axis1.plot(sell_order,'^',label='sell') axis1.legend(loc = 'upper left') axis2.plot(proper, label = 'property', color = 'g')
inputlist = row[:-output_num-2] output = row[-output_num-2] inputlist = np.array([inputlist]).astype(np.float32) outputlist.append(output) y = model.predict(xp.asarray(inputlist)) predictlist.append(y.data[0,0]) rec.append((output - y.data[0,0])*(output - y.data[0,0])) price = tools.getClose(args.code,START_TEST_DAY) #print len(test) #print len(outputlist), len(predictlist) ,len(price) ema_list = ta.EMA(np.array(rec,dtype='f8'),timeperiod=60) tools.listToCsv(folder+'visuallizer' + str(args.code)+'.csv',price[:-NEXT_DAY+1],outputlist,predictlist,rec,ema_list) #可視化 #2軸使用 fig, axis1 = plt.subplots() axis2 = axis1.twinx() axis1.set_ylabel('price') axis2.set_ylabel('move ratio') axis1.plot(price, label = "price") axis1.legend(loc = 'upper left') axis2.plot(outputlist, label = 'True', color = 'g') axis2.plot(predictlist, label = 'predict', color = 'r') axis2.plot(ema_list,label='ema',color='y') #axis2.plot(error, label = 'square error', color = 'm') axis2.legend() plt.show() print 'finished!!'
for row in test: inputlist = row[:-output_num-2] output = row[-output_num-2] inputlist = np.array([inputlist]).astype(np.float32) y = model_3.predict(xp.asarray(inputlist)) rec_3.append((output - y.data[0,0])*(output - y.data[0,0])) price = tools.getClose(args.code,START_TEST_DAY) #print len(test) #print len(outputlist), len(predictlist) ,len(price) sma_list_1 = ta.SMA(np.array(rec_1,dtype='f8'),timeperiod=60) sma_list_2 = ta.SMA(np.array(rec_2,dtype='f8'),timeperiod=60) sma_list_3 = ta.SMA(np.array(rec_3,dtype='f8'),timeperiod=60) tools.listToCsv(folder+'visuallizer_multi' + str(args.code)+'.csv',price[:-NEXT_DAY+1],sma_list_1,sma_list_2,sma_list_3) #可視化 #2軸使用 pic_name = folder + 'visuallizer_multi' + str(args.code)+'.png' fig, axis1 = plt.subplots() axis2 = axis1.twinx() axis1.set_ylabel('price') axis2.set_ylabel('move ratio') axis1.plot(price, label = "price") axis1.legend(loc = 'upper left') axis2.plot(sma_list_1,label='model_1',color='y') axis2.plot(sma_list_2,label='model_2',color='c') axis2.plot(sma_list_3,label='model_3',color='m') #axis2.plot(error, label = 'square error', color = 'm') axis2.legend()
def main(files): start_trading_day = 20000104 end_trading_day = 20081230 #start_trading_day = 20090105 #end_trading_day = 20101229 meigara_count = 0 profit_ratio_rsi_list = [] profit_ratio_macd_list = [] profit_ratio_gd_list = [] profit_ratio_stoch_list = [] profit_ratio_rsi_list = [] bh_profit_ratio_list = [] for f in tqdm(files): #print f _property = 0 #総資産 money = 1000000 #所持金 filepath = args.data_folder + '/' + f #株価データの読み込み _time, _open, _max, _min, _close, _volume, _keisu, _shihon = make_dataset.readfile( filepath) try: iday = _time.index(start_trading_day) eday = _time.index(end_trading_day) except: #print "can't find start_test_day" continue #start_trading_dayが見つからなければ次のファイルへ point_rsi = getStrategy_RSI(start_trading_day, end_trading_day, _time, _close) point_macd = getStrategy_MACD(start_trading_day, end_trading_day, _time, _close) point_gd = getStrategy_GD(start_trading_day, end_trading_day, _time, _close) point_stoch = getStrategy_STOCH(start_trading_day, end_trading_day, _time, _close, _max, _min) #売買開始日からスライス _time = _time[iday:eday] _close = _close[iday:eday] #buy&holdの利益率を計算 bh_profit_ratio = float((_close[-1] - _close[0]) / _close[0]) * 100 bh_profit_ratio_list.append(bh_profit_ratio) profit_ratio = trading(money, point_rsi, _close)[0] profit_ratio_rsi_list.append(profit_ratio) profit_ratio = trading(money, point_macd, _close)[0] profit_ratio_macd_list.append(profit_ratio) profit_ratio = trading(money, point_gd, _close)[0] profit_ratio_gd_list.append(profit_ratio) profit_ratio = trading(money, point_stoch, _close)[0] profit_ratio_stoch_list.append(profit_ratio) meigara_count += 1 #print meigara_count print "RSI profit average is = ", sum(profit_ratio_rsi_list) / len( profit_ratio_rsi_list) print "MACD profit average is = ", sum(profit_ratio_macd_list) / len( profit_ratio_macd_list) print "GD profit average is = ", sum(profit_ratio_gd_list) / len( profit_ratio_gd_list) print "STOCH profit average is = ", sum(profit_ratio_stoch_list) / len( profit_ratio_stoch_list) print 'buy&hold profit = ', sum(bh_profit_ratio_list) / len( bh_profit_ratio_list) print "all meigara is %d" % meigara_count tools.listToCsv('profit_ratio_list.csv', bh_profit_ratio_list, profit_ratio_gd_list, profit_ratio_macd_list, profit_ratio_rsi_list, profit_ratio_stoch_list)