Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify code for python 3.6 #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def act(self, state):
def expReplay(self, batch_size):
mini_batch = []
l = len(self.memory)
for i in xrange(l - batch_size + 1, l):
for i in range(l - batch_size + 1, l):
mini_batch.append(self.memory[i])

for state, action, reward, next_state, done in mini_batch:
Expand Down
14 changes: 7 additions & 7 deletions evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys

if len(sys.argv) != 3:
print "Usage: python evaluate.py [stock] [model]"
print("Usage: python evaluate.py [stock] [model]")
exit()

stock_name, model_name = sys.argv[1], sys.argv[2]
Expand All @@ -22,7 +22,7 @@
total_profit = 0
agent.inventory = []

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

# sit
Expand All @@ -31,19 +31,19 @@

if action == 1: # buy
agent.inventory.append(data[t])
print "Buy: " + formatPrice(data[t])
print("Buy: " + formatPrice(data[t]))

elif action == 2 and len(agent.inventory) > 0: # sell
bought_price = agent.inventory.pop(0)
reward = max(data[t] - bought_price, 0)
total_profit += data[t] - bought_price
print "Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price)
print("Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price))

done = True if t == l - 1 else False
agent.memory.append((state, action, reward, next_state, done))
state = next_state

if done:
print "--------------------------------"
print stock_name + " Total Profit: " + formatPrice(total_profit)
print "--------------------------------"
print("--------------------------------")
print(stock_name + " Total Profit: " + formatPrice(total_profit))
print("--------------------------------")
2 changes: 1 addition & 1 deletion functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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 = []
for i in xrange(n - 1):
for i in range(n - 1):
res.append(sigmoid(block[i + 1] - block[i]))

return np.array([res])
18 changes: 9 additions & 9 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

if len(sys.argv) != 4:
print "Usage: python train.py [stock] [window] [episodes]"
print("Usage: python train.py [stock] [window] [episodes]")
exit()

stock_name, window_size, episode_count = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
Expand All @@ -13,14 +13,14 @@
l = len(data) - 1
batch_size = 32

for e in xrange(episode_count + 1):
print "Episode " + str(e) + "/" + str(episode_count)
for e in range(episode_count + 1):
print("Episode " + str(e) + "/" + str(episode_count))
state = getState(data, 0, window_size + 1)

total_profit = 0
agent.inventory = []

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

# sit
Expand All @@ -29,22 +29,22 @@

if action == 1: # buy
agent.inventory.append(data[t])
print "Buy: " + formatPrice(data[t])
print("Buy: " + formatPrice(data[t]))

elif action == 2 and len(agent.inventory) > 0: # sell
bought_price = agent.inventory.pop(0)
reward = max(data[t] - bought_price, 0)
total_profit += data[t] - bought_price
print "Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price)
print("Sell: " + formatPrice(data[t]) + " | Profit: " + formatPrice(data[t] - bought_price))

done = True if t == l - 1 else False
agent.memory.append((state, action, reward, next_state, done))
state = next_state

if done:
print "--------------------------------"
print "Total Profit: " + formatPrice(total_profit)
print "--------------------------------"
print("--------------------------------")
print("Total Profit: " + formatPrice(total_profit))
print("--------------------------------")

if len(agent.memory) > batch_size:
agent.expReplay(batch_size)
Expand Down