-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
103 lines (84 loc) · 3.87 KB
/
main.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
import streamlit as st
from datetime import date
import numpy as np
import yfinance as yf
from prophet import Prophet
from prophet.plot import plot_plotly
from plotly import graph_objs as go
START = "2015-01-01" # Define START date
TODAY = date.today().strftime("%Y-%m-%d") # Define TODAY date
# Documentation and Help Section
st.sidebar.title('Help & Documentation')
st.sidebar.write("Welcome to the Stock Forecast App!")
st.sidebar.write("To use the app, select a stock from the dropdown menu, choose the forecast period using the slider, and view the forecast plot and components.")
st.sidebar.write("Please note that the forecasts provided are based on historical data and may not accurately predict future stock prices. Use them for informational purposes only.")
# Feedback Mechanism
st.sidebar.title('Feedback & Support')
feedback_form = st.sidebar.form(key='feedback_form')
feedback_text = feedback_form.text_area('Share your feedback or report an issue:', height=150)
email_input = feedback_form.text_input('Your email (optional):')
submit_button = feedback_form.form_submit_button(label='Submit Feedback')
if submit_button:
if feedback_text.strip() == '':
st.sidebar.error("Feedback cannot be empty. Please provide your feedback.")
else:
# Send feedback to a designated email address or store it in a database
st.sidebar.success("Thank you for your feedback! We'll review it and take necessary actions.")
if email_input.strip() != '':
# Send email notification if email is provided
st.sidebar.write(f"A confirmation email has been sent to {email_input}.")
st.title('Stock Forecast App')
# List of major Nasdaq stocks
stocks = (
'AAPL', 'MSFT', 'GOOGL', 'GOOG', 'AMZN', 'TSLA', 'META', 'NVDA', 'PYPL', 'CMCSA',
'PEP', 'ADBE', 'NFLX', 'INTC', 'CSCO', 'AVGO', 'TXN', 'QCOM', 'COST', 'HON',
'AMGN', 'SBUX', 'AMD', 'ISRG', 'BKNG', 'MDLZ', 'ADI', 'MU', 'LRCX', 'GILD',
'KHC', 'WDAY', 'BIDU', 'DOCU', 'ZM', 'PDD', 'MELI', 'ABNB', 'CRWD', 'DDOG'
)
selected_stock = st.selectbox('Select dataset for prediction', stocks)
# Update the slider to allow up to 10 years of prediction
n_years = st.slider('Years of prediction:', 1, 10)
period = n_years * 365
@st.cache_data
def load_data(ticker):
try:
data = yf.download(ticker, START, TODAY)
data.reset_index(inplace=True)
return data
except Exception as e:
st.error(f"Failed to fetch data for {ticker}. Error: {str(e)}")
return None
data_load_state = st.text('Loading data...')
data = load_data(selected_stock)
if data is not None:
data_load_state.text('Loading data... done!')
st.subheader('Raw data')
st.write(data.tail())
# Plot raw data
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)
st.plotly_chart(fig)
plot_raw_data()
# Predict forecast with Prophet.
@st.cache_resource
def predict_forecast(df_train, period):
m = Prophet()
m.fit(df_train)
future = m.make_future_dataframe(periods=period)
forecast = m.predict(future)
return m, forecast
df_train = data[['Date', 'Close']]
df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})
m, forecast = predict_forecast(df_train, period)
# Show and plot forecast
st.subheader('Forecast data')
st.write(forecast.tail())
st.write(f'Forecast plot for {n_years} years')
fig1 = plot_plotly(m, forecast)
st.plotly_chart(fig1)
st.write("Forecast components")
fig2 = m.plot_components(forecast)
st.write(fig2)