forked from nguyennoapp/mimibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
executable file
·200 lines (173 loc) · 7.34 KB
/
trainer.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
#!/usr/bin/env python3
import ccxt
from configparser import ConfigParser
import json
import os
import pickle
import redis
import socket
import time
import tempfile
import zlib
from random import shuffle
from requests_futures.sessions import FuturesSession
import numpy as np
from pandas import DataFrame
from sklearn.preprocessing import MinMaxScaler
import keras.models
from keras.models import Sequential, load_model
from keras.layers import LSTM, Dense
TIME_FRAMES = ['15m', '1h', '4h', '1d']
FEATURE_SIZE = 5
STEP_SIZE = 5
LSTM_SIZE = FEATURE_SIZE * STEP_SIZE
LSTM_MODEL = 'lstm'
BOT_NAME = 'Trainer'
HOST_NAME = socket.gethostname()
SAVE_PATH = '{}/model'.format(os.path.dirname(os.path.abspath(__file__)))
CONFIG_FILE = '{}/config.ini'.format(os.path.dirname(os.path.abspath(__file__)))
config = ConfigParser()
config.read(CONFIG_FILE)
session = FuturesSession()
rd = redis.StrictRedis(host=config['REDIS']['HOST'],
port=config['REDIS']['PORT'],
password=config['REDIS']['PASS'], db=0)
exchange = ccxt.binance({'apiKey': config['BINANCE']['KEY'],
'secret': config['BINANCE']['SECRET']})
def make_keras_picklable():
def __getstate__(self):
with tempfile.NamedTemporaryFile(suffix='.hdf5', delete=True) as fd:
keras.models.save_model(self, fd.name, overwrite=True)
model_str = fd.read()
d = {'model_str': model_str}
return d
def __setstate__(self, state):
with tempfile.NamedTemporaryFile(suffix='.hdf5', delete=True) as fd:
fd.write(state['model_str'])
fd.flush()
model = keras.models.load_model(fd.name)
self.__dict__ = model.__dict__
cls = keras.models.Model
cls.__getstate__ = __getstate__
cls.__setstate__ = __setstate__
class Symbol(object):
def __init__(self, obj):
self.__dict__ = json.loads(json.dumps(obj))
def log(text):
msg = '{} {} {} {}'.format(time.strftime("%d/%m/%Y %H:%M"), HOST_NAME, BOT_NAME, text)
url = 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}&parse_mode=markdown' \
.format(config['TELEGRAM']['BOT'], config['TELEGRAM']['CHAT'], msg)
session.get(url)
print(msg)
return
def open_model(new=False):
name = LSTM_MODEL
model = None
filename = '{}/{}.h5'.format(SAVE_PATH, name)
log('load LSTM model from file: {}'.format(filename))
try:
model = load_model(filename)
log('load LSTM model from file success: {}'.format(name))
except Exception as e:
log('load LSTM model from file error: {}'.format(str(e)))
log('load LSTM model from redis: {}'.format(name))
try:
model = pickle.loads(zlib.decompress(rd.get(name)), encoding='latin1')
log('load LSTM model from redis success: {}'.format(name))
except Exception as e:
log('load LSTM model from redis error: {}'.format(str(e)))
new_model = Sequential()
new_model.add(LSTM(LSTM_SIZE, stateful=True, return_sequences=True,
batch_input_shape=(LSTM_SIZE * 100, STEP_SIZE, FEATURE_SIZE)))
new_model.add(LSTM(LSTM_SIZE, stateful=True, return_sequences=True))
new_model.add(LSTM(LSTM_SIZE, stateful=True, return_sequences=True))
new_model.add(LSTM(LSTM_SIZE, stateful=True, return_sequences=True))
new_model.add(LSTM(FEATURE_SIZE, stateful=True))
new_model.add(Dense(FEATURE_SIZE, activation='linear'))
new_model.add(Dense(1))
new_model.compile(optimizer='adagrad', metrics=['accuracy'], loss='mse')
if not new and model is not None:
new_model.set_weights(model.get_weights())
else:
log('create LSTM model success: {}'.format(name))
return new_model
def save_model(model):
new_model = Sequential()
new_model.add(LSTM(LSTM_SIZE, stateful=True, return_sequences=True,
batch_input_shape=(1, STEP_SIZE, FEATURE_SIZE)))
new_model.add(LSTM(LSTM_SIZE, stateful=True, return_sequences=True))
new_model.add(LSTM(LSTM_SIZE, stateful=True, return_sequences=True))
new_model.add(LSTM(LSTM_SIZE, stateful=True, return_sequences=True))
new_model.add(LSTM(FEATURE_SIZE, stateful=True))
new_model.add(Dense(FEATURE_SIZE, activation='linear'))
new_model.add(Dense(1))
new_model.compile(optimizer='adagrad', metrics=['accuracy'], loss='mse') # adagrad
new_model.set_weights(model.get_weights())
name = LSTM_MODEL
filename = '{}/{}.h5'.format(SAVE_PATH, name)
try:
log('save LSTM model to file: {}'.format(filename))
new_model.save(filename)
log('save LSTM model to file success')
except Exception as e:
log('save LSTM model to file error: {}'.format(str(e)))
try:
log('save LSTM model to redis: {}'.format(name))
rd.set(name, zlib.compress(pickle.dumps(new_model)))
log('save LSTM model to redis success')
except Exception as e:
log('save LSTM model to redis error: {}'.format(str(e)))
def symbol_data(symbol, time_frame):
data = exchange.fetch_ohlcv(symbol, time_frame)
df = DataFrame(data, columns=['time', 'open', 'high', 'low', 'close', 'volume'])
df.set_index('time')
df.replace({0: np.nan}, inplace=True)
df['price'] = df[['open', 'high', 'low', 'close']].mean(axis=1)
df['price_change'] = df['price'].pct_change()
df['volume_change'] = df['volume'].pct_change()
df = df.assign(**{'volatility': lambda x: (x['high'] - x['low']) / x['open']})
df = df.assign(**{'convergence': lambda x: (x['open'] - x['close']) / (x['high'] - x['low'])})
df = df.assign(**{'predisposition': lambda x: 1 - 2 * (x['high'] - x['close']) / (x['high'] - x['low'])})
df.dropna(axis=0, how='any', inplace=True)
sc = MinMaxScaler(feature_range=(-1, 1))
na = sc.fit_transform(df[['price_change', 'volume_change', 'volatility', 'convergence', 'predisposition']])
return na, na[:, 0]
def save_data():
exchange.load_markets(reload=True)
train_x = []
train_y = []
for time_frame in TIME_FRAMES:
log('start collect data with time frame: {}'.format(time_frame))
symbols = exchange.symbols
shuffle(symbols)
for symbol in symbols:
log('start collect data from symbol: {}'.format(symbol))
input_data, output_data = symbol_data(symbol, time_frame)
if len(input_data) == 0:
continue
for i in range(len(input_data) - STEP_SIZE - 1):
train_x.append(input_data[i:i + STEP_SIZE])
train_y.append(output_data[i + STEP_SIZE])
length = int(len(train_x) // (LSTM_SIZE * 100) * (LSTM_SIZE * 100))
train_x = train_x[-length:]
train_y = train_y[-length:]
train_x = np.array(train_x)
train_y = np.array(train_y)
np.save('{}/train_x.npy'.format(SAVE_PATH), train_x)
np.save('{}/train_y.npy'.format(SAVE_PATH), train_y)
log('save train data success')
def train():
model = open_model()
train_x = np.load('{}/train_x.npy'.format(SAVE_PATH))
train_y = np.load('{}/train_y.npy'.format(SAVE_PATH))
log('train LSTM model with data length: %g' % len(train_x))
model.fit(train_x, train_y, epochs=LSTM_SIZE * 10, batch_size=LSTM_SIZE * 100, verbose=1)
log('train LSTM model success')
save_model(model)
def main():
log('*{} started*'.format(BOT_NAME))
make_keras_picklable()
save_data()
train()
if __name__ == "__main__":
main()