forked from skrishnan2001/Stock-Price-Predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockPricePredictor.py
211 lines (160 loc) · 6.56 KB
/
StockPricePredictor.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
import streamlit as st
from datetime import date
import yfinance as yf
from fbprophet import Prophet
from fbprophet.plot import plot_plotly
import pandas as pd
from plotly import graph_objs as go
import requests
#import config
import constants
import json
API_KEY = st.secrets["NEWS_API_KEY"]
countries = constants.countries
def business_news_feed():
select_country = st.sidebar.selectbox("Select Country: ", countries.keys())
st.header('NEWS FEED')
r = requests.get('https://newsapi.org/v2/top-headlines?country=' + countries[
select_country] + '&category=business&apikey=' + API_KEY)
data_news = json.loads(r.content)
length = min(15, len(data_news['articles']))
for i in range(length):
news = data_news['articles'][i]['title']
st.subheader(news)
image = data_news['articles'][i]['urlToImage']
try:
st.image(image)
except:
pass
else:
pass
content = data_news['articles'][i]['content']
st.write(content)
url = data_news['articles'][i]['url']
st.write(url)
def isLeapYear(y):
return (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0)
def sideBarHelper(text):
st.sidebar.text(text)
def populateSideBar():
st.sidebar.image(selection.info["logo_url"])
st.sidebar.header(selection.info['shortName'])
sideBarHelper("Sector: " + selection.info['sector'])
sideBarHelper("Financial Currency: " + selection.info['financialCurrency'])
sideBarHelper("Exchange: " + selection.info['exchange'])
sideBarHelper("Timezone: " + selection.info['exchangeTimezoneName'])
def stockPricesToday():
today_data = {'Current Price': [selection.info['currentPrice']],
'Previous Close': [selection.info['previousClose']],
'Open': [selection.info['open']],
'Day Low': [selection.info['dayLow']],
'Day High': [selection.info['dayHigh']]
}
df = pd.DataFrame(today_data)
# priceChangeToday = selection.info['currentPrice'] - data['Close'][len(data) - 1] # Current Price - Previous Closing
col1, col2 = st.columns(2)
priceChangeToday = selection.info['currentPrice'] - selection.info['open'] # Current Price - Previous Closing
col1.metric(label="Current Price, Change w.r.t Opening Price", value='%.2f' % selection.info['currentPrice'],
delta='%.2f' % priceChangeToday)
priceChangeYesterday = data['Close'][len(data) - 1] - data['Close'][len(data) - 2] if len(data) >= 2 else 0
col2.metric(label="Previous Closing, Previous Day Change", value='%.2f' % data['Close'][len(data) - 1],
delta='%.2f' % priceChangeYesterday)
st.dataframe(df)
@st.cache
def load_data(ticker):
historicData = yf.download(ticker, START, TODAY)
historicData.reset_index(inplace=True)
return historicData
def plot_raw_data():
# Plotting the raw data
fig = go.Figure()
fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="stock_open"))
fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="stock_close"))
fig.layout.update(title_text='Time Series data with Rangeslider', xaxis_rangeslider_visible=True)
st.plotly_chart(fig)
fig = go.Figure()
lastFiveDays = data.tail(10)
fig.add_trace(go.Candlestick(x=lastFiveDays['Date'], open=lastFiveDays['Open'], high=lastFiveDays['High'],
low=lastFiveDays['Low'],
close=lastFiveDays['Close']))
fig.layout.update(title_text='Candle Stick Chart - Last 10 Days Trend', xaxis_rangeslider_visible=True)
st.plotly_chart(fig)
def pastTrends():
st.info(selection.info['longBusinessSummary'])
st.subheader('Today')
stockPricesToday()
st.subheader('Last 5 Days Trend')
st.write(data.tail())
def predictingTheStockPrices():
period = 0
n_years = st.slider('Years of prediction:', 1, 4)
for i in range(0, n_years):
if isLeapYear(year + i):
period += 366
else:
period += 365
df_train = data[['Date', 'Close']]
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})
df_train['ds'] = df_train['ds'].dt.tz_convert(None)
model_param = {
"daily_seasonality": False,
"weekly_seasonality": False,
"yearly_seasonality": True,
"seasonality_mode": "multiplicative",
"growth": "logistic"
}
m = Prophet(**model_param)
m = m.add_seasonality(name="monthly", period=30, fourier_order=10)
m = m.add_seasonality(name="quarterly", period=92.25, fourier_order=10)
df_train['cap'] = df_train["y"].max() + df_train["y"].std() * 0.05
m.fit(df_train)
future = m.make_future_dataframe(periods=period)
future['cap'] = df_train['cap'].max()
forecast = m.predict(future)
# Showing and plotting the forecast
st.subheader('Forecast data')
st.write(forecast)
st.write(f'Forecast plot for {n_years} years')
fig1 = plot_plotly(m, forecast)
st.plotly_chart(fig1)
st.write("Forecast components - Yearly, Monthly and Quarterly Trends")
fig2 = m.plot_components(forecast)
st.write(fig2)
# Driver
START = "2015-01-01"
TODAY = date.today().strftime("%Y-%m-%d")
year = int(TODAY[: 4])
st.title('STOCK FORECAST APP')
try:
option = st.sidebar.selectbox("Which Dashboard?", ('Past Trends', 'Predict Stock Price', 'Trending Business News'),
0)
stock = st.sidebar.text_input("Symbol", value='GOOG')
# selected_stock = st.selectbox('Select dataset for prediction', stocks)
# stocks = listOfStockSymbols()
selected_stock = stock
data = load_data(selected_stock)
selection = yf.Ticker(selected_stock)
if option == 'Past Trends':
company_name = selection.info['longName']
st.subheader(company_name + "'s Stocks")
populateSideBar()
pastTrends()
plot_raw_data()
if option == 'Predict Stock Price':
# Predicting the forecast with Prophet.
company_name = selection.info['longName']
st.subheader(company_name + "'s Stocks")
populateSideBar()
predictingTheStockPrices()
if option == 'Trending Business News':
business_news_feed()
except KeyError:
st.error('This company is not listed !')
except FileNotFoundError:
st.error('No data is available about this stock !')
except TypeError:
st.error('No data is available about this stock !')
except ValueError:
st.error('Symbol cannot be empty !')
except ConnectionError:
st.error('Could not connect to the internet :(')