-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.py
67 lines (58 loc) · 2.17 KB
/
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
import gym
import numpy as np
from random import choice
from functions import *
class TradingEnv(gym.Env):
def __init__(self, X, drop_column_names=[], window_size=40):
self.nb_actions = 2
self.X = X
self.window_size = window_size
self.position = self.window_size + 3
self.drop_column_names = drop_column_names
# Next state, Reward, Done, _
def step(self, action):
next_state = self.X.loc[self.position +
2 - self.window_size: self.position + 1]
reward = 0
done = False
if self.position >= len(self.X) - 3:
done = True
if action == 0: # BUY
a = self.X.at[self.position, 'Close']
b = self.X.at[self.position + 1, 'Close']
c = self.X.at[self.position + 2, 'Close']
if a > b:
reward += 1
if c > b:
reward += 1
elif a < b:
reward -= 1
if c < b:
reward -= 1
elif action == 1: # SELL
a = self.X.at[self.position, 'Close']
b = self.X.at[self.position + 1, 'Close']
c = self.X.at[self.position + 2, 'Close']
if a < b:
reward += 1
if c < b:
reward += 1
elif a > b:
reward -= 1
if c > b:
reward -= 1
else:
raise Exception("Invalid Action")
self.position += choice([1])
next_state = normalize(next_state)
next_state = drop_columns(next_state, self.drop_column_names)
return next_state.values, reward, done, {"price": self.X.at[self.position, 'Close']}
def reset(self):
self.position = self.window_size + 3
next_state = self.X.loc[self.position +
2 - self.window_size: self.position + 1]
next_state = normalize(next_state)
next_state = drop_columns(next_state, self.drop_column_names)
return next_state.values, 0, False, {"price": self.X.at[self.position, 'Close']}
def render(self, mode='human', close=False):
pass