aboutsummaryrefslogtreecommitdiffstats
path: root/lstm.py
blob: 7d655529ed15cb08ea77f591b9a6f6f9e789c29b (plain) (blame)
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
#!python
# _*_ coding=utf-8 _*_
# original source:https://github.com/dashee87/blogScripts/blob/master/Jupyter/2017-11-20-predicting-cryptocurrency-prices-with-deep-learning.ipynb

# @#!pip install lxml
# @#!mkdir lstm-models
import argparse
import code
import readline
import signal
import sys
import pandas as pd
import json
import os
import numpy as np
import urllib3
import time
from keras.models import Sequential
from keras.layers import Activation, Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras.models import load_model
from keras import models
from keras import layers

window_len = 10
split_date = "2018-03-01"


def SigHandler_SIGINT(signum, frame):
    print()
    sys.exit(0)


class Argparser(object):
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("--string", type=str, help="string")
        parser.add_argument(
            "--bool", action="store_true", help="bool", default=False
        )
        parser.add_argument(
            "--dbg", action="store_true", help="debug", default=False
        )
        self.args = parser.parse_args()


def getData_CMC(crypto, crypto_short):
    market_info = pd.read_html(
        "https://coinmarketcap.com/currencies/"
        + crypto
        + "/historical-data/?start=20160428&end="
        + time.strftime("%Y%m%d")
    )[0]
    print(type(market_info))
    market_info = market_info.assign(Date=pd.to_datetime(market_info["Date"]))
    # print(market_info)
    # if crypto == "ethereum": market_info.loc[market_info["Market Cap"]=="-","Market Cap"]=0
    # if crypto == "dogecoin": market_info.loc[market_info["Volume"]=="-","Volume"]=0
    market_info["Volume"] = market_info["Volume"].astype("int64")
    market_info.columns = market_info.columns.str.replace("*", "")
    # print(type(market_info))
    # print(crypto + " head: ")
    # print(market_info.head())
    kwargs = {
        "close_off_high": lambda x: 2
        * (x["High"] - x["Close"])
        / (x["High"] - x["Low"])
        - 1,
        "volatility": lambda x: (x["High"] - x["Low"]) / (x["Open"]),
    }
    market_info = market_info.assign(**kwargs)
    model_data = market_info[
        ["Date"]
        + [
            coin + metric
            for coin in [""]
            for metric in ["Close", "Volume", "close_off_high", "volatility"]
        ]
    ]
    model_data = model_data.sort_values(by="Date")
    # print(model_data.head())
    print(type(model_data))
    return model_data


def getData_Stock(name, period):
    info = pd.read_csv(
        "./data/" + name + "/" + period + ".csv", encoding="utf-16"
    )
    return info


def get_sets(crypto, model_data):
    training_set, test_set = (
        model_data[model_data["Date"] < split_date],
        model_data[model_data["Date"] >= split_date],
    )
    training_set = training_set.drop("Date", 1)
    test_set = test_set.drop("Date", 1)
    norm_cols = [
        coin + metric for coin in [] for metric in ["Close", "Volume"]
    ]
    LSTM_training_inputs = []
    for i in range(len(training_set) - window_len):
        temp_set = training_set[i : (i + window_len)].copy()
        for col in norm_cols:
            temp_set.loc[:, col] = temp_set[col] / temp_set[col].iloc[0] - 1
        LSTM_training_inputs.append(temp_set)
    LSTM_training_outputs = (
        training_set["Close"][window_len:].values
        / training_set["Close"][:-window_len].values
    ) - 1
    LSTM_test_inputs = []
    for i in range(len(test_set) - window_len):
        temp_set = test_set[i : (i + window_len)].copy()
        for col in norm_cols:
            temp_set.loc[:, col] = temp_set[col] / temp_set[col].iloc[0] - 1
        LSTM_test_inputs.append(temp_set)
    LSTM_test_outputs = (
        test_set["Close"][window_len:].values
        / test_set["Close"][:-window_len].values
    ) - 1
    print(LSTM_training_inputs[0])
    LSTM_training_inputs = [
        np.array(LSTM_training_input)
        for LSTM_training_input in LSTM_training_inputs
    ]
    LSTM_training_inputs = np.array(LSTM_training_inputs)

    LSTM_test_inputs = [
        np.array(LSTM_test_inputs) for LSTM_test_inputs in LSTM_test_inputs
    ]
    LSTM_test_inputs = np.array(LSTM_test_inputs)
    return LSTM_training_inputs, LSTM_test_inputs, training_set, test_set


def build_model(
    inputs,
    output_size,
    neurons,
    activ_func="linear",
    dropout=0.25,
    loss="mae",
    optimizer="adam",
):
    model = Sequential()
    model.add(LSTM(neurons, input_shape=(inputs.shape[1], inputs.shape[2])))
    model.add(Dropout(dropout))
    model.add(Dense(units=output_size))
    model.add(Activation(activ_func))
    model.compile(loss=loss, optimizer=optimizer)
    return model


def stock():
    split_date = "2017.01.01"
    model_data = getData_Stock("irxo", "Daily")
    model_data = model_data.sort_values(by="Date")

    training_set, test_set = (
        model_data[model_data["Date"] < split_date],
        model_data[model_data["Date"] >= split_date],
    )
    training_set = training_set.drop("Date", 1)
    test_set = test_set.drop("Date", 1)

    training_inputs = training_set
    training_outputs = training_set.drop(
        ["Open", "High", "Low", "NTx", "Volume"], axis=1
    )
    test_inputs = test_set
    test_outputs = test_set.drop(
        ["Open", "High", "Low", "NTx", "Volume"], axis=1
    )

    print(training_set.head)
    print(test_set.head)
    print(training_inputs.shape)
    print(test_inputs.shape)
    print(training_outputs.shape)
    print(test_outputs.shape)

    model = models.Sequential()
    model.add(
        layers.Dense(
            64, activation="relu", input_shape=(training_inputs.shape[1],)
        )
    )
    model.add(layers.Dense(64, activation="relu"))
    model.add(layers.Dense(1))
    model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"])
    history = model.fit(
        training_inputs,
        training_outputs,
        validation_data=(test_inputs, test_outputs),
        epochs=10,
        batch_size=1,
        verbose=2,
    )


