-
Notifications
You must be signed in to change notification settings - Fork 482
/
Copy pathtrading_env.py
210 lines (155 loc) · 5.98 KB
/
trading_env.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
from time import time
from enum import Enum
import numpy as np
import matplotlib.pyplot as plt
import gymnasium as gym
class Actions(Enum):
Sell = 0
Buy = 1
class Positions(Enum):
Short = 0
Long = 1
def opposite(self):
return Positions.Short if self == Positions.Long else Positions.Long
class TradingEnv(gym.Env):
metadata = {'render_modes': ['human'], 'render_fps': 3}
def __init__(self, df, window_size, render_mode=None):
assert df.ndim == 2
assert render_mode is None or render_mode in self.metadata['render_modes']
self.render_mode = render_mode
self.df = df
self.window_size = window_size
self.prices, self.signal_features = self._process_data()
self.shape = (window_size, self.signal_features.shape[1])
# spaces
self.action_space = gym.spaces.Discrete(len(Actions))
INF = 1e10
self.observation_space = gym.spaces.Box(
low=-INF, high=INF, shape=self.shape, dtype=np.float32,
)
# episode
self._start_tick = self.window_size
self._end_tick = len(self.prices) - 1
self._truncated = None
self._current_tick = None
self._last_trade_tick = None
self._position = None
self._position_history = None
self._total_reward = None
self._total_profit = None
self._first_rendering = None
self.history = None
def reset(self, seed=None, options=None):
super().reset(seed=seed, options=options)
self.action_space.seed(int((self.np_random.uniform(0, seed if seed is not None else 1))))
self._truncated = False
self._current_tick = self._start_tick
self._last_trade_tick = self._current_tick - 1
self._position = Positions.Short
self._position_history = (self.window_size * [None]) + [self._position]
self._total_reward = 0.
self._total_profit = 1. # unit
self._first_rendering = True
self.history = {}
observation = self._get_observation()
info = self._get_info()
if self.render_mode == 'human':
self._render_frame()
return observation, info
def step(self, action):
self._truncated = False
self._current_tick += 1
if self._current_tick == self._end_tick:
self._truncated = True
step_reward = self._calculate_reward(action)
self._total_reward += step_reward
self._update_profit(action)
trade = False
if (
(action == Actions.Buy.value and self._position == Positions.Short) or
(action == Actions.Sell.value and self._position == Positions.Long)
):
trade = True
if trade:
self._position = self._position.opposite()
self._last_trade_tick = self._current_tick
self._position_history.append(self._position)
observation = self._get_observation()
info = self._get_info()
self._update_history(info)
if self.render_mode == 'human':
self._render_frame()
return observation, step_reward, False, self._truncated, info
def _get_info(self):
return dict(
total_reward=self._total_reward,
total_profit=self._total_profit,
position=self._position
)
def _get_observation(self):
return self.signal_features[(self._current_tick-self.window_size+1):self._current_tick+1]
def _update_history(self, info):
if not self.history:
self.history = {key: [] for key in info.keys()}
for key, value in info.items():
self.history[key].append(value)
def _render_frame(self):
self.render()
def render(self, mode='human'):
def _plot_position(position, tick):
color = None
if position == Positions.Short:
color = 'red'
elif position == Positions.Long:
color = 'green'
if color:
plt.scatter(tick, self.prices[tick], color=color)
start_time = time()
if self._first_rendering:
self._first_rendering = False
plt.cla()
plt.plot(self.prices)
start_position = self._position_history[self._start_tick]
_plot_position(start_position, self._start_tick)
_plot_position(self._position, self._current_tick)
plt.suptitle(
"Total Reward: %.6f" % self._total_reward + ' ~ ' +
"Total Profit: %.6f" % self._total_profit
)
end_time = time()
process_time = end_time - start_time
pause_time = (1 / self.metadata['render_fps']) - process_time
assert pause_time > 0., "High FPS! Try to reduce the 'render_fps' value."
plt.pause(pause_time)
def render_all(self, title=None):
window_ticks = np.arange(len(self._position_history))
plt.plot(self.prices)
short_ticks = []
long_ticks = []
for i, tick in enumerate(window_ticks):
if self._position_history[i] == Positions.Short:
short_ticks.append(tick)
elif self._position_history[i] == Positions.Long:
long_ticks.append(tick)
plt.plot(short_ticks, self.prices[short_ticks], 'ro')
plt.plot(long_ticks, self.prices[long_ticks], 'go')
if title:
plt.title(title)
plt.suptitle(
"Total Reward: %.6f" % self._total_reward + ' ~ ' +
"Total Profit: %.6f" % self._total_profit
)
def close(self):
plt.close()
def save_rendering(self, filepath):
plt.savefig(filepath)
def pause_rendering(self):
plt.show()
def _process_data(self):
raise NotImplementedError
def _calculate_reward(self, action):
raise NotImplementedError
def _update_profit(self, action):
raise NotImplementedError
def max_possible_profit(self): # trade fees are ignored
raise NotImplementedError