-
Notifications
You must be signed in to change notification settings - Fork 133
/
engine.py
150 lines (120 loc) · 4.81 KB
/
engine.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
from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
import pandas as pd
from datetime import datetime
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
import numpy as np
import datetime
from pytz import timezone
est = timezone('US/Eastern')
from twilio.rest import Client
# Your Account SID from twilio.com/console
account_sid = "*******"
# Your Auth Token from twilio.com/console
auth_token = "*******"
client = Client(account_sid, auth_token)
data = pd.read_csv("merged_data.csv")
datag = data[['Price','Sentiment']].groupby(data['Time']).mean()
from sklearn.preprocessing import MinMaxScaler
values = datag['Price'].values.reshape(-1,1)
sentiment = datag['Sentiment'].values.reshape(-1,1)
values = values.astype('float32')
sentiment = sentiment.astype('float32')
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
train_size = int(len(scaled) * 0.7)
test_size = len(scaled) - train_size
train, test = scaled[0:train_size,:], scaled[train_size:len(scaled),:]
print(len(train), len(test))
split = train_size
def create_dataset(dataset, look_back, sentiment, sent=False):
dataX, dataY = [], []
for i in range(len(dataset) - look_back):
if i >= look_back:
a = dataset[i-look_back:i+1, 0]
a = a.tolist()
if(sent==True):
a.append(sentiment[i].tolist()[0])
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
#print(len(dataY))
return np.array(dataX), np.array(dataY)
look_back = 2
trainX, trainY = create_dataset(train, look_back, sentiment[0:train_size],sent=True)
testX, testY = create_dataset(test, look_back, sentiment[train_size:len(scaled)], sent=True)
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
model = Sequential()
model.add(LSTM(100, input_shape=(trainX.shape[1], trainX.shape[2]), return_sequences=True))
model.add(LSTM(100))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(trainX, trainY, epochs=300, batch_size=100, validation_data=(testX, testY), verbose=0, shuffle=False)
yhat = model.predict(testX)
yhat_inverse_sent = scaler.inverse_transform(yhat.reshape(-1, 1))
testY_inverse_sent = scaler.inverse_transform(testY.reshape(-1, 1))
rmse_sent = sqrt(mean_squared_error(testY_inverse_sent, yhat_inverse_sent))
print('Test RMSE: %.3f' % rmse_sent)
import MySQLdb
#Enter the values for you database connection
dsn_database = "bitcoin" # e.g. "MySQLdbtest"
dsn_hostname = "173.194.231.244" # e.g.: "mydbinstance.xyz.us-east-1.rds.amazonaws.com"
dsn_port = 3306 # e.g. 3306
dsn_uid = "demo" # e.g. "user1"
dsn_pwd = "qwerty@123" # e.g. "Password123"
conn = MySQLdb.connect(host=dsn_hostname, port=dsn_port, user=dsn_uid, passwd=dsn_pwd, db=dsn_database)
cursor=conn.cursor()
import queue
import time
import queue
import matplotlib.pyplot as plt
true_q = queue.Queue()
pred_q = queue.Queue()
'''
fig = plt.figure()
ax = fig.add_subplot(111)
fig.show()
fig.canvas.draw()
plt.ion()
'''
def process_data(in_data):
out_data = []
for line in in_data:
out_data.append(float(line.split(',')[0]))
return np.array(out_data).reshape(-1,1)
prev = 15000
threshold = 0.05
while True:
btc = open('live_bitcoin.csv','r')
sent = open('live_tweet.csv','r')
bit_data = btc.readlines()
sent_data = sent.readlines()
bit_data = process_data(bit_data[len(bit_data)-5:])
sent_data = process_data(sent_data[len(sent_data)-5:])
live = scaler.transform(bit_data)
testX, testY = create_dataset(live, 2, sent_data, sent=True)
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
yhat = model.predict(testX)
yhat_inverse = scaler.inverse_transform(yhat.reshape(-1, 1))
true_q.put(bit_data[4])
pred_q.put(yhat_inverse[0])
val = 100*((yhat_inverse[0][0] - prev)/prev)
if val > threshold:
decision = 'Buy!!!'
message = client.messages.create(to="+15184234418", from_="+15188883052", body=decision+' - Price of Bitcoin is expected to rise.')
elif val <-threshold:
decision = 'Sell!!!'
message = client.messages.create(to="+15184234418", from_="+15188883052", body=decision+' - Price of Bitcoin is expected to drop.')
else:
decision = ''
prev = yhat_inverse[0][0]
input_string = "INSERT INTO live_data values ({},{},{},'{}','{}');".format(yhat_inverse[0][0],bit_data[0][0],sent_data[4][0],datetime.datetime.now(tz=est).strftime('%Y-%m-%d %H:%M:%S'),decision)
cursor.execute(input_string)
conn.commit()
time.sleep(60)