-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeep_rl_portfolio.py
496 lines (420 loc) · 15.5 KB
/
deep_rl_portfolio.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import sys
from datetime import datetime, timedelta
import time
import argparse
from pprint import pprint
from src.train_rl_algorithm import train_rl_algorithm
from src.test_rl_algorithm import test_rl_algorithm
from src.trading_environment import TradingEnvironment
from src.params import PF_INITIAL_VALUE, TRADING_COST, INTEREST_RATE, WINDOW_LENGTH
from visualization_scripts.plot_train_results import plot_train_results
from visualization_scripts.make_train_histograms import make_train_histograms
from data_pipelines import get_crypto_price_tensors
DEFAULT_TRADE_ENV_ARGS = {
"window_length": WINDOW_LENGTH,
"portfolio_value": PF_INITIAL_VALUE,
"trading_cost": TRADING_COST,
}
TRAIN_BASE_PARAMS = {
"interactive_session": False,
"verbose": False,
"no_of_assets": 11,
"plot_results": False,
"n_episodes": 3,
"n_batches": 20,
"window_length": 70,
"batch_size": 50,
"portfolio_value": 1,
"ratio_val": 0,
"max_pf_weight_penalty": 0.5,
}
def main(**train_configs):
print("\nStarting training process with the following options:")
pprint(train_configs)
start_time = time.time()
print("\n")
# Creation of the trading environment
trade_envs, asset_list, train_test_val_steps = _initialize_trade_envs(train_configs)
# Agent training
agent, state_fu, done_fu, train_performance_lists = train_rl_algorithm(
train_configs, trade_envs, train_test_val_steps
)
# Agent evaluation
test_performance_lists = test_rl_algorithm(
train_configs, agent, state_fu, done_fu, trade_envs, train_test_val_steps
)
end_time = time.time()
train_time_secs = round(end_time - start_time, 1)
print("\nTraining completed")
print(f"Process took {train_time_secs} seconds")
plot_train_results(
train_configs,
train_performance_lists,
test_performance_lists,
asset_list,
train_time_secs,
train_test_val_steps,
)
print("\nAggregating simulation statistics...")
make_train_histograms(train_configs["train_session_name"])
pprint("Exiting")
def _initialize_trade_envs(train_configs):
dataset, asset_names, ratio_train = get_crypto_price_tensors.main(
no_of_cryptos=train_configs["no_of_assets"],
start_date=train_configs["start_date"],
test_start_date=train_configs["test_start_date"],
end_date=train_configs["end_date"],
trading_period_length=train_configs["trading_period_length"],
train_session_name=train_configs["train_session_name"],
)
trade_env_args = DEFAULT_TRADE_ENV_ARGS
trade_env_args["train_size"] = ratio_train
trade_env_args["data"] = dataset
trade_env_args["window_length"] = train_configs["window_length"]
trading_periods = dataset.shape[2]
print("Trading periods: {}".format(dataset.shape[2]))
# Determine the step sizes of different datasets
train_test_val_steps = _get_train_val_test_steps(
trading_periods, train_configs, ratio_train
)
print("Starting training for {} assets".format(len(asset_names)))
print(asset_names)
train_envs = _get_train_environments(train_configs["no_of_assets"], trade_env_args)
return train_envs, asset_names, train_test_val_steps
def _get_train_environments(no_of_assets, trade_env_args):
# environment for trading of the agent
# this is the agent trading environment (policy network agent)
env = TradingEnvironment(**trade_env_args)
# environment for trading of the agent
# that just does the first cash distribution and then holds eq
env_first_action = TradingEnvironment(**trade_env_args)
# environment for equally weighted
# this environment is set up for an agent who only plays an equally weithed
# portfolio (baseline)
env_eq = TradingEnvironment(**trade_env_args)
# environment secured (only money)
# this environment is set up for an agent who plays secure, keeps its money
env_s = TradingEnvironment(**trade_env_args)
# full on one stock environment
# these environments are set up for agents who play only on one stock
env_fu = [TradingEnvironment(**trade_env_args) for asset in range(no_of_assets)]
trade_envs = {
"policy_network": env,
"policy_network_first_step_only": env_first_action,
"equal_weighted": env_eq,
"only_cash": env_s,
"full_on_one_stocks": env_fu,
"args": trade_env_args,
}
return trade_envs
def _get_train_val_test_steps(trading_period, train_configs, ratio_train):
# Total number of steps for pre-training in the training set
total_steps_train = int(ratio_train * trading_period) + 1
# Total number of steps for pre-training in the validation set
total_steps_val = int(train_configs["ratio_val"] * trading_period)
# Total number of steps for the test
total_steps_test = trading_period - total_steps_train - total_steps_val
train_test_val_steps = {
"train": total_steps_train,
"test": total_steps_test,
"validation": total_steps_val,
}
test_start_idx = train_test_val_steps["train"] + train_test_val_steps["validation"]
print(
f"Test will start from idx: {train_test_val_steps['train'] + train_test_val_steps['validation']}"
)
return train_test_val_steps
def _calculate_start_date(end_date, trading_period_length):
if trading_period_length in ["2h", "4h", "1d", "30min"]:
start_date = (datetime.strptime(end_date, "%Y%m%d") - timedelta(599)).strftime(
"%Y%m%d"
)
elif trading_period_length == "15min":
start_date = (datetime.strptime(end_date, "%Y%m%d") - timedelta(299)).strftime(
"%Y%m%d"
)
elif trading_period_length == "5min":
start_date = (datetime.strptime(end_date, "%Y%m%d") - timedelta(99)).strftime(
"%Y%m%d"
)
return start_date
if __name__ == "__main__":
PARSER = argparse.ArgumentParser()
PARSER.add_argument(
"-pr",
"--plot_results",
action="store_true",
help="Plot aftermath analysis",
default=False,
)
PARSER.add_argument(
"-i",
"--interactive_session",
action="store_true",
help="Plot interactively with matplotlib",
default=False,
)
PARSER.add_argument(
"-g", "--gpu_device", type=int, help="Choose GPU device number", default=None
)
PARSER.add_argument(
"-na",
"--no_of_assets",
type=int,
help="Choose how many assets are trained",
default=5,
)
PARSER.add_argument(
"-nb",
"--no_of_batches",
type=int,
help="Choose how many batches are trained",
default=10,
)
PARSER.add_argument(
"-rt",
"--ratio_train",
type=float,
help="Proportional size of train set",
default=0.95,
)
PARSER.add_argument(
"-rv",
"--ratio_val",
type=float,
help="Proportional size of val set",
default=0.0,
)
PARSER.add_argument(
"-bs", "--batch_size", type=int, help="Select batch size", default=50
)
PARSER.add_argument(
"-ne",
"--no_of_episodes",
type=int,
help="Choose how many episodes are trained",
default=2,
)
PARSER.add_argument(
"-wl", "--window_length", type=int, help="Choose window length", default=40
)
PARSER.add_argument(
"-pv",
"--portfolio_initial_value",
type=int,
help="Initial cash invested in portfolio",
default=100,
)
PARSER.add_argument(
"-v",
"--verbose",
help="Print train vectors",
default=False,
action="store_true",
)
PARSER.add_argument(
"-t", "--test_mode", help="Proper testrun", default=False, action="store_true"
)
PARSER.add_argument(
"-qt",
"--quick_test_mode",
help="Quick testrun",
default=False,
action="store_true",
)
PARSER.add_argument(
"-sd",
"--start_date",
type=str,
default="20170601",
help="date in format YYYYMMDD",
)
PARSER.add_argument(
"-ed",
"--end_date",
type=str,
default="20171231",
help="date in format YYYYMMDD",
)
PARSER.add_argument(
"-pl",
"--trading_period_length",
type=str,
default="1d",
help="Trade period length (5min, 15min, 30min, 2h, 4h, 1d)",
)
PARSER.add_argument(
"-cbts", "--calm_before_the_storm", default=False, action="store_true", help="Runs the 'calm_before_the_storm' from my thesis"
)
PARSER.add_argument("-awake", "--awakening", default=False, action="store_true", help="Runs the 'awakening' from my thesis")
PARSER.add_argument("-xrp", "--ripple_bull_run", default=False, action="store_true", help="Runs the 'ripple_bull_run' from my thesis")
PARSER.add_argument("-eth", "--ethereum_valley", default=False, action="store_true", help="Runs the 'ethereum_valley' from my thesis")
PARSER.add_argument("-ath", "--all_time_high", default=False, action="store_true", help="Runs the 'all_time_high' from my thesis")
PARSER.add_argument("-rock", "--rock_bottom", default=False, action="store_true", help="Runs the 'rock_bottom' from my thesis")
PARSER.add_argument("-recent", "--recent", default=False, action="store_true", help="Runs the 'recent' from my thesis")
ARGS = PARSER.parse_args()
if ARGS.verbose:
print("\nVerbose session. Alot of vectors will be printed below.\n")
if ARGS.quick_test_mode:
print("\nStarting rapid test run...")
main(
interactive_session=False,
gpu_device=None,
verbose=True,
no_of_assets=5,
plot_results=False,
n_episodes=1,
n_batches=1,
ratio_train=ARGS.ratio_train,
ratio_val=ARGS.ratio_val,
window_length=130,
batch_size=1,
portfolio_value=100,
start_date="20190101",
test_start_date="20190201",
end_date="20190301",
trading_period_length="4h",
max_pf_weight_penalty=0.7,
test_mode=True,
train_session_name="quick_test_run_with_long_name",
)
elif ARGS.test_mode:
print("\nStarting proper test run...")
main(
interactive_session=False,
gpu_device=None,
verbose=True,
no_of_assets=7,
plot_results=False,
ratio_train=ARGS.ratio_train,
ratio_val=ARGS.ratio_val,
n_episodes=1,
n_batches=1,
window_length=77,
batch_size=1,
portfolio_value=100,
start_date="20190101",
test_start_date="20190201",
end_date="20190301",
trading_period_length="2h",
max_pf_weight_penalty=0.7,
test_mode=True,
train_session_name="test_run_with_long_name",
)
elif ARGS.calm_before_the_storm:
print("\nRunning model: Calm_before_the_storm")
end_date = "20161028"
start_date = _calculate_start_date(end_date, ARGS.trading_period_length)
main(
**TRAIN_BASE_PARAMS,
start_date=start_date,
test_start_date="20160907",
end_date=end_date,
trading_period_length=ARGS.trading_period_length,
train_session_name="Calm_before_the_storm_{}".format(
ARGS.trading_period_length
),
gpu_device=ARGS.gpu_device,
)
elif ARGS.awakening:
print("\nRunning model: Awakening")
end_date = "20170128"
start_date = _calculate_start_date(end_date, ARGS.trading_period_length)
main(
**TRAIN_BASE_PARAMS,
start_date=start_date,
end_date=end_date,
test_start_date="20161208",
trading_period_length=ARGS.trading_period_length,
train_session_name="Awakening_{}".format(ARGS.trading_period_length),
gpu_device=ARGS.gpu_device,
)
elif ARGS.ripple_bull_run:
print("\nRunning model: Ripple bull run")
end_date = "20170427"
start_date = _calculate_start_date(end_date, ARGS.trading_period_length)
main(
**TRAIN_BASE_PARAMS,
start_date=start_date,
end_date=end_date,
test_start_date="20170307",
trading_period_length=ARGS.trading_period_length,
train_session_name="Ripple_bull_run_{}".format(ARGS.trading_period_length),
gpu_device=ARGS.gpu_device,
)
elif ARGS.ethereum_valley:
print("\nRunning model ethereum_valley")
end_date = "20170718"
start_date = _calculate_start_date(end_date, ARGS.trading_period_length)
main(
**TRAIN_BASE_PARAMS,
start_date=start_date,
end_date=end_date,
test_start_date="20170528",
trading_period_length=ARGS.trading_period_length,
train_session_name="Ethereum_valley_{}".format(ARGS.trading_period_length),
gpu_device=ARGS.gpu_device,
)
elif ARGS.all_time_high:
print("\nRunning All time high")
end_date = "20180113"
start_date = _calculate_start_date(end_date, ARGS.trading_period_length)
train_params = {**TRAIN_BASE_PARAMS, "max_pf_weight_penalty": 1.1}
main(
**train_params,
start_date=start_date,
end_date=end_date,
test_start_date="20171123",
trading_period_length=ARGS.trading_period_length,
train_session_name="All-time_high_{}".format(ARGS.trading_period_length),
gpu_device=ARGS.gpu_device,
)
elif ARGS.rock_bottom:
print("\nRunning Rock Bottom")
end_date = "20181231"
start_date = _calculate_start_date(end_date, ARGS.trading_period_length)
train_params = {**TRAIN_BASE_PARAMS, "max_pf_weight_penalty": 0.9}
main(
**train_params,
start_date=start_date,
end_date=end_date,
test_start_date="20181110",
trading_period_length=ARGS.trading_period_length,
train_session_name="Rock_bottom_{}".format(ARGS.trading_period_length),
gpu_device=ARGS.gpu_device,
)
elif ARGS.recent:
print("\nRunning recent year 2019")
end_date = "20190426"
start_date = _calculate_start_date(end_date, ARGS.trading_period_length)
main(
**TRAIN_BASE_PARAMS,
start_date=start_date,
end_date=end_date,
test_start_date="20190306",
trading_period_length=ARGS.trading_period_length,
train_session_name="Recent_{}".format(ARGS.trading_period_length),
gpu_device=ARGS.gpu_device,
)
else:
main(
interactive_session=False,
gpu_device=None,
verbose=True,
no_of_assets=7,
plot_results=False,
ratio_train=ARGS.ratio_train,
ratio_val=ARGS.ratio_val,
n_episodes=1,
n_batches=1,
window_length=77,
batch_size=1,
portfolio_value=100,
start_date="20190101",
test_start_date="20190201",
end_date="20190301",
trading_period_length="2h",
max_pf_weight_penalty=0.7,
test_mode=True,
train_session_name="default mode",
)