def lstm_type_1(crypto, crypto_short):
    model_data = getData_CMC(crypto, crypto_short)
    np.random.seed(202)
    training_inputs, test_inputs, training_set, test_set = get_sets(
        crypto, model_data
    )
    model = build_model(training_inputs, output_size=1, neurons=20, loss="mse")
    training_outputs = (
        training_set["Close"][window_len:].values
        / training_set["Close"][:-window_len].values
    ) - 1
    history = model.fit(
        training_inputs,
        training_outputs,
        epochs=50,
        batch_size=1,
        verbose=2,
        shuffle=True,
    )


def lstm_type_4(crypto, crypto_short, crypto2, crypto_short2):
    model_data = getData_CMC(crypto, crypto_short)
    model_data2 = getData_CMC(crypto2, crypto_short2)
    np.random.seed(202)
    training_inputs, test_inputs, training_set, test_set = get_sets(
        crypto, model_data
    )
    training_inputs2, test_inputs2, training_set2, test_set2 = get_sets(
        crypto2, model_data2
    )
    return
    model = build_model(
        training_inputs / training_inputs2,
        output_size=1,
        neurons=20,
        loss="mse",
    )
    training_outputs = (
        (training_set["Close"][window_len:].values)
        / (training_set["Close"][:-window_len].values)
    ) - 1
    history = model.fit(
        training_inputs / training_inputs2,
        training_outputs,
        epochs=10,
        batch_size=1,
        verbose=2,
        shuffle=True,
    )


def lstm_type_2(crypto, crypto_short, pred_range, neuron_count):
    model_data = getData_CMC(crypto, crypto_short)
    np.random.seed(202)
    training_inputs, test_inputs, training_set, test_set = get_sets(
        crypto, model_data
    )
    model = build_model(
        training_inputs,
        output_size=pred_range,
        neurons=neuron_count,
        loss="mse",
    )
    training_outputs = (
        training_set["Close"][window_len:].values
        / training_set["Close"][:-window_len].values
    ) - 1
    training_outputs = []
    for i in range(window_len, len(training_set["Close"]) - pred_range):
        training_outputs.append(
            (
                training_set["Close"][i : i + pred_range].values
                / training_set["Close"].values[i - window_len]
            )
            - 1
        )
    training_outputs = np.array(training_outputs)
    history = model.fit(
        training_inputs[:-pred_range],
        training_outputs,
        epochs=50,
        batch_size=1,
        verbose=2,
        shuffle=True,
    )


def lstm_type_3(crypto, crypto_short, pred_range, neuron_count):
    model_data = getData_CMC(crypto, crypto_short)
    np.random.seed(202)
    training_inputs, test_inputs, training_set, test_set = get_sets(
        crypto, model_data
    )
    model = build_model(training_inputs, output_size=1, neurons=neuron_count)
    training_outputs = (
        training_set["Close"][window_len:].values
        / training_set["Close"][:-window_len].values
    ) - 1
    training_outputs = []
    for rand_seed in range(775, 800):
        print(rand_seed)
        np.random.seed(rand_seed)
        temp_model = build_model(
            training_inputs, output_size=1, neurons=neuron_count
        )
        temp_model.fit(
            training_inputs,
            (
                training_set["Close"][window_len:].values
                / training_set["Close"][:-window_len].values
            )
            - 1,
            epochs=50,
            batch_size=1,
            verbose=0,
            shuffle=True,
        )
        temp_model.save(
            "./lstm-models/" + crypto + "_model_randseed_%d.h5" % rand_seed
        )


def load_models(crypto, crypto_short):
    preds = []
    model_data = getData_CMC(crypto, crypto_short)
    np.random.seed(202)
    training_inputs, test_inputs, training_set, test_set = get_sets(
        crypto, model_data
    )
    for rand_seed in range(775, 800):
        temp_model = load_model(
            "./lstm-models/" + crypto + "_model_randseed_%d.h5" % rand_seed
        )
        preds.append(
            np.mean(
                abs(
                    np.transpose(temp_model.predict(test_inputs))
                    - (
                        test_set["Close"].values[window_len:]
                        / test_set["Close"].values[:-window_len]
                        - 1
                    )
                )
            )
        )


# write code here
def premain(argparser):
    signal.signal(signal.SIGINT, SigHandler_SIGINT)
    # here
    # lstm_type_1("ethereum", "ether")
    # lstm_type_2("ethereum", "ether", 5, 20)
    # lstm_type_3("ethereum", "ether", 5, 20)
    # lstm_type_4("ethereum", "ether", "dogecoin", "doge")
    # load_models("ethereum", "eth")
    stock()


def main():
    argparser = Argparser()
    if argparser.args.dbg:
        try:
            premain(argparser)
        except Exception as e:
            print(e.__doc__)
            if e.message:
                print(e.message)
            variables = globals().copy()
            variables.update(locals())
            shell = code.InteractiveConsole(variables)
            shell.interact(banner="DEBUG REPL")
    else:
        premain(argparser)


if __name__ == "__main__":
    main()