-
Notifications
You must be signed in to change notification settings - Fork 47
/
train_lstm_v2.py
140 lines (113 loc) · 5.99 KB
/
train_lstm_v2.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
"""
Training lstm v2:
using model to allocate funds, i.e. maximizing return without target labels.
"""
import numpy as np
import pylab as pl
import tensorflow as tf
from helpers.utils import extract_timeseries_from_oanda_data, train_test_validation_split
from helpers.utils import remove_nan_rows, get_data_batch, get_lstm_input_output
from models import lstm_nn
from helpers.get_features import get_features, min_max_scaling
# other-params
np.set_printoptions(linewidth=75*3+5, edgeitems=6)
pl.rcParams.update({'font.size': 6})
# hyper-params
batch_size = 10024
learning_rate = 0.05
drop_keep_prob = 0.7
value_moving_average = 50
split = (0.7, 0.2, 0.1)
plotting = False
saving = False
time_steps = 6
# load data
oanda_data = np.load('data\\EUR_USD_H1.npy') # [-50000:]
price_data_raw = extract_timeseries_from_oanda_data(oanda_data, ['closeMid'])
input_data_raw, input_data_dummy = get_features(oanda_data)
price_data_raw = np.concatenate([[[0]],
(price_data_raw[1:] - price_data_raw[:-1]) / (price_data_raw[1:] + 1e-10)], axis=0)
# prepare data
input_data, price_data, input_data_dummy = remove_nan_rows([input_data_raw, price_data_raw, input_data_dummy])
input_data_scaled_no_dummies = (input_data - min_max_scaling[1, :]) / (min_max_scaling[0, :] - min_max_scaling[1, :])
input_data_scaled = np.concatenate([input_data_scaled_no_dummies, input_data_dummy], axis=1)
input_data_lstm, _ = get_lstm_input_output(input_data_scaled, np.zeros_like(input_data), time_steps=time_steps)
price_data = price_data[-len(input_data_lstm):]
# split to train,test and cross validation
input_train, input_test, input_cv, price_train, price_test, price_cv = \
train_test_validation_split([input_data_lstm, price_data], split=split)
# get dims
_, _, input_dim = np.shape(input_train)
# forward-propagation
x, y, logits, y_, learning_r, drop_out = lstm_nn(input_dim, 3, time_steps=time_steps, n_hidden=[3])
# tf cost and optimizer
price_h = tf.placeholder(tf.float32, [None, 1])
signals = tf.constant([[1., -1., -1e-10]])
cost = (tf.reduce_mean(y_ * signals * price_h * 100)) # profit function
train_step = tf.train.AdamOptimizer(learning_r).minimize(-cost)
# init session
cost_hist_train, cost_hist_test, value_hist_train, value_hist_test, value_hist_cv, value_hist_train_ma, \
value_hist_test_ma, value_hist_cv_ma, step, step_hist, saving_score = [], [], [], [], [], [], [], [], 0, [], 0.05
saver = tf.train.Saver()
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# train
while True:
if step == 30000:
break
# train model
x_train, price_batch = get_data_batch([input_train[:-1], price_train[1:]], batch_size, sequential=False)
_, cost_train = sess.run([train_step, cost],
feed_dict={x: x_train, price_h: price_batch,
learning_r: learning_rate, drop_out: drop_keep_prob})
# keep track of stuff
step += 1
if step % 100 == 0 or step == 1:
# get y_ predictions
y_train_pred = sess.run(y_, feed_dict={x: input_train, drop_out: drop_keep_prob})
y_test_pred, cost_test = sess.run([y_, cost], feed_dict={x: input_test[:-1], price_h: price_test[1:], drop_out: drop_keep_prob})
y_cv_pred = sess.run(y_, feed_dict={x: input_cv, drop_out: drop_keep_prob})
# get portfolio value
value_train = 1 + np.cumsum(np.sum(y_train_pred[:-1] * [1., -1., 0.] * price_train[1:], axis=1))
value_test = 1 + np.cumsum(np.sum(y_test_pred * [1., -1., 0.] * price_test[1:], axis=1))
value_cv = 1 + np.cumsum(np.sum(y_cv_pred[:-1] * [1., -1., 0.] * price_cv[1:], axis=1))
# save history
step_hist.append(step)
cost_hist_train.append(cost_train)
cost_hist_test.append(cost_test)
value_hist_train.append(value_train[-1])
value_hist_test.append(value_test[-1])
value_hist_cv.append(value_cv[-1])
value_hist_train_ma.append(np.mean(value_hist_train[-value_moving_average:]))
value_hist_test_ma.append(np.mean(value_hist_test[-value_moving_average:]))
value_hist_cv_ma.append(np.mean(value_hist_cv[-value_moving_average:]))
print('Step {}: train {:.4f}, test {:.4f}'.format(step, cost_train, cost_test))
if plotting:
pl.figure(1, figsize=(3, 7), dpi=80, facecolor='w', edgecolor='k')
pl.subplot(211)
pl.title('Objective function')
pl.plot(step_hist, cost_hist_train, color='darkorange', linewidth=0.3)
pl.plot(step_hist, cost_hist_test, color='dodgerblue', linewidth=0.3)
pl.subplot(212)
pl.title('Portfolio value')
pl.plot(step_hist, value_hist_train, color='darkorange', linewidth=0.3)
pl.plot(step_hist, value_hist_test, color='dodgerblue', linewidth=0.3)
pl.plot(step_hist, value_hist_cv, color='magenta', linewidth=1)
pl.plot(step_hist, value_hist_train_ma, color='tomato', linewidth=1.5)
pl.plot(step_hist, value_hist_test_ma, color='royalblue', linewidth=1.5)
pl.plot(step_hist, value_hist_cv_ma, color='black', linewidth=1.5)
pl.pause(1e-10)
# save if some complicated rules
if saving:
current_score = 0 if value_test[-1] < 0.01 or value_cv[-1] < 0.01 \
else np.average([value_test[-1], value_cv[-1]])
saving_score = current_score if saving_score < current_score else saving_score
if saving_score == current_score and saving_score > 0.05:
saver.save(sess, 'saved_models/lstm-v2-avg_score{:.3f}'.format(current_score), global_step=step)
print('Model saved. Average score: {:.2f}'.format(current_score))
pl.figure(2)
pl.plot(value_train, linewidth=1)
pl.plot(value_test, linewidth=1)
pl.plot(value_cv, linewidth=1)
pl.pause(1e-10)