-
Notifications
You must be signed in to change notification settings - Fork 6
/
tradingnet.py
261 lines (227 loc) · 11.5 KB
/
tradingnet.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
from pickletools import string1
from re import L
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import gym
import time
import pandas as pd
import datetime
import scipy.signal
import time
import LeverageSimMarket
import UnscaledSimMarket
import tensorflow as tf
from gym import spaces
import torch
import stable_baselines3 as baselines
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv, VecNormalize, VecCheckNan, SubprocVecEnv
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.env_checker import check_env
from stable_baselines3.common.callbacks import EvalCallback, CheckpointCallback
from stable_baselines3.common import utils
import itertools
from typing import Callable
def linschedmodified(start: float, end: float, end_fraction: float) -> Callable[[float], float]:
def func(progress_remaining: float) -> float:
if (1 - progress_remaining) > end_fraction:
return end
else:
return start + (1 - progress_remaining) * (end - start) / end_fraction
return func
def getSplit(filename, leftsplit):
"""
Arguments:
string: filename
float: leftsplit, the train percentage. Testdf is 1-leftsplit
Returns:
dataframe: traindf, testdf
"""
dataframe = pd.read_excel(filename)
if dataframe.isnull().values.any():
print('na values exist')
quit()
dataframe = dataframe.drop(['Unnamed: 0'], axis = 1)
traindf = dataframe[:int(len(dataframe)*leftsplit)]
testdf = dataframe[int(len(dataframe)*leftsplit):]
return traindf, testdf
def parameterSearch(traindf):
"""
Arguments:
traindf: dataframe
"""
#testdf = dataframe[int(len(dataframe)*.60):]
parameters = [list(np.linspace(0.01,0.03,2)), [0.001],
list(np.linspace(0.9999,0.95,3)),list(np.linspace(0.001,0.3,4)),
list([32,2048])]
#learningrate
# ent_coef = [np.linspace(0.001, 0.2, 4)]
# gamma = [np.linspace(0.9999,0.95,4)]
# clip = [np.linspace(0.001,0.3,4)]
# batch = [32,64,128,512,2048]
combos = list(itertools.product(*parameters))
i = 0
while i < len(combos):
model_parems = {'data' : traindf,
'timesteps' : 150000,
'learningrate' : combos[i][0],
'ent_coef' : combos[i][1], #0.01
'gamma' : combos[i][2],
'policy' : "MlpPolicy", #MLPPOLICY DEFAULT
'clip' : combos[i][3],
'batch_size' : combos[i][4]
}
values = list(model_parems.values())
string = ""
for key,value in model_parems.items():
if key == 'data':
string += 'minileveragesearch'
continue
string += "_{}{}".format(key,value)
env = LeverageSimMarket.LeverageSimMarket
env = make_vec_env(env, n_envs=1, env_kwargs = {'cash' : 500, 'data' : values[0]}, seed=42)
env = VecCheckNan(env, raise_exception=True)
env = VecNormalize(env, norm_obs=True, norm_reward=True, clip_obs=1., clip_reward=1.)
#checkpoint_callback = CheckpointCallback(save_freq=len(traindf), save_path='./' + str(string) + 'checkpoint/',
# name_prefix='ppo_model')
model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = 'minitesting2', seed=42, learning_rate= values[2], device = "cpu", ent_coef = values[3], gamma=values[4], clip_range=values[6], batch_size = values[7]) #significantly faster on cpu
callback= LeverageSimMarket.TensorboardCallback()
# model = PPO.load("ppohourly60trainnewmse_timesteps5000000_learningrate0.003", env = env, device = "cpu")
model.learn(total_timesteps=values[1], log_interval=1, callback = [callback], tb_log_name =string, reset_num_timesteps= False)
model.save("/minitesting/" + string)
del env
del string
del model
del callback
del model_parems
i += 1
def vecEnvTry(parems, tbfolder, numvecs, learningrate = None, schedlist = None):
parems_ = parems
parems = list(parems.values())
env = LeverageSimMarket.LeverageSimMarket
# check_env(env, warn = True, skip_render_check = True)
env = make_vec_env(env, n_envs=numvecs, env_kwargs = {'cash' : 500, 'data' : parems[0]}, seed=parems[2], vec_env_cls=SubprocVecEnv)
env = VecCheckNan(env, raise_exception=True)
env = VecNormalize(env, norm_obs=True, norm_reward=True, clip_obs=1., clip_reward=1.)
numvecs = 6
string = ""
for key,value in parems_.items():
if key == 'data':
string += 'postfinal'
continue
string += "_{}{}".format(key,value)
if learningrate is float:
string+= 'learningrate' + learningrate
elif isinstance(schedlist, list):
string += 'lrbeg' + str(schedlist[0]) + 'lrend' + str(schedlist[1]) + 'frac' + str(schedlist[2])
learningrate = utils.get_linear_fn(schedlist[0],schedlist[1],schedlist[2])
if learningrate == None and schedlist == None:
'You need to include a learning rate'
quit()
checkpoint_callback = CheckpointCallback(save_freq=len(parems[0]), save_path='./' + str(string) + 'defaultcheckpoint/',
name_prefix='postfinal')
#model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = 'results', seed=42, learning_rate= values[2], device = "cpu", ent_coef = values[3], gamma=values[4], clip_range=values[6], batch_size = values[7]) #significantly faster on cpu
if type(learningrate) is float:
model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = tbfolder, seed=parems[2], learning_rate= learningrate, device = "cpu", gamma=parems[4], clip_range=parems[6], ent_coef=parems[3], batch_size=2048) #significantly faster on cpu
else:
model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = tbfolder, seed=parems[2], learning_rate= learningrate, device = "cpu", gamma=parems[4], clip_range=parems[6], ent_coef=parems[3]) #significantly faster on cpu
callback= LeverageSimMarket.TensorboardCallback()
# model = PPO.load("ppohourly60trainnewmse_timesteps5000000_learningrate0.003", env = env, device = "cpu")
model.learn(total_timesteps=parems[1], log_interval=1, callback = [callback, checkpoint_callback], tb_log_name = string, reset_num_timesteps= False)
model.save(string)
def makeEnv(parems):
parems = list(parems.values())
env = LeverageSimMarket.LeverageSimMarket
# check_env(env, warn = True, skip_render_check = True)
env = make_vec_env(env, n_envs=1, env_kwargs = {'cash' : 500, 'data' : parems[0]}, seed=parems[2])
env = VecCheckNan(env, raise_exception=True)
env = VecNormalize(env, norm_obs=True, norm_reward=True, clip_obs=1., clip_reward=1.)
return env
def Train(parems, tbfolder, learningrate = None, schedlist = None):
string = ""
for key,value in parems.items():
if key == 'data':
string += 'postfinal'
continue
string += "_{}{}".format(key,value)
if isinstance(learningrate, float):
string+= 'learningrate' + str(learningrate)
elif isinstance(schedlist, list):
string += 'lrbeg' + str(schedlist[0]) + 'lrend' + str(schedlist[1]) + 'frac' + str(schedlist[2])
learningrate = linschedmodified(schedlist[0],schedlist[1],schedlist[2])
if learningrate == None and schedlist == None:
'You need to include a learning rate'
quit()
parems = list(parems.values())
checkpoint_callback = CheckpointCallback(save_freq=len(parems[0]), save_path='./' + str(string) + 'defaultcheckpoint/',
name_prefix='postfinal')
#model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = 'results', seed=42, learning_rate= values[2], device = "cpu", ent_coef = values[3], gamma=values[4], clip_range=values[6], batch_size = values[7]) #significantly faster on cpu
if isinstance(learningrate, float):
model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = tbfolder, seed=parems[2], learning_rate= learningrate, device = "cpu", gamma=parems[4], clip_range=parems[6], ent_coef=parems[3]) #significantly faster on cpu
else:
model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = tbfolder, seed=parems[2], learning_rate= learningrate, device = "cpu", gamma=parems[4], clip_range=parems[6], ent_coef=parems[3]) #significantly faster on cpu
callback= LeverageSimMarket.TensorboardCallback()
# model = PPO.load("ppohourly60trainnewmse_timesteps5000000_learningrate0.003", env = env, device = "cpu")
model.learn(total_timesteps=parems[1], log_interval=1, callback = [callback, checkpoint_callback], tb_log_name = string, reset_num_timesteps= False)
model.save(string)
if __name__ == '__main__':
#best timesteps 1000000
#seed 41
#ent 0.0001
#gamma 0.9999
#clip 0.2
#[0.2,0.000001,.60]
np.random.seed(int(time.time()))
train, test = getSplit("1hourly36hahead36hback60percenttest.xlsx", 0.6)
parems = {
'data' : train,
'timesteps' : 1000000,
'seed' : 41,
'ent_coef' : 0.0001, #0.01
'gamma' : 0.9999,
'policy' : "bestfinal", #MLPPOLICY DEFAULT
'clip' : 0.2,
}
env = makeEnv(parems)
Train(parems, schedlist = [0.2,0.000001,.60], tbfolder = "tblogging")
#notes:
#default is bad
#
# values = list(model_parems.values())
# string = ""
# for key,value in model_parems.items():
# if key == 'data':
# string += 'postfinal'
# continue
# string += "_{}{}".format(key,value)
# env = UnscaledSimMarket.UnscaledSimMarket
# # check_env(env, warn = True, skip_render_check = True)
# env = make_vec_env(env, n_envs=1, env_kwargs = {'cash' : 500, 'data' : values[0]}, seed=42)
# env = VecCheckNan(env, raise_exception=True)
# env = VecNormalize(env, norm_obs=True, norm_reward=True, clip_obs=1., clip_reward=1.)
# checkpoint_callback = CheckpointCallback(save_freq=len(traindf), save_path='./' + str(string) + 'defaultcheckpoint/',
# name_prefix='postfinal')
# #model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = 'results', seed=42, learning_rate= values[2], device = "cpu", ent_coef = values[3], gamma=values[4], clip_range=values[6], batch_size = values[7]) #significantly faster on cpu
# model = PPO("MlpPolicy", env, verbose=0, tensorboard_log = 'postfinal', seed=42, learning_rate= utils.get_linear_fn(0.2,0.000001,.60), device = "cpu", gamma=values[4], clip_range=values[6], ent_coef=values[3]) #significantly faster on cpu
# callback= LeverageSimMarket.TensorboardCallback()
# # model = PPO.load("ppohourly60trainnewmse_timesteps5000000_learningrate0.003", env = env, device = "cpu")
# model.learn(total_timesteps=values[1], log_interval=1, callback = [callback, checkpoint_callback], tb_log_name = string, reset_num_timesteps= False)
# model.save(string)
#BEST AND STABLE!!!@@@@@
# train, test = getSplit("1hourly36hahead36hback60percenttest.xlsx", 0.6)
# parems = {
# 'data' : train,
# 'timesteps' : 2000001,
# 'seed' : 42,
# 'ent_coef' : 0.001, #0.01
# 'gamma' : 0.99999,
# 'policy' : "MlpPolicy", #MLPPOLICY DEFAULT
# 'clip' : 0.2,
# }
# # utils.get_linear_fn(0.2,0.000001,.60)
# env = makeEnv(parems)
# #run again, then try it again on a different seed
# Train(parems, learningrate = None, schedlist = [0.2,0.000001,.60], tbfolder = "postfinal")
# print('hi')