-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc_4h.py
99 lines (81 loc) · 3.12 KB
/
btc_4h.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
# required libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.metrics import classification_report, confusion_matrix
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout, SimpleRNN
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
# Load data
data = pd.read_csv('btc_4h.csv')
data.drop(columns='timestamp',inplace=True)
data.head()
# target and more features calculations
data['target'] = (np.sign(data['close']-data['open'])+1)/2
data['f1'] = data['high']-data['open']
data['f2'] = data['close']-data['low']
print(data.head())
print(data.info())
# feature and target separation
X = data.drop(columns=['open','close','target'])
y = data['target']
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=10)
# data normalization
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Build LSTM model
model1 = Sequential()
model1.add(LSTM(64, activation='relu',input_shape=(X_train.shape[1],1)))
model1.add(Dropout(0.2))
model1.add(Dense(units=8,activation='relu'))
model1.add(Dense(units=1,activation='tanh'))
# Compile model
model1.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
# Train model
history1 = model1.fit(X_train, y_train, epochs=50, batch_size=128, verbose=None)
# Evaluate model
loss1, accuracy1 = model1.evaluate(X_test,y_test)
print(f'model_1 loss value is: {loss1} \n')
print(f'model_1 accuracy is: {accuracy1}')
fig,ax = plt.subplots(1,2,figsize=[12,5])
ax[0].plot(np.arange(1,101),history1.history['loss'])
ax[0].set_xlabel('Epoch')
ax[0].set_ylabel('Loss')
ax[0].set_title('Loss over learning epochs')
ax[0].grid()
ax[1].plot(np.arange(1,101),history1.history['accuracy'])
ax[1].set_xlabel('Epoch')
ax[1].set_ylabel('Accuracy')
ax[1].set_title('Accuracy over learning epochs')
ax[1].grid()
# Build SimpleRNN model
model2 = Sequential()
model2.add(SimpleRNN(units=64, activation='relu', input_shape=(X_train.shape[1], 1), return_sequences=True))
model2.add(Dropout(0.2))
model2.add(SimpleRNN(units=32, activation='relu', return_sequences=True))
model2.add(Dropout(0.2))
model2.add(SimpleRNN(units=8, activation='relu'))
model2.add(Dense(units=1,activation='tanh'))
# Compile model
model2.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
# Train model
history2 = model2.fit(X_train, y_train, epochs=50, batch_size=128, verbose=None)
# Evaluate model
loss2, accuracy2 = model2.evaluate(X_test,y_test)
print(f'model_2 loss value is: {loss2} \n')
print(f'model_2 accuracy is: {accuracy2}')
fig,ax = plt.subplots(1,2,figsize=[12,5])
ax[0].plot(np.arange(1,101),history2.history['loss'])
ax[0].set_xlabel('Epoch')
ax[0].set_ylabel('Loss')
ax[0].set_title('Loss over learning epochs')
ax[0].grid()
ax[1].plot(np.arange(1,101),history2.history['accuracy'])
ax[1].set_xlabel('Epoch')
ax[1].set_ylabel('Accuracy')
ax[1].set_title('Accuracy over learning epochs')
ax[1].grid()