import streamlit as st
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
from datetime import date
# Set up the app title and layout
st.title("📈 Financial Dashboard")
# Sidebar inputs
st.sidebar.header("Stock Selection")
symbol = st.sidebar.text_input("Enter Stock Symbol", value="AAPL")
st.sidebar.header("Date Range")
start_date = st.sidebar.date_input("Start Date", date(2023, 1, 1))
end_date = st.sidebar.date_input("End Date", date.today())
# Fetch stock data function
@st.cache_data
def fetch_stock_data(symbol, start, end):
return yf.download(symbol, start=start, end=end)
# Fetch and display the data
if st.sidebar.button("Fetch Data"):
try:
df = fetch_stock_data(symbol, start_date, end_date)
if df.empty:
st.error("No data found for the selected stock symbol and date range.")
else:
st.success(f"Showing data for {symbol}")
# Display raw data
st.subheader(f"Raw Data for {symbol}")
st.dataframe(df.tail(10))
# Plot the candlestick chart
st.subheader(f"{symbol} Stock Price Chart")
fig = go.Figure(data=[
go.Candlestick(
x=df.index,
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'],
name=symbol
)
])
fig.update_layout(
title=f"{symbol} Stock Prices",
xaxis_title="Date",
yaxis_title="Price (USD)",
xaxis_rangeslider_visible=True
)
st.plotly_chart(fig)
except Exception as e:
st.error(f"An error occurred: {e}")