-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnncrypto.py
149 lines (108 loc) · 3.86 KB
/
rnncrypto.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
import pandas as pd
import os
from sklearn import preprocessing
from collections import deque
import random
import numpy as np
import time
import tensorflow as tf
from tensorflow.keras.models import Sequaential
from tensorflow.keras.layers import Dense, Dropout, LSTM, CuDNNLSTM, BatchNormilization
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint
SEQ_LEN = 60
FUTURE_PERIOD_PREDICT = 3
RATIO_TO_PREDICT = "LTC-USD"
EPOCHS = 10
BATCH_SIZE = 64
NAME = f"{RATIO_TO_PREDICT}-{SEQ_LEN}-SEQ-{FUTURE_PERIOD_PREDICT}-PRED-{int(time.time())}"
def classify(current,future):
if float(future) > float(current):
return 1
else:
return 0
def preprocess_df(df):
df = df.drop('future',1)
for col in df.columns:
if col != "target":
df[col] = df[col].pct_change()
df.dropna(inplace=True)
df[col] = preprocessing.scale(df[col].values)
df.dropna(inplace=True)
sequential_data = []
prev_days = deque(maxlen=SEQ_LEN)
for i in df.values:
prev_days.append([n for n in i[:-1]])
if len(prev_days) == SEQ_LEN:
sequential_data.append([np.array(prev_days), i[-1]])
random.shuffle(sequential_data)
buys = []
sells = []
for seq, target in sequential_data:
if target == 0:
sells.append([seq,target])
elif target == 1:
buys.append([seq, target])
random.shuffle(buys)
random.shuffle(sells)
lower = min(len(buys), len(sells))
buys = buys[:lower]
sells = sells[:lower]
sequential_data = buys + sells
random.shuffle(sequential_data)
X = []
y = []
for seq, target in sequential_data:
X.append(seq)
y.append(target)
return np.array(X), y
main_df = pd.DataFrame()
ratios = ["BTC-USD", "LTC-USD", "ETH-USD", "BCH-USD"]
for ratio in ratios:
dataset = f"crypto_data/{ratio}.csv"
df = pd.read_csv(dataset, names=["time", "low", "high", "open", "close", "volume"])
#print(df.head())
df.rename(columns={"close": f"{ratio}_close", "volume":f"{ratio}_volume"}, inplace=True)
df.set_index("time", inplace=True)
df = df[[f"{ratio}_close", f"{ratio}_volume"]]
if len(main_df) == 0:
main_df = df
else:
main_df = main_df.join(df)
main_df['future'] = main_df[f"{RATIO_TO_PREDICT}_close"].shift(-FUTURE_PERIOD_PREDICT)
main_df['target'] = list(map(classify, main_df[f"{RATIO_TO_PREDICT}_close"], main_df["future"]))
#print(main_df[[f"{RATIO_TO_PREDICT}_close", "future", "target"]].head(10))
times = sorted(main_df.index.values)
last_5pct = times[-int(0.05*len(times))]
validation_main_df = main_df[(main_df.index >= last_5pct)]
main_df = main_df[(main_df.index >= last_5pct)]
train_x, train_y = preprocess_df(main_df)
validation_x, validation_y = preprocess_df(validation_main_df)
print(f"train data: {len(train_x)} validation: {len(validation_x)}")
print(f"Dont buys : {train_y.count(0)}, buys: {train_y.count(1)}")
print(f"VALDIATION Dont buys: {validation_y.count(0)}, buys: {validation_y.count(1)}")
model = Sequaential()
model.add(CuDNNLSTM(128, input_shape =(train_x.shape[1:]), return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormilization())
model.add(CuDNNLSTM(128, input_shape =(train_x.shape[1:]), return_sequences=True))
model.add(Dropout(0.1))
model.add(BatchNormilization())
model.add(CuDNNLSTM(128, input_shape =(train_x.shape[1:])))
model.add(Dropout(0.2))
model.add(BatchNormilization())
model.add(Dense(32,activation="relu"))
model.add(Droupout(0.2))
model.add(Dense(2,activation="softmax"))
opt = tf.keras.optimizers.Adam(lr=0.001, decay= 1e-6)
model.compile(loss='sparse_categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
tensorboard = TensorBoard(log_dir=f'logs/{NAME}')
filepath = "RNN_Final-{epoch:02d}-{val_acc:3f}"
checkpoint = ModelCheckpoint ("models/{}.model".format(filepath, monitor = 'val_acc', verbose=1, save_best_only=True, mode='max'))
history = model.fit(
train_x, train_y,
batch_size=Batch_SIZE,
epochs=EPOCHS,
validation_data=(validation_x,validation_y),
callbacks=[tensorboard,checkpoint])