-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevaluate_buy.py
65 lines (46 loc) · 1.61 KB
/
evaluate_buy.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
# Actor-Critic Buy
import numpy as np
import random
from keras.models import load_model
from functions import *
from agent.agent import A2CAgent
data = getStockDataVec("taiW_test")
l = len(data) - 1
window_size = 10
actor_model = "models/model_actor-1514.hdf5"
critic_model = "models/model_critic-1514.hdf5"
agent = A2CAgent(window_size, action_size=3,load_models = True, actor_model_file = actor_model, critic_model_file = critic_model)
total_profit = 0
agent.inventory = []
actionN = []
tradeN = 0
winN = 0
state = getState(data, 0, window_size + 1)
for t in range(l):
action = agent.act(state)
actionN.append(action)
# sit
next_state = getState(data, t + 1, window_size + 1)
if action == 1: # buy
#if action == 1 and len(agent.inventory) < 1: # buy Start & one position only
agent.inventory.append(data[t])
print(str(t)+" Buy: " + str(data[t]))
elif action == 2 and len(agent.inventory) > 0: # sell
tradeN += 1
bought_price = agent.inventory.pop(0)
profit = data[t] - bought_price
total_profit += profit
if profit > 0:
winN += 1
print(str(t)+" Sell: " + str(data[t]) + " | Profit: " + str(data[t] - bought_price))
else:
print(str(t))
done = True if t == l - 1 else False
state = next_state
if done:
winNR = 100*winN/tradeN
print ("--------------------------------")
print (" Total Profit: " + formatPrice(total_profit))
print ("Winning Rate: {:.2f} %".format(winNR))
print ("Trade No: "+str(tradeN))
print ("--------------------------------")