-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_svm.py
278 lines (208 loc) · 7.83 KB
/
gui_svm.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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 26 20:33:00 2023
@author: amit_
"""
"""
CHANGE LINES 93 AND 95 ACCORDING TO DATE
"""
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from datetime import timedelta
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
#global current_price
#current_price = None
#CURRENT PRICE FUNCTION
def get_current_price(stock_name):
stock_data = yf.Ticker(stock_name)
stock_df1 = stock_data.history(period='1y', interval='1d')
return stock_df1['Close'].iloc[-1]
#GENERATING CHARTS FUNCTION
def gen_charts1(stock_name):
#IMPORTING STOCK DATA
stock_data = yf.Ticker(stock_name)
stock_df1 = stock_data.history(period='1y', interval='1d')
#Displaying current price
current_price = get_current_price(stock_name)
current_price = round(current_price, 2)
stock_label = tk.Label(window, text = "Current Price : Rs."+str(current_price))
stock_label.pack()
#Change in datetime
stock_df1.index = pd.to_datetime(stock_df1.index).strftime('%Y-%m-%d')
#FIRST CHART
x1 = stock_df1.index.to_numpy()
y1 = stock_df1['Close'].to_numpy()
#Stock price line plot
fig, (ax1, ax2) = plt.subplots(2,1, sharex=True, figsize=(15,9))
#fig.tight_layout(pad=6.0)
#fig.autofmt_xdate()
#plt.figure(figsize=(10, 10))
#plt.xticks(rotation=45)
ax1.plot(x1,y1)
#plt.xticks(rotation=45)
ax1.set_title('Stock Price')
plt.xticks(np.arange(0, len(stock_df1.index.values), step=8), stock_df1.index.values[::8])
plt.gcf().autofmt_xdate()
plt.show()
#SECOND CHART
x2 = stock_df1.index.to_numpy()
y2=stock_df1['Volume'].to_numpy()
#Volume bar plot
#fig1, ax1 = plt.subplots()
#plt.figure(figsize=(10, 10))
#plt.xticks(rotation=45)
ax2.bar(x2, y2)
#plt.xticks(rotation=45)
ax2.set_title('Volume')
plt.xticks(np.arange(0, len(stock_df1.index.values), step=8), stock_df1.index.values[::8])
plt.gcf().autofmt_xdate()
plt.show()
#plt.xticks(rotation=45)
#plt.subplots_adjust(bottom=0.2)
#SHOW BOTH PLOTS
plt.show()
#creating the Tkinter canvas
#containing the Matplotlib figure
canvas = FigureCanvasTkAgg(fig, window)
canvas.draw()
#placing the canvas on the Tkinter window
canvas.get_tk_widget().pack()
#creating the Matplotlib toolbar
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()
#placing the toolbar on the Tkinter window
canvas.get_tk_widget().pack()
return current_price
def gen_charts2(stock_name):
#IMPORTING STOCK DATA
#stock_name = 'CIPLA.NS'
stock_data = yf.Ticker(stock_name)
stock_df1 = stock_data.history(period='1y', interval='1d')
#Displaying current price
current_price = get_current_price(stock_name)
current_price = round(current_price, 2)
stock_label = tk.Label(window, text="Current Price : Rs." + str(current_price))
stock_label.pack()
#Assigning the variables and reshaping them into a 2-D array
#x=stock_df1.index.values
#y=stock_df1['Close']
x = np.arange(stock_df1.shape[0]).reshape(-1, 1)
y = np.array(stock_df1['Close'])
train_ratio=0.8
train_size = int(len(x) * train_ratio)
x_train, x_test = x[:train_size], x[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
#Training the SVR model
svm_model = SVR(kernel='rbf', gamma = 0.0001, C = 100)
svm_model.fit(x_train, y_train)
#Making predictions for all instances in the test set
predprices_train = svm_model.predict(x_train)
predprices_test = svm_model.predict(x_test)
#Making predictions for the next day(using the last date from the test set)
last_sequence = x_test[-1:]
y_pred = svm_model.predict(last_sequence)
#Inverse transform the predicted value
#y_pred = scaler.inverse_transform(y_pred)
#Evaluating the model on test data
#acc = model.evaluate(x_test, y_test)
r2tr = r2_score(y_train, predprices_train)
#r2tr=avg_r2
#r2t = r2_score(y_test, predprices_test)
mse_train = mean_squared_error(y_train, predprices_train)
#mse_train=avg_mse
mse_test = mean_squared_error(y_test, predprices_test)
#Displaying the loss
r2tr = np.round(r2tr, 3)
#r2t = np.round(r2t, 3)
mse_train = np.round(mse_train, 3)
mse_test = np.round(mse_test, 3)
r2tr_label = tk.Label(window, text = "R2 train score is : "+str(r2tr))
#r2t_label = tk.Label(window, text = "R2 test score Is : "+str(r2t))
mse_train_label = tk.Label(window, text = "Mean Square Error(MSE) on train data is : "+str(mse_train))
mse_test_label = tk.Label(window, text = "Mean Square Error(MSE) on test data is : "+str(mse_test))
r2tr_label.pack()
#r2t_label.pack()
mse_train_label.pack()
mse_test_label.pack()
#Displaying predicted price
#Converting array to numerical value of y_pred
y_pred = y_pred.item()
y_pred = round(y_pred, 2)
pred_label = tk.Label(window, text="Predicted Price : Rs." + str(y_pred))
pred_label.pack()
#CONVERTING THE DATE COLUMN TO DATETIME FORMAT
stock_df1.index = pd.to_datetime(stock_df1.index).strftime('%Y-%m-%d %H:%M:%S')
#get the maximum date in the column
max_date = stock_df1.index.max()
max_date = pd.to_datetime(max_date)
one_day = timedelta(days=1)
#calculate the next date
next_date = max_date + one_day
#ADD THE PREDICTED VALUE TO THE DATAFRAME
stock_df1.at[next_date, "Close"] = y_pred
#CONVERTING TO DATETIME FORMAT AGAIN
stock_df1.index = pd.to_datetime(stock_df1.index).strftime('%Y-%m-%d')
#FINAL PLOT
DF = pd.DataFrame()
DF['value'] = stock_df1['Close'].values
DF = DF.set_index(stock_df1.index.values)
x_values = np.array(DF.index.values)
y_values = np.array(DF['value'])
colors = ['g'] * len(stock_df1.index.values)
colors[-1] = 'r'
fig2, ax2 = plt.subplots(figsize=(17, 9))
ax2.scatter(x_values, y_values, c=colors)
ax2.plot(x_values, y_values, linestyle='-', markersize=5, color='b')
ax2.set_title('Predicted Price (Random Forest)')
plt.xticks(np.arange(0, len(x_values), step=8), x_values[::8])
plt.gcf().autofmt_xdate()
plt.show()
# creating the Tkinter canvas
canvas = FigureCanvasTkAgg(fig2, window)
canvas.draw()
# placing the canvas on the Tkinter window
canvas.get_tk_widget().pack()
# creating the Matplotlib toolbar
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()
# placing the toolbar on the Tkinter window
canvas.get_tk_widget().pack()
#PRESS BUTTON FUNCTION
def but_press1():
stock_name = stock_entry.get()
gen_charts1(stock_name)
def but_press2():
stock_name = stock_entry.get()
gen_charts2(stock_name)
#MAIN WINDOW
window = tk.Tk()
window.title("Price and Volume Charts")
window.geometry("1000x1000")
main_label = tk.Label(window, text="Enter stock name in uppercase followed by '.NS' : ")
#STOCK INPUT
#stock_label = tk.Label(window, text = "")
#stock_label.pack()
main_label.pack()
stock_entry = tk.Entry(window)
stock_entry.pack()
#BUTTONS
generate_button1 = tk.Button(window, text = "Genarate Charts", command = but_press1)
generate_button1.pack()
generate_button2 = tk.Button(window, text = "Predict stock price", command = but_press2)
generate_button2.pack()
#LABEL
#stock_label = tk.Label(window, text = "Current Price : Rs."+str(current_price))
#stock_label.pack()
#label.pack()
#MAIN FUNCTION
window.mainloop()