File size: 3,520 Bytes
b7d2c24
 
 
2162ce8
a3a681d
f901f42
b7d2c24
 
 
 
 
 
 
a3a681d
b7d2c24
a3a681d
 
 
b7d2c24
 
 
 
 
f901f42
b7d2c24
a3a681d
b7d2c24
 
 
 
 
 
a3a681d
b7d2c24
 
 
2162ce8
b7d2c24
 
08d18c6
 
 
 
 
 
 
a3a681d
b7d2c24
 
 
2162ce8
 
b7d2c24
 
 
08d18c6
 
 
b7d2c24
08d18c6
 
 
b7d2c24
08d18c6
 
 
b7d2c24
08d18c6
 
 
b7d2c24
08d18c6
 
 
 
b7d2c24
08d18c6
 
b7d2c24
08d18c6
 
 
 
2162ce8
08d18c6
 
 
 
 
 
 
 
 
2162ce8
 
 
 
 
 
 
 
 
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 yfinance as yf
import pandas as pd  # Importing pandas to avoid NameError
from prophet import Prophet
from prophet.plot import plot_plotly
from plotly import graph_objs as go

# Constants for date range
START = "2015-01-01"
TODAY = date.today().strftime("%Y-%m-%d")

# Streamlit app title
st.title('Stock & Cryptocurrency Forecast App')

# Stock and cryptocurrency selection
assets = ('GOOG', 'AAPL', 'MSFT', 'GME', 'BTC-USD', 'ETH-USD')  # Added BTC and ETH
selected_asset = st.selectbox('Select dataset for prediction', assets)

# Years of prediction slider
n_years = st.slider('Years of prediction:', 1, 4)
period = n_years * 365

@st.cache_data
def load_data(ticker):
    """Load stock or cryptocurrency data from Yahoo Finance."""
    data = yf.download(ticker, START, TODAY)
    data.reset_index(inplace=True)
    return data

# Load data and show loading state
data_load_state = st.text('Loading data...')
data = load_data(selected_asset)
data_load_state.text('Loading data... done!')

# Display raw data
st.subheader('Raw Data')
st.write(data.tail())

# Check if 'Close' column exists before converting
if 'Close' in data.columns:
    # Ensure 'Close' prices are numeric and handle any missing values
    data['Close'] = pd.to_numeric(data['Close'], errors='coerce')
    data.dropna(subset=['Close'], inplace=True)
else:
    st.error("The 'Close' column is missing from the data. Please check the selected asset.")

# Plot raw data function
def plot_raw_data():
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name="Open Price", line=dict(color='blue')))
    fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name="Close Price", line=dict(color='orange')))
    fig.layout.update(title_text='Time Series Data with Rangeslider', xaxis_rangeslider_visible=True)
    st.plotly_chart(fig)

# Call the plotting function if there's enough data
if not data.empty:
    plot_raw_data()

    # Prepare data for Prophet model
    df_train = data[['Date', 'Close']]
    df_train = df_train.rename(columns={"Date": "ds", "Close": "y"})

    # Create and fit the Prophet model
    m = Prophet()
    m.fit(df_train)

    # Create future dataframe and make predictions
    future = m.make_future_dataframe(periods=period)
    forecast = m.predict(future)

    # Show forecast data and plot forecast
    st.subheader('Forecast Data')
    st.write(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())
    st.write(f'Forecast plot for the next {n_years} years')

    fig1 = plot_plotly(m, forecast)
    st.plotly_chart(fig1)

    # Show forecast components
    st.subheader("Forecast Components")
    fig2 = m.plot_components(forecast)
    st.plotly_chart(fig2)

    # Additional Insights: Displaying key metrics
    st.subheader("Key Metrics")
    latest_data = forecast.iloc[-1]
    st.write(f"Predicted Price: ${latest_data['yhat']:.2f}")
    st.write(f"Lower Bound: ${latest_data['yhat_lower']:.2f}")
    st.write(f"Upper Bound: ${latest_data['yhat_upper']:.2f}")

else:
    st.error("No valid data available to make predictions.")

# User Guidance Section
st.sidebar.header("User Guidance")
st.sidebar.write("""
This application allows you to predict stock prices or cryptocurrency values based on historical data.
- Select a stock or cryptocurrency from the dropdown menu.
- Use the slider to choose how many years into the future you want to predict.
- View the forecasted prices along with confidence intervals.
""")