-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction.py
71 lines (54 loc) · 2.07 KB
/
prediction.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
import streamlit as st
import yfinance as yf
from datetime import date
from prophet import Prophet
from prophet.plot import plot_plotly
from plotly import graph_objs as go
START = "2017-01-01"
TODAY = date.today().strftime("%Y-%m-%d")
st.title('Stock Prediction Web App')
stocks = st.text_input("Enter stock name")
n_days = st.number_input('Number of Days of Prediction:', min_value=1, value=30)
st.info("Kindly note that the application will present the stock value in the local currency of the selected stock.")
def load_data(ticker):
try:
data = yf.download(ticker, START, TODAY)
if data.empty:
st.error("Error: No data found for the specified stock.")
return None
data.reset_index(inplace=True)
return data
except Exception as e:
st.error(f"Error fetching data: {str(e)}")
return None
data = load_data(stocks)
if data is not None:
st.subheader('Raw Data')
st.write(data.tail())
def plot_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)
fig.update_xaxes(title_text='Date')
fig.update_yaxes(title_text='Close Price')
st.plotly_chart(fig)
plot_raw_data()
df_train = data[['Date', 'Close']]
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})
m = Prophet()
m.fit(df_train)
future = m.make_future_dataframe(periods=n_days, freq='D')
forecast = m.predict(future)
st.subheader('Forecast Data')
st.write(forecast.tail())
fig1 = plot_plotly(m, forecast)
fig1.layout.update(title_text=f'Forecast plot for {n_days} days', xaxis_rangeslider_visible=True)
fig1.update_layout(
xaxis_title='Date',
yaxis_title='Close Price'
)
st.plotly_chart(fig1)
st.write("Forecast Components")
fig2 = m.plot_components(forecast)
st.write(fig2)