-
Notifications
You must be signed in to change notification settings - Fork 2
/
closep_regression.py
341 lines (276 loc) · 12.4 KB
/
closep_regression.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import matplotlib.pyplot as plt
from ebcli.lib.utils import urllib
from matplotlib.pylab import gca
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import json
import numpy as np
import pymysql
from matplotlib.lines import Line2D
import fit
import segment
plt.matplotlib.rcParams.update({'font.size': 9})
def get_angular_coefficient(segment_set, index):
"""
It returns the angular coefficient of the segment set
:param: segment expressed as a couple of points with two coordinates x and y
:return: the angular coefficient
"""
current_segment = segment_set[index]
coeff_angular = (current_segment[3]-current_segment[1])/(current_segment[2]-current_segment[0])
return coeff_angular
def get_constant_term(segment_set, index):
"""
It returns the constant term of the segment set
:param: segment expressed as a couple of points with two coordinates x and y
:return: the angular coefficient
:return:
"""
current_segment = segment_set[index]
coeff_constant_term = (current_segment[2]*current_segment[1]-current_segment[0]*current_segment[3])/\
(current_segment[2]-current_segment[0])
return coeff_constant_term
def evaluate_global_error(data, segment_set):
"""
:param data: the set of close price data
:param segment_set: the set of segments
:return: the global error
"""
current_segment = []
total_error = 0
for i in range(0, len(segment_set)):
error = 0
current_angular_coeff = get_angular_coefficient(segment_set, i)
current_constant_term = get_constant_term(segment_set, i)
# The equation of the line is y = current_angular_coeff * x + current_constant_term
for j in range((segment_set[i])[0], (segment_set[i])[2]):
y = current_angular_coeff * j + current_constant_term
current_segment.append(y)
error += abs(y - data[j])
total_error += error
return total_error
def connection_to_db(path):
"""
It opens the connection with the database by parsing the json file in which database credentials are listed
:param path: path of the database details (i.e. username, password, ...)
:return: the connection to the database
"""
with open(path) as data_file:
json_data = json.load(data_file)
connection = pymysql.connect(host=json_data['HOST'],
user=json_data['USER'],
password=json_data['PASSWORD'],
db=json_data['DB_NAME'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return connection
def fetch_data_from_db(db, company):
"""
All data contanining the stock price info are retrieved from the database given the stock name
:param db: connection name
:param company: company name
:return: the list of data just fetched
"""
cur = db.cursor()
cur.execute("SELECT * FROM Stock_price WHERE company = %s", company)
query_result = cur.fetchall()
# list of dates
result = []
for i in query_result:
date_formatted = (str(i['date_stock'])[0:10]).split("-")
result.append(date_formatted[0]+""+date_formatted[1]+""+date_formatted[2]+","+str(i['close_price']))
return result
def draw_window(my_dpi, data, max_error):
"""
All data contanining the stock price info are retrieved from the database given the stock name
:param my_dpi: dpi screen
:param data: data to be plot
:param max_error: maximum error allowed
"""
fig = plt.figure(figsize=(1000/my_dpi, 700/my_dpi), dpi=96, facecolor='black')
fig.suptitle("PIECEWISE SEGMENTATION REGRESSION", fontsize="15", color="white", fontweight='bold', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
try:
stockFile = []
try:
for eachLine in data:
splitLine = eachLine.split(',')
if len(splitLine) == 2:
if 'values' not in eachLine:
stockFile.append(eachLine)
except Exception as e:
print(str(e), 'failed to organize pulled data.')
except Exception as e:
print(str(e), 'failed to pull pricing data')
try:
date, closep_raw = np.loadtxt(stockFile, delimiter=',', unpack=True,
converters={0: mdates.bytespdate2num('%Y%m%d')})
closep = closep_raw[::-1]
print(max(closep))
max_error = max(closep)*2.5;
# First subplot
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
segments = segment.slidingwindowsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep,plt,ax1,"Sliding window with regression")
draw_segments(segments,'red')
plt.ylabel('Stock Price')
plt.title("SLIDING WINDOW - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')
# Second subplot
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3)
segments = segment.topdownsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep, plt, ax2, "Sliding window with regression")
draw_segments(segments,'green')
plt.ylabel('Stock Price')
plt.title("TOP DOWN - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')
# Third subplot
ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
segments = segment.bottomupsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep, plt, ax3, "Sliding window with regression")
draw_segments(segments,'blue')
plt.ylabel('Stock Price')
plt.title("BOTTOM UP - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')
plt.subplots_adjust(hspace=0.3)
plt.show()
except e:
print("Error")
def draw_window(my_dpi, data):
"""
All data contanining the stock price info are retrieved from the database given the stock name
:param my_dpi: dpi screen
:param data: data to be plot
:param max_error: maximum error allowed
"""
fig = plt.figure(figsize=(1000/my_dpi, 700/my_dpi), dpi=96, facecolor='black')
fig.suptitle("PIECEWISE SEGMENTATION REGRESSION", fontsize="15", color="white", fontweight='bold', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
try:
stockFile = []
try:
for eachLine in data:
splitLine = eachLine.split(',')
if len(splitLine) == 2:
if 'values' not in eachLine:
stockFile.append(eachLine)
except Exception as e:
print(str(e), 'failed to organize pulled data.')
except Exception as e:
print(str(e), 'failed to pull pricing data')
try:
date, closep_raw = np.loadtxt(stockFile, delimiter=',', unpack=True,
converters={0: mdates.bytespdate2num('%Y%m%d')})
closep = closep_raw[::-1]
max_closep = max(closep)
if(max_closep > 2.0):
max_error = max(closep)*2.7;
else:
max_error = max(closep)/2.5;
print(max_error)
# First subplot
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
segments = segment.slidingwindowsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep,plt,ax1,"Sliding window with regression")
draw_segments(segments,'red')
plt.ylabel('Stock Price')
plt.title("SLIDING WINDOW - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')
# Second subplot
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3)
segments = segment.topdownsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep, plt, ax2, "Sliding window with regression")
draw_segments(segments,'green')
plt.ylabel('Stock Price')
plt.title("TOP DOWN - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')
# Third subplot
ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
segments = segment.bottomupsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep, plt, ax3, "Sliding window with regression")
draw_segments(segments,'blue')
plt.ylabel('Stock Price')
plt.title("BOTTOM UP - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')
plt.subplots_adjust(hspace=0.3)
plt.show()
except e:
print("Error")
def draw_plot(data, plt,ax,plot_title):
ax.plot(range(len(data)), data, alpha=0.8, color='black')
ax.grid(True, color='#969696')
ax.yaxis.label.set_color("w")
ax.xaxis.label.set_color("w")
ax.tick_params(axis='y', colors='w')
ax.tick_params(axis='x', colors='w')
plt.ylabel('Stock Price')
plt.title("Sliding window", color='w')
plt.title(plot_title)
plt.xlim((0, len(data)-1))
def draw_segments(segments,color):
ax = gca()
for segment in segments:
line = Line2D((segment[0],segment[2]),(segment[1],segment[3]),color=color)
ax.add_line(line)
def draw_window_API(my_dpi, max_error, stockToFetch):
"""
All data contanining the stock price info are retrieved from the database given the stock name
:param my_dpi: dpi screen
:param data: data to be plot
:param max_error: maximum error allowed
"""
fig = plt.figure(figsize=(1000/my_dpi, 700/my_dpi), dpi=96, edgecolor='k', facecolor='black')
fig.suptitle("PIECEWISE SEGMENTATION REGRESSION", fontsize="15", color="white", fontweight='bold', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
try:
print('Currently Pulling',stockToFetch)
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stockToFetch+'/chartdata;type=quote;range=5y/csv'
stockFile =[]
try:
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine) == 6:
if 'values' not in eachLine:
stockFile.append(eachLine)
except Exception as e:
print(str(e), 'failed to organize pulled data.')
except Exception as e:
print(str(e), 'failed to pull pricing data')
try:
date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=',', unpack=True,
converters={0: mdates.bytespdate2num('%Y%m%d')})
SP = len(date)
# First subplot
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
segments = segment.slidingwindowsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep,plt,ax1,"Sliding window with regression")
draw_segments(segments,'red')
plt.ylabel('Stock Price')
plt.title("Sliding window", color='w')
# Second subplot
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3)
segments = segment.topdownsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep, plt, ax2, "Sliding window with regression")
draw_segments(segments,'green')
plt.ylabel('Stock Price')
plt.title("Top down", color='w')
# Third subplot
ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
segments = segment.bottomupsegment(closep, fit.regression, fit.sumsquared_error, max_error)
draw_plot(closep, plt, ax3, "Sliding window with regression")
draw_segments(segments,'blue')
plt.ylabel('Stock Price')
plt.title("Bottom up", color='w')
plt.subplots_adjust(hspace=0.3)
plt.show()
except e:
print("Error")
if __name__ == '__main__':
"""
CONSTANTS
"""
MY_DPI = 96
PATH_AWS_DB = 'resources/AWS_DB_details.json'
# Connection to the database
connection = connection_to_db(PATH_AWS_DB)
# Data is fetched from db
stock = input("Stock name: ")
# err = input("Max error: ")
res = fetch_data_from_db(connection, stock)
# Figure is built
draw_window(MY_DPI, res)
# draw_window(MY_DPI, res, float(err))
# draw_window_API(MY_DPI, float(err), stock)