-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstock_price.py
140 lines (106 loc) · 4.27 KB
/
stock_price.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
import numpy
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pandas
import math
# Keras is used to model the network
from keras.models import Sequential, load_model
from keras.layers import Dense
from keras.layers import LSTM
from keras.callbacks import TensorBoard
# from keras.utils import normalize
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.metrics import mean_absolute_error
from model import build_lstm_model, prediction_sequence
from data_processing import build_stock_input, merge_data
def main():
# Change to get new results
numpy.random.seed(123456)
# Reading stock data
# Edit these strings to change which stocks to create predictions from.
raw_1 = pandas.read_csv('data/TEVA.csv', usecols=[2], engine='python').values.astype('float32')
raw_2 = pandas.read_csv('data/GOLD.csv', usecols=[2], engine='python').values.astype('float32')
# plt.plot(stock_1)
# plt.show()
scaler_1 = preprocessing.StandardScaler().fit(raw_1)
scaler_2 = preprocessing.StandardScaler().fit(raw_2)
stock_1 = scaler_1.transform(raw_1)
stock_2 = scaler_2.transform(raw_2)
# Creating training and testing data
stock_1 = build_stock_input(data_list = stock_1, input_size = 100, test_ratio = 0.1, step_size = 5)
stock_2 = build_stock_input(data_list = stock_2, input_size = 100, test_ratio = 0.1, step_size = 5)
train_x1 = stock_1[0]
train_x2 = stock_2[0]
train_y1 = stock_1[1]
train_y2 = stock_2[1]
test_x1 = stock_1[2]
test_x2 = stock_2[2]
test_y = stock_1[3]
print("Loaded and processed data")
# print(train_y)
# # Creating a scaler object and apply it to the data set
# Does not work in 2D
# stocks_test = scaler.fit_transform(stocks_test)
loadModelFromFile = True
if not loadModelFromFile:
model = build_lstm_model()
# Training model
tensorboard = TensorBoard(log_dir="logs/{}".format("lstm_test"))
model.fit([train_x1, train_x2], [train_y1, train_y2], verbose=1, epochs = 20, callbacks=[tensorboard])
model.save('save/model.h5')
print("Result saved!")
else:
model = load_model('save/model.h5')
# Creating predictions
# predictions = prediction_invalid(model, [test_x1, test_x2], len(test_x1))[0]
predictions = model.predict([test_x1, test_x2])[0]
#Creating difference graph
difference = []
buy_sell = []
threshold = 0.0035
for i in range(1, len(predictions), 1):
difference.append(predictions[i] - predictions[i-1])
if (abs(difference[-1]) > threshold):
buy_sell.append(round(abs(difference[-1][0])/difference[-1][0]))
else:
buy_sell.append(0)
money_result = [0]
nr_of_stocks = 0
money = 100
actual_price = build_stock_input(data_list = raw_1, input_size = 100, test_ratio = 0.1, step_size = 5)[3]
# You get the result one day in advance!
for i in range(1, len(buy_sell), 1):
if buy_sell[i] > 0:
nr_of_stocks += money / actual_price[i-1]
money = 0
elif buy_sell[i] < 0:
money += nr_of_stocks * actual_price[i-1]
nr_of_stocks = 0
money_result.append(money + nr_of_stocks * actual_price[i])
growth = (money_result[-1] - money_result[1]) / money_result[1]
stock_growth = (actual_price[-1] - actual_price[1]) / actual_price[1]
print("Model start: " + str(money_result[0]))
print("Model end: " + str(money_result[-1]))
print("Stock start: " + str(actual_price[0]))
print("Stock end: " + str(actual_price[-1]))
print("Model growth: " + str(growth))
print("Stock growth: " + str(stock_growth))
subPlots = True
if subPlots:
fig, ax = plt.subplots(nrows=4)
ax[0].set_title("Predicted price (for tomorrow)")
ax[0].plot(predictions)
ax[1].set_title("Buy (1) /Sell (-1)")
ax[1].plot(buy_sell)
ax[2].set_title("Growth (initially 100)")
ax[2].plot(money_result)
ax[3].set_title("Actual data")
ax[3].plot(actual_price)
plt.tight_layout()
else:
plt.plot(predictions)
plt.plot(test_y)
plt.show()
if __name__ == '__main__':
main()