-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoui.py
140 lines (119 loc) · 5 KB
/
noui.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
# Load Libraries
from urllib.error import HTTPError
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
import datetime as dt
from copy import deepcopy
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
# Globals
# Hello World Part 2
# REQUEST URL Vars, These are default values. The UI will change them before making the reuquest.
global COIN_SYMBOL; COIN_SYMBOL = 'BTC'
global PERIOD_ID; PERIOD_ID = '1DAY'
global START_DATE; START_DATE = '2015-01-01'
global END_DATE; END_DATE = '2016-02-01'
global LIMIT; LIMIT = 900
global API_KEY; API_KEY = '53663783-E96C-4CF7-A3F4-0F8D59946927'
global API_KEY2; API_KEY2 = '9117E3A0-8011-4C76-830D-F7BFB6D96199'
global REQUEST_URL; REQUEST_URL = 'https://rest.coinapi.io/v1/exchangerate/{}/USD/history?period_id={}&time_start={}&time_end={}&limit={}&apikey={}&output_format=csv'
# Training
global LOOK_BACK; LOOK_BACK = 6
global NUM_EPOCHS; NUM_EPOCHS = 300
global BATCH_SIZE; BATCH_SIZE = 32
global TT_SPLIT; TT_SPLIT = .67
# convert an array of values into a dataset matrix
def create_dataset(dataset, lookBack=1):
dataX, dataY = [], []
for i in range(len(dataset)-lookBack-1):
a = dataset[i:(i+lookBack), 0]
dataX.append(a)
dataY.append(dataset[i + lookBack, 0])
return np.array(dataX), np.array(dataY)
def main(TICKER, lim):
# Import BTC/USD data
""" url = REQUEST_URL.format(COIN_SYMBOL, PERIOD_ID, START_DATE, END_DATE, LIMIT, API_KEY)
print(url)
try:
data = pd.read_csv(url, sep=';')
except(HTTPError):
print("Too many requests to API! Using Default Dataset")
data = pd.read_csv('test.csv', sep=';') """
data = pd.read_csv('CSV/%s.csv' % TICKER, sep=';')
data['date'] = [i[:10] for i in data['time_period_start']]
# Create Dataframe
df = deepcopy(data)
df = df[['rate_open']]
dataset = df.values
dataset = dataset.astype('float32')
# Scale Data Frame
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# Define Training and Testing Boundaries
train_size = int(len(dataset) * TT_SPLIT)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
# Create Data Sets
trainX, trainY = create_dataset(train, lookBack=LOOK_BACK)
testX, testY = create_dataset(test, lookBack=LOOK_BACK)
try:
# Reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
except(Exception):
print("Not enough data! Choose a larger timeframe or a smaller trading interval.")
return
# Build Model
model = Sequential()
model.add(LSTM(4, input_shape=(1, LOOK_BACK)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, verbose=2)
# Make Predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# Revert Predictions to Previous Scale
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
# Print Scores
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:, 0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:, 0]))
print('Test Score: %.2f RMSE' % (testScore))
# Plot Results
trainPredictPlot = np.empty_like(dataset)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[LOOK_BACK:len(trainPredict) + LOOK_BACK, :] = trainPredict
testPredictPlot = np.empty_like(dataset)
testPredictPlot[:, :] = np.nan
testPredictPlot[len(trainPredict) + (LOOK_BACK * 2) + 1:len(dataset) - 1, :] = testPredict
plt.plot(df['rate_open'], label='Actual')
plt.plot(pd.DataFrame(trainPredictPlot, columns=["rate_open"], index=data['date']).rate_open, label='Training')
plt.plot(pd.DataFrame(testPredictPlot, columns=["rate_open"], index=data['date']).rate_open, label='Testing')
plt.title(label="Coin: {} Interval: {} Epochs: {} Lookback: {} Batchsize: {}".format(TICKER, PERIOD_ID, NUM_EPOCHS, LOOK_BACK, BATCH_SIZE))
plt.legend(loc='best')
plt.xticks(np.arange(0, len(data['date']), len(data['date']) / 20 ), rotation=80)
plt.subplots_adjust(bottom=.265, top=.95, left=.1, right=.98)
plt.xlabel("Date\nTrain RMSE: %.2f Test RMSE: %.2f" % (trainScore, testScore))
plt.ylabel("Coin Price")
#plt.xlim(['2020-01-01', '2022-02-01'])
plt.ylim([0, lim])
plt.savefig('IMG/after/%s-AFTER.png' % TICKER, transparent=True)
plt.show()
if __name__ == "__main__":
tickers0 = ['ADA', 'DOGE', 'USDT', 'SHIB', 'XTZ']
tickers1 = ['AVAX', 'SOL', 'DOT']
tickers2 = ['BTC', 'ETH']
for ticker in tickers0:
main(ticker, 5)
for ticker in tickers1:
main(ticker, 1000)
for ticker in tickers2:
main(ticker, 70000)