-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapp.py
222 lines (197 loc) · 8.93 KB
/
webapp.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
import streamlit as st
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from sklearn.preprocessing import MinMaxScaler
from keras.layers import LSTM, Dense, Dropout
from sklearn.model_selection import TimeSeriesSplit
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import MinMaxScaler
from sklearn import linear_model
from keras.models import Sequential
from keras.layers import Dense, LSTM
import keras.backend as K
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from keras.models import load_model
from datetime import datetime, timedelta
import joblib
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import load_model
def get_stock_data(stock_symbol, start_date, end_date):
data = yf.download(stock_symbol, start=start_date, end=end_date)
date_range = pd.date_range(start=start_date, end=end_date, freq='D')
data = data.reindex(date_range)
data['Date'] = data.index
data.fillna(method='ffill', inplace=True)
stock_info = yf.Ticker(stock_symbol).info
stock_name = stock_info.get('longName', 'N/A')
return data,stock_name
def calculate_7d_average(data):
return np.mean(data['Close'].tail(7))
def calculate_7d_high(data):
return np.max(data['High'].tail(7))
def calculate_7d_low(data):
return np.min(data['Low'].tail(7))
def train_lstm_model(data):
output_var = pd.DataFrame(data['Adj Close'])
features = ['Open']
scaler = MinMaxScaler()
feature_transform = scaler.fit_transform(data[features])
feature_transform = pd.DataFrame(columns=features, data=feature_transform, index=data.index)
timesplit = TimeSeriesSplit(n_splits=10)
for train_index, test_index in timesplit.split(feature_transform):
X_train, X_test = feature_transform[:len(train_index)], feature_transform[len(train_index): (len(train_index) + len(test_index))]
y_train, y_test = output_var[:len(train_index)].values.ravel(), output_var[len(train_index): (len(train_index) + len(test_index))].values.ravel()
trainX = np.array(X_train)
testX = np.array(X_test)
X_train = trainX.reshape(X_train.shape[0], 1, X_train.shape[1])
X_test = testX.reshape(X_test.shape[0], 1, X_test.shape[1])
lstm = Sequential()
lstm.add(LSTM(32, input_shape=(1, trainX.shape[1]), return_sequences=False))
lstm.add(Dense(1))
lstm.compile(loss='mean_squared_error', optimizer='adam')
history = lstm.fit(X_train, y_train, epochs=100, batch_size=8, verbose=1, shuffle=False)
return lstm, scaler
def main():
st.set_page_config(
page_title="Stockify!",
page_icon=":chart_with_upwards_trend:",
)
st.write("""
# Stockify! :chart_with_upwards_trend:
### Stock Prediction Model using LSTM
This app uses an LSTM (Long Short-Term Memory) model to predict stock prices based on historical data.
You can enter the stock symbol, start date, and end date to analyze the historical stock data and make predictions.
""")
#Hiding the watermark
hide_streamlit_style = """
<style>
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
stock_symbols = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'META'] # Add more stock symbols as needed
stock_symbol = st.selectbox('Select the stock symbol:', stock_symbols)
start_date = st.date_input('Select the start date:',datetime.now() - timedelta(days=3652))
end_date = st.date_input('Select the end date:')
if stock_symbol and start_date and end_date:
loaded_model = load_model(f'./models/{stock_symbol}_model.h5')
scaler = joblib.load(f'./models/{stock_symbol}_scaler.joblib')
stock_data = yf.Ticker(stock_symbol)
latest_data = stock_data.history(period='1d')
openp = latest_data['Open'][0]
scaledopenp = scaler.fit_transform([[openp]])
scaledopenp = scaledopenp.reshape(1, 1, scaledopenp.shape[1])
expectedclosep = loaded_model.predict(scaledopenp)
expectedclosep = scaler.inverse_transform(expectedclosep)
st.subheader('Model Prediction')
action_text = '<div style="text-align:center; margin-top: 1px;font-size:24px">'
if expectedclosep[0, 0] > openp:
action_text += f'''<strong>Latest Stock Price for {stock_symbol}:</strong> ${openp:.2f}<br>
<strong>Predicted Close Price for {stock_symbol}:</strong> ${expectedclosep[0, 0]:.2f}<br>
<span style="color:green">Go Long (Buy)</span>'''
else:
action_text += f'''<strong>Latest Stock Price for {stock_symbol}:</strong> ${openp:.2f}<br>
<strong>Predicted Close Price for {stock_symbol}:</strong> ${expectedclosep[0, 0]:.2f}<br>
<span style="color:red">Go Short (Sell)</span>'''
action_text += '</div>'
st.markdown(
f'<div style="border: 2px solid #4CAF50; border-radius: 10px; padding: 15px;">{action_text}</div>',
unsafe_allow_html=True
)
tradingview_widget = """
<!-- TradingView Widget BEGIN -->
<div class="tradingview-widget-container">
<div class="tradingview-widget-container__widget"></div>
<div class="tradingview-widget-copyright"><a href="https://www.tradingview.com/" rel="noopener nofollow" target="_blank"><span class="blue-text">Track all markets on TradingView</span></a></div>
<script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-ticker-tape.js" async>
{
"symbols": [
{
"description": "",
"proName": "NASDAQ:AAPL"
},
{
"description": "",
"proName": "NASDAQ:MSFT"
},
{
"description": "",
"proName": "NASDAQ:GOOGL"
},
{
"description": "",
"proName": "NASDAQ:AMZN"
},
{
"description": "",
"proName": "NASDAQ:META"
}
],
"showSymbolLogo": true,
"colorTheme": "dark",
"isTransparent": true,
"displayMode": "adaptive",
"locale": "en"
}
</script>
</div>
<!-- TradingView Widget END -->
"""
st.components.v1.html(tradingview_widget)
stock_data = yf.Ticker(stock_symbol)
latest_data = stock_data.history(period='1d')
data_24h = stock_data.history(period='1d')
data_7d = stock_data.history(period='7d')
current_price = latest_data['Close'].iloc[-1]
high_24h = data_24h['High'].max()
low_24h = data_24h['Low'].min()
volume_24h = data_24h['Volume'].iloc[-1]
average_7d = calculate_7d_average(data_7d)
high_7d = calculate_7d_high(data_7d)
low_7d = calculate_7d_low(data_7d)
volume_7d = data_7d['Volume'].sum()
st.subheader('Stock Details')
col1, col2 = st.columns(2)
with col1:
st.markdown(
f'''
<div style="center: 2px solid #4CAF50; border-radius: 10px; padding: 15px;">
<p><strong>24H High:</strong> ${high_24h:.2f}</p>
<p><strong>24H Low:</strong> ${low_24h:.2f}</p>
<p><strong>24H Volume:</strong> ${volume_24h}</p>
</div>
''',
unsafe_allow_html=True
)
with col2:
st.markdown(
f'''
<div style="center: 2px solid #4CAF50; border-radius: 10px; padding: 15px;">
<p><strong>7D High:</strong> ${high_7d:.2f}</p>
<p><strong>7D Low:</strong> ${low_7d:.2f}</p>
<p><strong>7D Volume:</strong> ${volume_7d}</p>
</div>
''',
unsafe_allow_html=True
)
data,stock_name = get_stock_data(stock_symbol, start_date, end_date)
st.subheader(f'Stock Data of {stock_name} (${stock_symbol.upper()})')
st.write(data)
st.subheader('Adjusted Close Price')
st.line_chart(data['Adj Close'])
st.markdown(
"""
<div style="text-align:center; margin-top: 50px;">
<span style="font-size: 18px;">Made with ❤️ by Sashank Ravipati</span> <br>
<a href="https://www.linkedin.com/in/sashankravipati/" target="_blank"><img src="https://img.shields.io/badge/-LinkedIn-blue?style=flat-square&logo=Linkedin&logoColor=white" height="24"></a>
<a href="https://github.com/rsashank" target="_blank"><img src="https://img.shields.io/badge/-GitHub-black?style=flat-square&logo=GitHub&logoColor=white" height="24"></a>
</div>
""",
unsafe_allow_html=True
)
if __name__ == '__main__':
main()