-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_environ.py
211 lines (181 loc) · 7.23 KB
/
stock_environ.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import gym
import gym.spaces
from gym.utils import seeding
import enum
import numpy as np
from . import data
DEFAULT_BARS_COUNT = 10
DEFAULT_COMMISSION_PERC = 0.1
class Actions(enum.Enum):
Skip = 0
Buy = 1
Close = 2
class State:
def __init__(self, bars_count, commission_perc, reset_on_close, reward_on_close=True, volumes=True):
assert isinstance(bars_count, int)
assert bars_count > 0
assert isinstance(commission_perc, float)
assert commission_perc >= 0.0
assert isinstance(reset_on_close, bool)
assert isinstance(reward_on_close, bool)
self.bars_count = bars_count
self.commission_perc = commission_perc
self.reset_on_close = reset_on_close
self.reward_on_close = reward_on_close
self.volumes = volumes
def reset(self, prices, offset):
assert isinstance(prices, data.Prices)
assert offset >= self.bars_count-1
self.have_position = False
self.open_price = 0.0
self._prices = prices
self._offset = offset
@property
def shape(self):
# [h, l, c] * bars + position_flag + rel_profit (since open)
if self.volumes:
return (4 * self.bars_count + 1 + 1, )
else:
return (3*self.bars_count + 1 + 1, )
def encode(self):
"""
Convert current state into numpy array.
"""
res = np.ndarray(shape=self.shape, dtype=np.float32)
shift = 0
for bar_idx in range(-self.bars_count+1, 1):
res[shift] = self._prices.high[self._offset + bar_idx]
shift += 1
res[shift] = self._prices.low[self._offset + bar_idx]
shift += 1
res[shift] = self._prices.close[self._offset + bar_idx]
shift += 1
if self.volumes:
res[shift] = self._prices.volume[self._offset + bar_idx]
shift += 1
res[shift] = float(self.have_position)
shift += 1
if not self.have_position:
res[shift] = 0.0
else:
res[shift] = (self._cur_close() - self.open_price) / self.open_price
return res
def _cur_close(self):
"""
Calculate real close price for the current bar
"""
open = self._prices.open[self._offset]
rel_close = self._prices.close[self._offset]
return open * (1.0 + rel_close)
def step(self, action):
"""
Perform one step in our price, adjust offset, check for the end of prices
and handle position change
:param action:
:return: reward, done
"""
assert isinstance(action, Actions)
reward = 0.0
done = False
close = self._cur_close()
if action == Actions.Buy and not self.have_position:
self.have_position = True
self.open_price = close
reward -= self.commission_perc
elif action == Actions.Close and self.have_position:
reward -= self.commission_perc
done |= self.reset_on_close
if self.reward_on_close:
reward += 100.0 * (close - self.open_price) / self.open_price
self.have_position = False
self.open_price = 0.0
self._offset += 1
prev_close = close
close = self._cur_close()
done |= self._offset >= self._prices.close.shape[0]-1
if self.have_position and not self.reward_on_close:
reward += 100.0 * (close - prev_close) / prev_close
return reward, done
class State1D(State):
"""
State with shape suitable for 1D convolution
"""
@property
def shape(self):
if self.volumes:
return (6, self.bars_count)
else:
return (5, self.bars_count)
def encode(self):
res = np.zeros(shape=self.shape, dtype=np.float32)
ofs = self.bars_count-1
res[0] = self._prices.high[self._offset-ofs:self._offset+1]
res[1] = self._prices.low[self._offset-ofs:self._offset+1]
res[2] = self._prices.close[self._offset-ofs:self._offset+1]
if self.volumes:
res[3] = self._prices.volume[self._offset-ofs:self._offset+1]
dst = 4
else:
dst = 3
if self.have_position:
res[dst] = 1.0
res[dst+1] = (self._cur_close() - self.open_price) / self.open_price
return res
class PokeEnv(gym.Env):
player_picks_text = "Go! %s"
opponent_picks_text = "%s sent out %s!"
player_pokemon_uses = "%s used %s!"
opposing_pokemon_uses = "The opposing %s used %s!"
player_pokemon_comeback = "%s Come back!"
opposing_pokemon_comeback = "The opposing %s used %s!"
player_pokemon_loses_health = "%s lost %.1f%% of its health!"
opposing_pokemon_loses_health = "The opposing %s lost %.1f%% of its health!"
player_pokemon_uses_leftovers = "%s restored a little HP using its Leftovers!"
opposing_pokemon_uses_leftovers = "The opposing %s restored a little HP using its Leftovers!"
metadata = {'render.modes': ['human']}
def __init__(self, prices, bars_count=DEFAULT_BARS_COUNT,
commission=DEFAULT_COMMISSION_PERC, reset_on_close=True, state_1d=False,
random_ofs_on_reset=True, reward_on_close=False, volumes=False):
assert isinstance(prices, dict)
self._prices = prices
if state_1d:
self._state = State1D(bars_count, commission, reset_on_close, reward_on_close=reward_on_close,
volumes=volumes)
else:
self._state = State(bars_count, commission, reset_on_close, reward_on_close=reward_on_close,
volumes=volumes)
self.action_space = gym.spaces.Discrete(n=len(Actions))
self.observation_space = gym.spaces.Box(low=-np.inf, high=np.inf, shape=self._state.shape, dtype=np.float32)
self.random_ofs_on_reset = random_ofs_on_reset
self.seed()
def reset(self):
player_selected_pokemon_text = "Go! Wynaut!"
agent_selected_pokemon_text = "CrashinBoomBang sent out Landorus!""
# make selection of the instrument and it's offset. Then reset the state
self._instrument = self.np_random.choice(list(self._prices.keys()))
prices = self._prices[self._instrument]
bars = self._state.bars_count
if self.random_ofs_on_reset:
offset = self.np_random.choice(prices.high.shape[0]-bars*10) + bars
else:
offset = bars
self._state.reset(prices, offset)
return self._state.encode()
def step(self, action_idx):
action = Actions(action_idx)
reward, done = self._state.step(action)
obs = self._state.encode()
info = {"instrument": self._instrument, "offset": self._state._offset}
return obs, reward, done, info
def render(self, mode='human', close=False):
pass
def close(self):
pass
def seed(self, seed=None):
self.np_random, seed1 = seeding.np_random(seed)
seed2 = seeding.hash_seed(seed1 + 1) % 2 ** 31
return [seed1, seed2]
@classmethod
def from_dir(cls, data_dir, **kwargs):
prices = {file: data.load_relative(file) for file in data.price_files(data_dir)}
return StocksEnv(prices, **kwargs)