-
Notifications
You must be signed in to change notification settings - Fork 126
/
test_trading.py
executable file
·329 lines (280 loc) · 11.8 KB
/
test_trading.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
# -*- coding: utf-8 -*-
CUDA_VISIBLE_DEVICES = -1
import os
import pandas as pd
from multiprocessing import Pool
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import scipy
import matplotlib as mpl
mpl.use('Agg')
import pybacktest as pb
import matplotlib.pyplot as plt
import threading
import multiprocessing
import tensorflow as tf
import tensorflow.contrib.slim as slim
import scipy.signal
from random import choice
from time import sleep
from time import time
import sys
from trader_gym import environment
from A3C_class import *
# %load_ext ipycache
max_episode_length = 300
import matplotlib
matplotlib.rcParams.update({'font.size': 21})
from configs import postfix, test_postfix, load_model, LR, dep, test_postfix_real, postfix_real
data_dir_path = '/mnt/a3c_data/'
# data
print(data_dir_path + str(test_postfix))
R = pd.read_pickle(data_dir_path + str(test_postfix))
R = R[False == R.index.duplicated()]
test_R = pd.read_pickle(data_dir_path + str(test_postfix_real))
test_R = test_R[False == test_R.index.duplicated()]
# print (R.mean()[0], R.max()[0], (R.max()[0] - R.min()[0]), (R.max()[0] - R.mean()[0])/(R.max()[0] - R.min()[0]), (R.min()[0] - R.mean()[0])/(R.max()[0] - R.min()[0]))
R = (R - R.mean()) / (R.max() - R.min())
old_R = R.copy()
vals = R.values
D = np.hstack([vals[i:-dep + i - 1, :] for i in range(dep, 0, -1)])
R = pd.DataFrame(D, R[dep:-1].index)
# R = R[:10000]
# Main
def run_trades(max_episode_length, gamma, s_size, a_size, load_model, model_path, length, env):
tf.reset_default_graph()
with tf.device('/cpu:0'):
global_episodes = tf.Variable(0, dtype=tf.int32, name='global_episodes', trainable=False)
trainer = tf.train.RMSPropOptimizer(learning_rate=1e-3, decay=0.99, epsilon=1e-6)
master_network = AC_Network(s_size, a_size, 'global', None)
num_workers = 1
workers = []
for i in range(num_workers):
workers.append(Test_Worker(env, i, s_size, a_size, trainer, model_path, global_episodes))
saver = tf.train.Saver(max_to_keep=5)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0)
if not os.path.exists(model_path):
os.makedirs(model_path)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=False, log_device_placement=False, gpu_options=gpu_options)) as sess:
coord = tf.train.Coordinator()
if load_model:
print('Loading Model...')
ckpt = tf.train.get_checkpoint_state(model_path)
saver.restore(sess, ckpt.model_checkpoint_path)
else:
sess.run(tf.global_variables_initializer())
summary_writer = tf.summary.FileWriter('tb/train', graph=sess.graph)
worker_threads = []
for worker in workers:
def worker_work(): return worker.work(max_episode_length, gamma, sess, coord, saver, dep, 0.33)
t = threading.Thread(target=(worker_work))
t.start()
worker_threads.append(t)
coord.join(worker_threads)
acts = workers[0].acts
rews = workers[0].rewards
return acts, rews
# Торговая система - покупаем/продаем в зависимости от предсказанного класса
def backtest(acts, o_R):
pacts = np.asarray(acts).copy()
for i in range(1, pacts.shape[0]):
if pacts[i] == 0:
if pacts[i - 1] == -1:
# from short
pacts[i] = 0.5
elif pacts[i - 1] == 1:
pacts[i] = -0.5
# plt.plot(pacts, '.')
data = o_R[dep - 1:-3]
buy = cover = pd.Series(pacts == 1, index=data.index)
short = sell = pd.Series(pacts == -1, index=data.index)
cover = pd.Series(pacts > 0, index=data.index)
sell = pd.Series(pacts < 0, index=data.index)
# print(np.sum(buy-sell))
OHLC = data[[('DealPrice', 'close'), ('DealPrice', 'close'), ('DealPrice', 'close'), ('DealPrice', 'close')]]
OHLC.columns = ['O', 'H', 'L', 'C']
# Основной график эквити
bt = pb.Backtest(locals())
return bt
load_model = True
model_path = 'model'
length = R.shape[0]
noise_level = 1e-5
price = R.values[:, 3]
n_noises = 1
n_runs = 1
test_acts = np.zeros((n_noises, n_runs, length - 1))
test_rews = np.zeros((n_noises, n_runs, length - 1))
for j in range(n_noises):
np.random.seed(j)
env = environment(R + (np.random.rand(R.shape[0], R.shape[1]) - 0.5) * (1e-5 * (0**j)), length)
for i in range(n_runs):
np.random.seed(i + 1377)
test_acts[j, i, :], test_rews[j, i, :] = run_trades(
max_episode_length, gamma, s_size, a_size, load_model, model_path, length, env)
print(j, i)
# ПРИ НЕУВЕРЕННОСТИ НУЖНО ДЕЛАТЬ ПРЕДЫДУЩЕЕ ДЕЙСТВИЕ, А НЕ 0
test_acts_probs = np.zeros((n_noises, a_size, length - 1))
tresh = 0.0
acts = np.zeros((n_noises, length - 1))
test_acts = test_acts.astype(int)
for k in range(n_noises):
for j in range(n_runs):
for i in range(length - 1):
a = test_acts[k, j, i]
test_acts_probs[k, a + 1, i] += 1
test_acts_probs[k, :, :] = test_acts_probs[k, :, :] / n_runs
acts[k, :] = [np.argmax(x) - 1 if max(x) > tresh else 0 for x in test_acts_probs[k, :, :].T]
# print(test_acts_probs)
# plt.plot(acts.T)
# plt.show()
def write_report(r, filename):
with open(filename, "a") as input_file:
for k, v in r.items():
line = '{}, {}'.format(k, v)
print(line, file=input_file)
folder = os.path.relpath(".", "..")
for i in range(2):
# [ R.index.get_loc('2015-12-29 10:06:00')[0], R.index.get_loc('2016-01-15 10:06:00')[0], \
lengths = [R.index.get_loc('2016-03-15 10:06:00')[0], R.index.get_loc('2016-06-15 10:06:00')[0]]
#names = ['2 weeks', '1 month', '3 month', '6 months']
names = ['3 month', '6 months']
bt = backtest(acts[-1, :lengths[i]], test_R[:lengths[i] + dep + 2])
fig = plt.figure(figsize=(12, 10))
plt.ylim(60000, 140000)
bt.plot_trades()
plt.legend(['long enter', 'short enter', 'long exit', 'short exit', 'equity', 'price'], loc='best')
plt.title(folder + ' test on ' + names[i], fontname="Times New Roman")
plt.ylabel('Rubles', fontname="Times New Roman")
plt.xlabel('Time', fontname="Liberation Serif")
plt.plot()
# plt.show()
plt.savefig('../new_plots/' + folder + '_' + names[i] + '.pdf', bbox_inches='tight', format='pdf')
# Отчет по системе
B = bt.report
write_report(B, '../new_plots/' + folder + '_.txt')
# data
R = pd.read_pickle(data_dir_path + str(postfix))
R = R[False == R.index.duplicated()]
test_R = pd.read_pickle(data_dir_path + str(postfix_real))
test_R = test_R[False == test_R.index.duplicated()]
# print (R.mean()[0], R.max()[0], (R.max()[0] - R.min()[0]), (R.max()[0] - R.mean()[0])/(R.max()[0] - R.min()[0]), (R.min()[0] - R.mean()[0])/(R.max()[0] - R.min()[0]))
R = (R - R.mean()) / (R.max() - R.min())
old_R = R.copy()
vals = R.values
D = np.hstack([vals[i:-dep + i - 1, :] for i in range(dep, 0, -1)])
R = pd.DataFrame(D, R[dep:-1].index)
# R = R[:10000]
# Main
def run_trades(max_episode_length, gamma, s_size, a_size, load_model, model_path, length, env):
tf.reset_default_graph()
with tf.device('/cpu:0'):
global_episodes = tf.Variable(0, dtype=tf.int32, name='global_episodes', trainable=False)
trainer = tf.train.RMSPropOptimizer(learning_rate=1e-3, decay=0.99, epsilon=1e-6)
master_network = AC_Network(s_size, a_size, 'global', None)
num_workers = 1
workers = []
for i in range(num_workers):
workers.append(Test_Worker(env, i, s_size, a_size, trainer, model_path, global_episodes))
saver = tf.train.Saver(max_to_keep=5)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0)
if not os.path.exists(model_path):
os.makedirs(model_path)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=False, log_device_placement=False, gpu_options=gpu_options)) as sess:
coord = tf.train.Coordinator()
if load_model:
print('Loading Model...')
ckpt = tf.train.get_checkpoint_state(model_path)
saver.restore(sess, ckpt.model_checkpoint_path)
else:
sess.run(tf.global_variables_initializer())
summary_writer = tf.summary.FileWriter('tb/train', graph=sess.graph)
worker_threads = []
for worker in workers:
def worker_work(): return worker.work(max_episode_length, gamma, sess, coord, saver, dep, 0.33)
t = threading.Thread(target=(worker_work))
t.start()
worker_threads.append(t)
coord.join(worker_threads)
acts = workers[0].acts
rews = workers[0].rewards
return acts, rews
# Торговая система - покупаем/продаем в зависимости от предсказанного класса
def backtest(acts, o_R):
pacts = np.asarray(acts).copy()
for i in range(1, pacts.shape[0]):
if pacts[i] == 0:
if pacts[i - 1] == -1:
# from short
pacts[i] = 0.5
elif pacts[i - 1] == 1:
pacts[i] = -0.5
# plt.plot(pacts, '.')
data = o_R[dep - 1:-3]
buy = cover = pd.Series(pacts == 1, index=data.index)
short = sell = pd.Series(pacts == -1, index=data.index)
cover = pd.Series(pacts > 0, index=data.index)
sell = pd.Series(pacts < 0, index=data.index)
# print(np.sum(buy-sell))
OHLC = data[[('DealPrice', 'close'), ('DealPrice', 'close'), ('DealPrice', 'close'), ('DealPrice', 'close')]]
OHLC.columns = ['O', 'H', 'L', 'C']
# Основной график эквити
bt = pb.Backtest(locals())
return bt
load_model = True
model_path = 'model'
length = R.shape[0]
noise_level = 1e-5
price = R.values[:, 3]
n_noises = 1
n_runs = 1
test_acts = np.zeros((n_noises, n_runs, length - 1))
test_rews = np.zeros((n_noises, n_runs, length - 1))
for j in range(n_noises):
np.random.seed(j)
env = environment(R + (np.random.rand(R.shape[0], R.shape[1]) - 0.5) * (1e-5 * (0**j)), length)
for i in range(n_runs):
np.random.seed(i + 1377)
test_acts[j, i, :], test_rews[j, i, :] = run_trades(
max_episode_length, gamma, s_size, a_size, load_model, model_path, length, env)
print(j, i)
# ПРИ НЕУВЕРЕННОСТИ НУЖНО ДЕЛАТЬ ПРЕДЫДУЩЕЕ ДЕЙСТВИЕ, А НЕ 0
test_acts_probs = np.zeros((n_noises, a_size, length - 1))
tresh = 0.0
acts = np.zeros((n_noises, length - 1))
test_acts = test_acts.astype(int)
for k in range(n_noises):
for j in range(n_runs):
for i in range(length - 1):
a = test_acts[k, j, i]
test_acts_probs[k, a + 1, i] += 1
test_acts_probs[k, :, :] = test_acts_probs[k, :, :] / n_runs
acts[k, :] = [np.argmax(x) - 1 if max(x) > tresh else 0 for x in test_acts_probs[k, :, :].T]
# print(test_acts_probs)
# plt.plot(acts.T)
# plt.show()
def write_report(r, filename):
with open(filename, "a") as input_file:
for k, v in r.items():
line = '{}, {}'.format(k, v)
print(line, file=input_file)
folder = os.path.relpath(".", "..")
for i in range(1):
lengths = [R.values.shape[0]]
names = [' train on 3 months']
bt = backtest(acts[-1, :lengths[i]], test_R[:lengths[i] + dep + 2])
fig = plt.figure(figsize=(12, 10))
plt.ylim(60000, 1400000)
bt.plot_trades()
plt.legend(['long enter', 'short enter', 'long exit', 'short exit', 'equity', 'price'], loc='best')
plt.title(folder + names[i], fontname="Times New Roman")
plt.ylabel('Rubles', fontname="Times New Roman")
plt.xlabel('Time', fontname="Times New Roman")
plt.yscale('log')
plt.plot()
# plt.show()
plt.savefig('../new_plots/' + folder + '_' + names[i] + '.pdf', bbox_inches='tight', format='pdf')
# Отчет по системе
B = bt.report
write_report(B, '../new_plots/' + folder + '_train.txt')