-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshare_price_predictor.py
More file actions
executable file
·146 lines (126 loc) · 4.39 KB
/
share_price_predictor.py
File metadata and controls
executable file
·146 lines (126 loc) · 4.39 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
#!/usr/bin/env python
import os
from io import StringIO
import requests
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense, Dropout, LSTM
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
def getData(script_code, start, end):
base_url = "https://api.bseindia.com/BseIndiaAPI/api/"
user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv :94.0)\
Gecko/20100101 Firefox/94.0"
price_endpoint = base_url + "StockPriceCSVDownload/w"
info_endpoint = base_url + "ListofScripData/w"
price_params = {
"pageType" : "0",
"rbType" : "D",
"Scode" : str(script_code),
"FDates" : str(start),
"TDates" : str(end),
}
info_params = { "Scripcode" : str(script_code) }
headers = {
"Host" : "api.bseindia.com",
"User-Agent" : user_agent,
}
csv_data = requests.get(price_endpoint,
params=price_params,
headers=headers)
info = requests.get(info_endpoint,
params=info_params,
headers=headers)
name = info.json()[0]["Scrip_Name"][:-1]
return name, StringIO(csv_data.text)
start = dt.datetime(2020,1,1)
end = dt.datetime.now()
if int(end.strftime("%H")) < 16:
end -= dt.timedelta(days=1)
start = start.strftime("%d/%m/%Y")
end = end.strftime("%d/%m/%Y")
script_code = input("Enter script code : ")
if not script_code : script_code = '500209'
try:
company, daily = getData(script_code,start,end)
daily = pd.read_csv(daily)
except requests.exceptions.RequestException:
if 'y' in input("Read from file :").lower():
daily = pd.read_csv(script_code + '.csv')
company = input("Enter company name : ")
else:
quit()
daily["Date"] = pd.to_datetime(daily["Date"], format="%d-%B-%Y")
daily.sort_values(by = ["Date"])
daily.set_index("Date", inplace = True)
data = daily.head(len(daily) - 100)
prediction_days = 60
scaler = MinMaxScaler(feature_range=(0,1))
train_data = data['Close Price'].values.reshape(-1,1)
scaled_data = scaler.fit_transform(train_data)
def train_model():
x_train = []
y_train = []
for x in range(prediction_days, len(scaled_data)):
x_train.append(scaled_data[x-prediction_days:x, 0])
y_train.append(scaled_data[x, 0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train,
(x_train.shape[0], x_train.shape[1], 1)
)
model = Sequential()
model.add(LSTM(units=50,
return_sequences=True,
input_shape=(x_train.shape[1], 1))
)
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=1))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(x_train, y_train, epochs=25, batch_size=32)
model.save(f'{script_code}_model.hdf5')
return model
try:
model = load_model(f'{script_code}_model.hdf5')
except Exception as e:
print(e)
print("Training model :")
model = train_model()
test_data = daily.tail(100)
actual_prices = test_data['Close Price'].values
total_dataset = pd.concat(
(data['Close Price'], test_data['Close Price']),
axis=0)
model_inputs = total_dataset[len(total_dataset)
- len(test_data) - prediction_days:].values
model_inputs = model_inputs.reshape(-1, 1)
model_inputs = scaler.transform(model_inputs)
x_test = []
for x in range(prediction_days, len(model_inputs) + 1):
x_test.append(model_inputs[x-prediction_days:x, 0])
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
predicted_prices = model.predict(x_test)
predicted_prices = scaler.inverse_transform(predicted_prices)
next_day_price = round(float(predicted_prices[-1]), 2)
print(f"Next day's {company} price : {next_day_price}")
fig = plt.figure()
fig.patch.set_facecolor('black')
fig.patch.set_alpha(0.6)
ax = fig.add_subplot(111)
ax.patch.set_facecolor('#2d2d2d')
ax.patch.set_alpha(1.0)
plt.plot(actual_prices, color='#5567d5',
label=f"Actual {company} Price")
plt.plot(predicted_prices, color='#55d567',
label=f"Predicted {company} Price")
plt.title(f"{company} Share Price \u20b9")
plt.ylabel('Price')
plt.legend()
plt.show()