intraday stock dashboard
To upload files, please first save the app
import streamlit as st
import yfinance as yf
import plotly.graph_objects as go
from datetime import datetime, timedelta
import pandas as pd
st.set_page_config(layout="wide")
st.title("Intraday Stock Dashboard")
# Sidebar inputs
with st.sidebar:
symbol = st.text_input("Stock Symbol", value="AAPL").upper()
days_back = st.slider("Days to Look Back", min_value=1, max_value=7, value=3)
interval = st.selectbox(
"Interval",
options=["1m", "2m", "5m", "15m", "30m", "60m"],
index=2
)
# Get stock data
@st.cache_data(ttl=60) # Cache for 1 minute
def get_stock_data(symbol, days_back, interval):
try:
end = datetime.now()
start = end - timedelta(days=days_back)
stock = yf.Ticker(symbol)
df = stock.history(start=start, end=end, interval=interval)
return df, stock.info
except Exception as e:
st.error(f"Error fetching data: {e}")
return None, None
df, info = get_stock_data(symbol, days_back, interval)
if df is not None and not df.empty:
# Company info
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Current Price", f"${df['Close'].iloc[-1]:.2f}",
f"{((df['Close'].iloc[-1] - df['Close'].iloc[-2])/df['Close'].iloc[-2]*100):.2f}%")
with col2:
st.metric("Volume", f"{df['Volume'].iloc[-1]:,.0f}")
with col3:
if info:
st.metric("Market Cap", f"${info.get('marketCap', 0)/1e9:.2f}B")
# Candlestick chart
fig = go.Figure(data=[go.Candlestick(x=df.index,
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'])])
fig.update_layout(
title=f"{symbol} Stock Price",
yaxis_title="Price (USD)",
xaxis_title="Date",
height=600,
template="plotly_dark"
)
st.plotly_chart(fig, use_container_width=True)
# Volume chart
volume_fig = go.Figure(data=[go.Bar(x=df.index, y=df['Volume'])])
volume_fig.update_layout(
title=f"{symbol} Volume",
yaxis_title="Volume",
xaxis_title="Date",
height=300,
template="plotly_dark"
)
st.plotly_chart(volume_fig, use_container_width=True)
# Recent trades table
st.subheader("Recent Trades")
recent_data = df.tail(10).reset_index()
recent_data['Datetime'] = recent_data['Datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')
st.dataframe(recent_data)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?