-
Notifications
You must be signed in to change notification settings - Fork 20
/
evaluate.py
73 lines (63 loc) · 1.57 KB
/
evaluate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import torch
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
from agent.agent import Agent
from functions import *
stock_name = '^HSI_2018'
window_size = 10
agent = Agent(window_size, True)
data = getStockDataVec(stock_name)
l = len(data) - 1
batch_size = 32
episode_count = 1000
closes = []
buys = []
sells = []
for e in range(episode_count):
closes = []
buys = []
sells = []
state = getState(data, 0, window_size + 1)
total_profit = 0
agent.inventory = []
capital = 100000
for t in range(l):
#action = agent.act(state)
action = np.random.randint(0, 3)
closes.append(data[t])
# sit
next_state = getState(data, t + 1, window_size + 1)
reward = 0
if action == 1: # buy
if capital > data[t]:
agent.inventory.append(data[t])
buys.append(data[t])
sells.append(None)
capital -= data[t]
else:
buys.append(None)
sells.append(None)
elif action == 2: # sell
if len(agent.inventory) > 0:
bought_price = agent.inventory.pop(0)
reward = max(data[t] - bought_price, 0)
total_profit += data[t] - bought_price
buys.append(None)
sells.append(data[t])
capital += data[t]
else:
buys.append(None)
sells.append(None)
elif action == 0:
buys.append(None)
sells.append(None)
done = True if t == l - 1 else False
agent.memory.push(state, action, next_state, reward)
state = next_state
if done:
print("--------------------------------")
print(stock_name + " Total Profit: " + formatPrice(total_profit))
print("--------------------------------")