-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_model.py
More file actions
158 lines (118 loc) · 5.67 KB
/
predict_model.py
File metadata and controls
158 lines (118 loc) · 5.67 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
import pandas as pd
import yfinance as yf
from yahoo_earnings_calendar import YahooEarningsCalendar
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.losses import MeanSquaredError
from sklearn.metrics import mean_squared_error
from pytanggalmerah import TanggalMerah
import pandas as pd
import numpy as np
import math
import datetime
import json
def predict_model(kode_saham, algoritma):
tanggal_merah = TanggalMerah(cache_path = None, cache_time = 600) # cache_path = None berarti directory cache automatis
# Fetch the data
ticker = yf.Ticker(kode_saham)
csv_data = ticker.history(period="3mo")
csv_data.head()
csv_data['Date'] = csv_data.index
graph_date = list(csv_data['Date'].values.flat)
tanggal_merah = TanggalMerah(cache_path = None, cache_time = 600) # cache_path = None berarti directory cache automatis
last_date = (pd.to_datetime(str(graph_date[-1])) + datetime.timedelta(days = 1))
last_date = (pd.to_datetime(str(last_date)) + datetime.timedelta(days = 1)) if last_date == datetime.date.today() else last_date
start_date = (pd.to_datetime(str(graph_date[-1])) + datetime.timedelta(days = 1))
current_year = datetime.date.today().strftime("%Y")
current_month = datetime.date.today().strftime("%m")
current_day_date = datetime.date.today().strftime("%d")
current_day = datetime.date.today().strftime("%A")
tanggal_merah.set_date(current_year, current_month, current_day_date)
start_date = start_date if (start_date == datetime.date.today()) else (pd.to_datetime(str(start_date)) - datetime.timedelta(days = 1))
start_date = datetime.date.today() if(tanggal_merah.check() or tanggal_merah.is_holiday() or tanggal_merah.is_sunday() or current_day == "Saturday") else start_date
initial_date = "2017-01-01"
current_date = "2023-06-01" #last_date.strftime("%Y-%m-%d") #datetime.date.today()
current_year = last_date.strftime("%Y")
current_month = last_date.strftime("%m")
current_day_date = last_date.strftime("%d")
ticker = kode_saham
csv_data = yf.download(ticker, initial_date, last_date)
csv_data.head()
# Creating the data
csv_data['TradeDate'] = csv_data.index
full_data = csv_data[['Close']].values
# print(full_data[-15:])
# Choosing between Standardization or normalization
sc = MinMaxScaler()
data_scaler = sc.fit(full_data)
x = data_scaler.transform(full_data)
# Making predictions on test data
actual_fifteen = full_data[-22:-7]
actual_seven = full_data[-7:]
# Reshaping the data to (-1, 1) because its a single entry
actual_fifteen = actual_fifteen.reshape(-1, 1)
# Scaling the data on the same level on which model was trained
x_test = data_scaler.transform(actual_fifteen)
n_sample = 1
time_step = x_test.shape[0]
n_feature = x_test.shape[1]
# Reshaping the data as 3D input
x_test = x_test.reshape(n_sample, time_step, n_feature)
# Finding the model according to the path and the model's name
regressor = keras.models.load_model('./model/' + kode_saham.replace('.', '').lower() + '_training_' + algoritma + '_model_' + current_date + '.h5')
# Generating the predictions for next 7 days
predicted_seven = regressor.predict(x_test)
# Generating the prices in original scale
predicted_seven = data_scaler.inverse_transform(predicted_seven)
rmse = np.sqrt(np.mean(((predicted_seven - actual_seven) ** 2)))
# Making predictions on test data
actual_fifteen = full_data[-15:]
# Reshaping the data to (-1, 1) because its a single entry
actual_fifteen = actual_fifteen.reshape(-1, 1)
# Scaling the data on the same level on which model was trained
x_test = data_scaler.transform(actual_fifteen)
n_sample = 1
time_step = x_test.shape[0]
n_feature = x_test.shape[1]
# Reshaping the data as 3D input
x_test = x_test.reshape(n_sample, time_step, n_feature)
# Generating the predictions for next 7 days
predicted_seven = regressor.predict(x_test)
# Generating the prices in original scale
predicted_seven = data_scaler.inverse_transform(predicted_seven)
list_actual = list(actual_seven.flat)
list_predict = list(predicted_seven.flat)
list_actual = [float(i) for i in list_actual]
#list_predict = [float(i) for i in list_predict]
list_date = []
list_predict_date = []
i = 0
while i < 7:
start_date = (pd.to_datetime(str(start_date)) + datetime.timedelta(days = 1))
current_year = start_date.strftime("%Y")
current_month = start_date.strftime("%m")
current_day_date = start_date.strftime("%d")
current_day = start_date.strftime("%A")
tanggal_merah.set_date(current_year, current_month, current_day_date)
if(tanggal_merah.check() or tanggal_merah.is_holiday() or tanggal_merah.is_sunday() or current_day == "Saturday"):
i += 0
else:
list_date.append(start_date.strftime("%Y-%m-%d"))
i += 1
for i, j in enumerate(list_predict):
list_predict_date.append({
"tanggal": list_date[i],
"prediksi_harga_penutupan": json.dumps(float(j))
})
datetime.date.today().strftime("%Y-%m-%d")
return {
"harga_penutupan_sebelumnya": json.dumps(float(full_data[-2])),
"harga_penutupan_saat_ini": json.dumps(float(full_data[-1])),
"prediksi": list_predict_date,
"rmse": rmse
}