Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from datetime import date
|
3 |
import yfinance as yf
|
4 |
-
from prophet import Prophet
|
5 |
from prophet.plot import plot_plotly
|
6 |
from plotly import graph_objs as go
|
7 |
|
@@ -10,11 +10,11 @@ START = "2015-01-01"
|
|
10 |
TODAY = date.today().strftime("%Y-%m-%d")
|
11 |
|
12 |
# Streamlit app title
|
13 |
-
st.title('Stock Forecast App')
|
14 |
|
15 |
-
# Stock selection
|
16 |
-
|
17 |
-
|
18 |
|
19 |
# Years of prediction slider
|
20 |
n_years = st.slider('Years of prediction:', 1, 4)
|
@@ -22,25 +22,29 @@ period = n_years * 365
|
|
22 |
|
23 |
@st.cache_data
|
24 |
def load_data(ticker):
|
25 |
-
"""Load stock data from Yahoo Finance."""
|
26 |
data = yf.download(ticker, START, TODAY)
|
27 |
data.reset_index(inplace=True)
|
28 |
return data
|
29 |
|
30 |
# Load data and show loading state
|
31 |
data_load_state = st.text('Loading data...')
|
32 |
-
data = load_data(
|
33 |
data_load_state.text('Loading data... done!')
|
34 |
|
35 |
# Display raw data
|
36 |
st.subheader('Raw data')
|
37 |
st.write(data.tail())
|
38 |
|
|
|
|
|
|
|
|
|
39 |
# Plot raw data function
|
40 |
def plot_raw_data():
|
41 |
fig = go.Figure()
|
42 |
-
fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="
|
43 |
-
fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="
|
44 |
fig.layout.update(title_text='Time Series Data with Rangeslider', xaxis_rangeslider_visible=True)
|
45 |
st.plotly_chart(fig)
|
46 |
|
@@ -61,7 +65,7 @@ forecast = m.predict(future)
|
|
61 |
|
62 |
# Show forecast data and plot forecast
|
63 |
st.subheader('Forecast data')
|
64 |
-
st.write(forecast.tail())
|
65 |
st.write(f'Forecast plot for {n_years} years')
|
66 |
|
67 |
fig1 = plot_plotly(m, forecast)
|
|
|
1 |
import streamlit as st
|
2 |
from datetime import date
|
3 |
import yfinance as yf
|
4 |
+
from prophet import Prophet
|
5 |
from prophet.plot import plot_plotly
|
6 |
from plotly import graph_objs as go
|
7 |
|
|
|
10 |
TODAY = date.today().strftime("%Y-%m-%d")
|
11 |
|
12 |
# Streamlit app title
|
13 |
+
st.title('Stock & Cryptocurrency Forecast App')
|
14 |
|
15 |
+
# Stock and cryptocurrency selection
|
16 |
+
assets = ('GOOG', 'AAPL', 'MSFT', 'GME', 'BTC-USD', 'ETH-USD') # Added BTC and ETH
|
17 |
+
selected_asset = st.selectbox('Select dataset for prediction', assets)
|
18 |
|
19 |
# Years of prediction slider
|
20 |
n_years = st.slider('Years of prediction:', 1, 4)
|
|
|
22 |
|
23 |
@st.cache_data
|
24 |
def load_data(ticker):
|
25 |
+
"""Load stock or cryptocurrency data from Yahoo Finance."""
|
26 |
data = yf.download(ticker, START, TODAY)
|
27 |
data.reset_index(inplace=True)
|
28 |
return data
|
29 |
|
30 |
# Load data and show loading state
|
31 |
data_load_state = st.text('Loading data...')
|
32 |
+
data = load_data(selected_asset)
|
33 |
data_load_state.text('Loading data... done!')
|
34 |
|
35 |
# Display raw data
|
36 |
st.subheader('Raw data')
|
37 |
st.write(data.tail())
|
38 |
|
39 |
+
# Ensure 'Close' prices are numeric and handle any missing values
|
40 |
+
data['Close'] = pd.to_numeric(data['Close'], errors='coerce')
|
41 |
+
data.dropna(subset=['Close'], inplace=True)
|
42 |
+
|
43 |
# Plot raw data function
|
44 |
def plot_raw_data():
|
45 |
fig = go.Figure()
|
46 |
+
fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="Open Price"))
|
47 |
+
fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="Close Price"))
|
48 |
fig.layout.update(title_text='Time Series Data with Rangeslider', xaxis_rangeslider_visible=True)
|
49 |
st.plotly_chart(fig)
|
50 |
|
|
|
65 |
|
66 |
# Show forecast data and plot forecast
|
67 |
st.subheader('Forecast data')
|
68 |
+
st.write(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())
|
69 |
st.write(f'Forecast plot for {n_years} years')
|
70 |
|
71 |
fig1 = plot_plotly(m, forecast)
|