Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,43 @@
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)
state = getState(data, 0, window_size + 1)

total_profit = 0
agent.inventory = []
b=0
a=0

for t in xrange(l):
action = agent.act(state)

# 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

Expand Down