-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptoBot.py
More file actions
241 lines (191 loc) · 8.72 KB
/
cryptoBot.py
File metadata and controls
241 lines (191 loc) · 8.72 KB
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
#Imports
import config
import pickle
import numpy as np
import requests
import threading
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
from keras.models import load_model
from tensorflow.python.keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt
import keras.backend as K
import datetime
import time as t
import scipy.stats as stats
import cbpro
import pandas as pd
from PIL import Image
from time import sleep
#Log into coinbase
client = cbpro.AuthenticatedClient(config.coinbasePublic, config.coinbaseSecretKey, config.coinbasePassPhrase)
#Create coins
class Coin:
def __init__(self, symbol):
self.symbol = symbol
self.alreadyHave = False
self.prices = []
self.predictedPrices = []
self.priceBoughtAt = 0
self.quantityBought = 0
#Coins
coins = []
#Neural Network Settings, predictionRequired must be lower than dataPoints(Restart entire setup procedure if you change anything here, also do not change the predictedPoints)
predictionRequired = 100
predictAhead = 60
predictedPoints = predictAhead + 200
#Number of data points and refresh rate in seconds, dataPoints should stay 500, initial data is amount of data fed into training
dataPoints = 500
refreshRate = 300
initialData = 400
#Multiplier for trades, 1 means it will buy all it can, 0.5 means it will trade with half the money in one trade and could spend the other half on another or split it up more
budgetTolerance = 0.25
#Number of coins you want to track
numCoins = 100
#Do not change
ranOnce = False
def collectInitialData():
coins = client.get_products()
numCoins = 100
for coin in coins:
coins.append(Coin(coin['id']))
if numCoins == 100:
break
for coin in coins:
endTime = datetime.datetime.utcnow()
startTime = endTime - datetime.timedelta(minutes=60 * 5)
count = 0
while True:
historical = pd.DataFrame(client.get_product_historic_rates(product_id=coin.symbol, start=startTime, end=endTime))
historical.columns= ["Date","Open","High","Low","Close","Volume"]
historical['Date'] = pd.to_datetime(historical['Date'], unit='s')
for i in range(len(historical)):
if int(str(historical.iloc[i]['Date']).split(':')[1]) % 5 == 0:
coin.prices.insert(0, float(historical.iloc[i]['Close']))
count += 1
if count == 500:
break
if count == 500:
break
endTime = startTime
startTime = startTime = endTime - datetime.timedelta(minutes=60 * 5)
with open('crypto.txt', 'wb') as fh:
pickle.dump(coins, fh, pickle.HIGHEST_PROTOCOL)
def on_ready():
try:
if ranOnce == False:
collectInitialData()
except Exception as e:
print("On Ready: " + str(e))
def collectData():
while True:
start = t.time()
#Update prices
for coin in coins:
price = None
while price == None:
try:
price = requests.get('https://api.pro.coinbase.com/products/' + coin.symbol + '/ticker').json()
price = price['price']
except:
pass
coin.prices.append(price)
while len(coin.prices) > dataPoints:
coins.prices.pop(0)
#Write information into file
with open("crypto.txt", "wb") as filehandler:
pickle.dump(coins, filehandler, pickle.HIGHEST_PROTOCOL)
end = t.time()
#Get wait time
newRefresh = round(refreshRate - (end - start))
if newRefresh > 0:
await sleep(newRefresh)
def train():
while True:
global coins
for coin in coins:
K.clear_session()
if len(coin.prices) == dataPoints:
fileName = "./models/" + str(coin.symbol) + "model.h5"
#Prices used to train and predict next points
prices = []
for i in range(initialData):
prices.append(coin.prices[i])
#Prepare data using first 400 points
scaler = MinMaxScaler(feature_range=(0, 1))
trainPrices = np.array(prices)
scaled_data = scaler.fit_transform(trainPrices.reshape(-1, 1))
x_train = []
y_train = []
for x in range(predictionRequired, len(scaled_data) - predictAhead):
x_train.append(scaled_data[x - predictionRequired:x, 0])
y_train.append(scaled_data[x + predictAhead, 0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
try:
model = load_model(fileName)
except:
#Build model
model = Sequential()
#Experiment with layers, more layers longer time to train
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=1)) #Prediction of next closing value
model.compile(optimizer='adam', loss='mean_squared_error')
#Epoch = how many times model sees data, batchsize = how many units it sees at once
callbacks = [EarlyStopping(monitor='val_loss', patience=100)]
model.fit(x_train, y_train, epochs=1000, validation_split=0.2, callbacks=callbacks)
# model.fit(x_train, y_train, epochs=1000)
model.save(fileName)
#Get prices to predict data
prices = []
for i in range(0, initialData - predictAhead):
prices.append(coin.prices[i])
#Get predicted prices for points 400 - 500
for i in range(initialData - predictAhead + 1, dataPoints - predictAhead):
scaler = MinMaxScaler(feature_range=(0, 1))
predictPrices = np.array(prices).reshape(-1, 1)
scaler = scaler.fit(predictPrices)
total_dataset = predictPrices
model_inputs = np.array(total_dataset[len(total_dataset) - predictionRequired:]).reshape(-1, 1)
model_inputs = scaler.transform(model_inputs)
#Predict Next period
real_data = [model_inputs[len(model_inputs) - predictionRequired:len(model_inputs), 0]]
real_data = np.array(real_data)
real_data = np.reshape(real_data, (real_data.shape[0], real_data.shape[1], 1))
prediction = model.predict(real_data)
prediction = scaler.inverse_transform(prediction)
coin.predictedPrices.append(prediction)
prices.append(coin.prices[i])
while len(coin.predictedPrices) > dataPoints:
coin.predictedPrices.pop(0)
#Get actual prices for coins used for result
actualPrices = []
for i in range(initialData, dataPoints):
actualPrices.append(float(coin.prices[i]))
actualPrices = np.reshape(actualPrices, -1)
predictedPrices = coin.predictedPrices
predictedPrices = np.reshape(predictedPrices, -1)
plt.style.use('dark_background')
plt.plot(actualPrices, color='white')
plt.plot(predictedPrices, color='green')
plt.title(str(coin.symbol))
plt.savefig("./plots/" + str(coin.symbol) + ".png", transparent=True)
plt.clf()
coin.newPredictions = True
result = stats.ttest_ind(a=coin.prices, b=coin.predictedPrices, equal_var=True)
p = result[1]
if p > 0.05:
with open('./plots/' + str(coin.symbol) + '.png', 'rb') as fh:
picture = discord.File(fh)
await generalChannel.send(file=picture)
if __name__ == '__main__':
on_ready()
t1 = threading.Thread(target=train)
t1.start()
t2 = threading.Thread()