-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_trading_env.py.save.1
160 lines (136 loc) · 6.31 KB
/
stock_trading_env.py.save.1
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import gym
from gym import spaces
from gym.utils import seeding
from order import trade_order
import numpy as np
class StockTradingEnv(gym.Env):
def __init__(self, data, window_size, current_step=0):
self.stock_prices = data
self.position = None # could be 'buy', 'hold', or 'sell'
self.current_step = current_step
self.done = False
self.bought_price = None
self.observation_space = spaces.Box(low=0, high=1, shape=(len(data), len(self.stock_prices)), dtype=np.float32)
self.action_space = spaces.Discrete(3)
self.render_mode = 'human'
self.reset_info = {}
self.orders = np.array([])
self.max_orders = 5
self.max_order_length = 5
self.net_reward = 0
self.window_size = window_size
self.order_count = 0
if current_step == 0:
self.current_step = window_size-1
def get_percent_change(self, current_price, future_price):
if future_price == 0:
return 0
return ((future_price - current_price)/future_price)*100
def scaled_sigmoid(self, n, i):
if abs(i) > n:
i = n
scaled_x = 10 * (abs(i) / n) - 5
return 1 / (1 + np.exp(-scaled_x))
def piecewise(self, p, theta, _max):
if abs(p) <= theta:
return 3*(1-self.scaled_sigmoid(theta, p))
elif abs(p) > theta:
return -3*(self.scaled_sigmoid(_max, p))
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def reset(self, seed=None):
if self.current_step >= (len(self.stock_prices) - 1-self.window_size):
self.current_step = self.window_size-1
self.position = None
self.done = False
self.orders = np.array([])
return self.stock_prices
def get_best_reward(self, n=5):
if self.current_step + n > len(self.stock_prices):
n = (len(self.stock_prices)-1)-self.current_step
min = 1000
max = -1000
action = 0
percent_gain = 3
for i in range(n):
percent_change = self.get_percent_change(self.stock_prices[self.current_step][0], self.stock_prices[self.current_step+n][0])
if percent_change > max:
max = percent_change
elif percent_change < min:
min = percent_change
if max > percent_gain and min <= -percent_gain/2:
action = 1
break
elif min < -percent_gain and max >= percent_gain/2:
action = 2
break
if (max > percent_gain or min < -percent_gain) and action == 0:
if max > abs(min):
action = 1
else:
action = 2
print(f"Best action: {action} Reward:{max if action == 1 else min if action == 2 else (max, min)}")
return action
def get_net_rewards(self):
return self.net_reward
def get_reward(self, current_price, forward_index=-1):
reward = 0
closed_orders = []
for order in self.orders:
order_percent = order.get_percent_change(current_price[0])
order_state = order.check_change(current_price[0]) # returns 0 nothing, 1 exit
if forward_index == -1:
print(f"Order {order.order_id} Order type: {order.order_type} Percent: {order_percent}")
if order.order_type == "sell":
order_percent = order_percent * -1
if order_state != 0 or order.life_counter >= self.max_order_length:
if order_state == 1:
reward += order_percent
else:
reward -= order.percent_gain/2
if forward_index == -1:
self.orders = np.delete(self.orders, np.where(self.orders==order)[0][0])
else:
life = order.life_counter
if life+forward_index >= self.max_order_length and forward_index != -1:
life = self.max_order_length
reward += (order_percent * (1-order.scaled_sigmoid(self.max_order_length, order.life_counter)))
if forward_index == -1:
order.life_counter = order.life_counter + 1
return reward
def step(self, action, n=5, step=-1):
self.current_step += 1
if self.current_step >= (len(self.stock_prices) - 1) or self.current_step >= len(self.stock_prices)-self.window_size:
self.done = True
self.current_step = len(self.stock_prices)-self.window_size
if not self.done:
current_price = self.stock_prices[self.current_step]
else:
current_price = self.stock_prices[-1]
if (action == [1] or action == [2]) and self.orders.size <= self.max_orders:
_type = "buy"
if action == [2]:
_type = "sell"
self.orders = np.append(self.orders, trade_order(_type, current_price[0], self.order_count))
self.order_count += 1
reward = self.get_reward(current_price)
n_rewards = []
if self.current_step + n > len(self.stock_prices):
n = (len(self.stock_prices)-1)-self.current_step
if n != 0:
future_price = self.stock_prices[self.current_step+1]
if future_price[0] == 0.0:
future_price[0] = current_price[0]
order_percent = ((future_price[0]-current_price[0])/future_price[0])*100
print(f"Percent Change: {order_percent}")
if action == [0]:
future_price = self.stock_prices[self.current_step+n]
order_percent = ((future_price[0]-current_price[0])/future_price[0])*100
print(f"Percent Change (Hold): {order_percent}")
reward += self.piecewise(order_percent, 1, 3)
for i in range(n):
n_rewards.append(self.get_reward(self.stock_prices[self.current_step+i+1], forward_index=i))
self.net_reward += reward
self.reset_info = {"net reward": self.net_reward, "orders": self.orders.size, "step": self.current_step, "n_rewards": n_rewards}
return self.stock_prices, reward, self.done, False, self.reset_info