From dbb2f5baf888f75b537ea0521f4a91b5ab31e600 Mon Sep 17 00:00:00 2001 From: satinder singh Date: Mon, 18 Feb 2019 23:33:18 +0530 Subject: [PATCH] Changing the way reward is given so that agent performs well. When I trained your model for the first time, I saw that the agent was not buying anything on actual data. I made some changes to the way, the reward was given. I have tested it on a few stocks and I am getting descent profits. --- train.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/train.py b/train.py index c4ba8ed..b35e77e 100644 --- a/train.py +++ b/train.py @@ -11,7 +11,7 @@ agent = Agent(window_size) data = getStockDataVec(stock_name) l = len(data) - 1 -batch_size = 32 +batch_size = 64 for e in xrange(episode_count + 1): print "Episode " + str(e) + "/" + str(episode_count) @@ -19,6 +19,8 @@ total_profit = 0 agent.inventory = [] + b=0 + a=0 for t in xrange(l): action = agent.act(state) @@ -26,18 +28,26 @@ # sit next_state = getState(data, t + 1, window_size + 1) reward = 0 + a+=1 if action == 1: # buy agent.inventory.append(data[t]) print "Buy: " + formatPrice(data[t]) + b=b+1 + a=0 elif action == 2 and len(agent.inventory) > 0: # sell bought_price = agent.inventory.pop(0) - reward = max(data[t] - bought_price, 0) + reward =(data[t] - bought_price)*100 total_profit += data[t] - bought_price + b=0 + a=0 print "Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price) + if(b>10 or a>20): + reward+=-200 done = True if t == l - 1 else False + agent.memory.append((state, action, reward, next_state, done)) state = next_state