-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
269 lines (220 loc) · 8.97 KB
/
test.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import argparse
import os
import json
import sys
sys.path.append("../")
from collections import namedtuple
from tensorforce.agents import Agent
import config
from core.utils import PrepData
from core.basic_logger import get_logger
from env.environment import PortfolioEnv
_Defaults = namedtuple('Defaults', [
'data',
'split',
'agent_config',
'net_config',
'episodes',
'horizon',
'action_type',
'action_space',
'num_actions',
'eval_path',
'verbose',
'load_agent',
'basic_agent',
'discrete_states',
'standardize_state'
])
def get_defaults():
try:
return _Defaults(
data=config.ENV_DATA,
split=config.TRAIN_SPLIT,
agent_config=os.path.join(config.AGENT_CONFIG, 'ppo_sb.json'),
net_config=os.path.join(config.NET_CONFIG, 'mlp3.json'),
episodes=config.EPISODES,
horizon=config.HORIZON,
action_type='signal_softmax',
action_space='bounded',
num_actions=41,
eval_path=os.path.join(config.RUN_DIR, 'test'),
verbose=2,
load_agent=os.path.join(config.MODEL_DIR, 'saves', 'PPOAgent'),
basic_agent=None,
discrete_states=True,
standardize_state=False
)
except Exception as e:
print(e)
# in case of problems dealing with the config file or paths
return _Defaults(
data=None,
split=0.75,
agent_config=None,
net_config=None,
episodes=50,
horizon=20,
action_type='signal_softmax',
action_space='bounded',
num_actions=11,
eval_path=None,
verbose=2,
load_agent=None,
basic_agent=None,
discrete_states=False,
standardize_state=True
)
def get_args(default):
# Args: namedtuple with default values
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--data', help="Environment data file",
default=default.data)
parser.add_argument('-sp', '--split', help="Train test split",
default=default.split)
parser.add_argument('-a', '--agent-config', help="Agent configuration file",
default=default.agent_config)
parser.add_argument('-n', '--net-config', help="Network specification file",
default=default.net_config)
parser.add_argument('-at', '--action-type', help="How to change weights",
default=default.action_type)
parser.add_argument('-ap', '--action-space', help="Action space continues or discrete",
default=default.action_space)
parser.add_argument('-na', '--num_actions', help="Number of discrete actions", type=int,
default=default.num_actions)
parser.add_argument('-e', '--episodes', type=int, help="Number of episodes per epoch",
default=default.episodes)
parser.add_argument('-hz', '--horizon', type=int, help="Investment horizon",
default=default.horizon)
parser.add_argument('-ep', '--eval-path', help="Save agent to this dir",
default=default.eval_path)
parser.add_argument('-v', '--verbose', help="Console printing level", type=int,
default=default.verbose)
parser.add_argument('-l', '--load-agent', help="Load agent from this dir",
default=default.load_agent)
parser.add_argument('-ba', '--basic-agent', help="Load basic agent",
default=default.basic_agent)
parser.add_argument('-ds', '--discrete-states', help="Discrete state space true/false",
default=default.discrete_states)
parser.add_argument('-ss', '--standardize-state', help="Standardize or normalize state true/false",
default=default.standardize_state)
return parser.parse_args()
class TestAgent(object):
def __init__(self, info):
self.args = info
if not os.path.isdir("tmp"):
try:
os.mkdir("tmp", 0o755)
except OSError:
raise OSError("Cannot create directory `tmp`")
self.logger = get_logger(filename='tmp/test.log', logger_name='TestLogger')
self.logger.debug(self.args)
self.test, self.scaler = self._get_data()
# build environment
self.environment = PortfolioEnv(
self.test,
nb_assets=config.NB_ASSETS,
horizon=self.args.horizon,
window_size=config.WINDOW_SIZE,
portfolio_value=config.PORTFOLIO_VALUE,
assets=config.ASSETS,
risk_aversion=config.RISK_AVERSION,
scaler=self.scaler,
predictor=config.PREDICTION_MODEL,
cost_buying=config.COST_BUYING,
cost_selling=config.COST_SELLING,
action_type=self.args.action_type,
action_space=self.args.action_space,
optimized=True,
num_actions=self.args.num_actions,
standardize=self.args.standardize_state,
episodes=self.args.episodes,
epochs=1,
random_starts=False
)
# check if there is a valid path for saving the evaluation files
if self.args.eval_path:
save_dir = os.path.dirname(self.args.eval_path)
if not os.path.isdir(save_dir):
try:
os.mkdir(save_dir, 0o755)
except OSError:
raise OSError("Cannot save evaluation to dir {} ()".format(save_dir))
# build agent, either basic or rl agent
if self.args.basic_agent is None:
# load agent config
with open(self.args.agent_config, 'r') as fp:
self.agent_config = json.load(fp=fp)
# load network config
if self.args.net_config:
with open(self.args.net_config, 'r') as fp:
self.network_spec = json.load(fp=fp)
try:
print(f'Agent spec {self.agent_config}'
f'\nNetwork spec {self.network_spec}'
f'\nEnvironment spec: {self.environment.env_spec()}\n')
self.logger.info(f'\nAgent spec: {self.agent_config}'
f'\nNetwork spec: {self.network_spec}'
f'\nEnvironment spec: {self.environment.env_spec()}\n')
except Exception:
pass
self.agent = Agent.from_spec(
spec=self.agent_config,
kwargs=dict(
states_spec=self.environment.states,
actions_spec=self.environment.actions,
network_spec=self.network_spec
)
)
# try to load a pre trained agent
if self.args.load_agent:
load_dir = os.path.dirname(self.args.load_agent)
if not os.path.isdir(load_dir):
raise OSError("Could not load agent from {}: No such directory.".format(load_dir))
self.agent.restore_model(directory=self.args.load_agent)
else:
if self.args.basic_agent == 'random':
from model.basic_agents import RandomActionAgent
self.agent = RandomActionAgent(action_shape=(config.NB_ASSETS,))
else:
from model.basic_agents import BuyAndHoldAgent
self.agent = BuyAndHoldAgent(action_shape=(config.NB_ASSETS,))
def _get_data(self):
prep = PrepData(horizon=self.args.horizon,
window_size=config.WINDOW_SIZE,
nb_assets=config.NB_ASSETS,
split=self.args.split)
data = prep.get_data(self.args.data) # get data
test = data[int(self.args.split * data.shape[0]):] # test data split
scaler = prep.get_scaler(data[0: int(args.split * data.shape[0])])
return test, scaler
def run(self):
try:
from run.runner import Runner
except ModuleNotFoundError:
from runner import Runner
run_test = Runner(
self.agent,
self.environment,
epochs=1,
episodes=self.args.episodes,
horizon=self.args.horizon,
mode='test',
verbose=self.args.verbose,
model_path=None,
seed=92
)
run_test.run()
run_test.close(
save_full=self.args.eval_path + '/full_' + str(self.agent) + '_evaluation.csv',
save_short=self.args.eval_path + '/short_' + str(self.agent) + '_evaluation.csv'
)
if __name__ == '__main__':
# get default parameters
defaults = get_defaults()
# get arguments
# they are equal to the default ones if not given by flags
args = get_args(defaults)
# run test
run = TestAgent(args)
run.run()