Replicate the app from the image.
To upload files, please first save the app
import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime, timedelta
import yfinance as yf
# Set page config
st.set_page_config(layout="wide", page_title="Financial Dashboard")
# Apply dark theme
st.markdown("""
<style>
.stApp {
background-color: #0E1117;
color: #FAFAFA;
}
</style>
""", unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.title("Stock Analysis Dashboard")
ticker_symbol = st.text_input("Enter Stock Symbol", value="AAPL")
period = st.selectbox("Select Time Period",
["1mo", "3mo", "6mo", "1y", "2y", "5y"],
index=3)
# Fetch data
@st.cache_data
def get_stock_data(symbol, period):
stock = yf.Ticker(symbol)
hist = stock.history(period=period)
return hist, stock.info
df, info = get_stock_data(ticker_symbol, period)
# Main layout
col1, col2 = st.columns([2, 1])
with col1:
# Main price chart
fig = go.Figure()
fig.add_trace(go.Candlestick(x=df.index,
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'],
name='OHLC'))
fig.update_layout(
title=f"{ticker_symbol} Stock Price",
yaxis_title="Stock Price (USD)",
template="plotly_dark",
height=400
)
st.plotly_chart(fig, use_container_width=True)
# Volume chart
volume_fig = px.bar(df, x=df.index, y='Volume',
title=f"{ticker_symbol} Trading Volume")
volume_fig.update_layout(template="plotly_dark", height=200)
st.plotly_chart(volume_fig, use_container_width=True)
with col2:
# Key metrics
st.subheader("Key Metrics")
metrics = {
"Market Cap": info.get('marketCap', 'N/A'),
"P/E Ratio": info.get('trailingPE', 'N/A'),
"52 Week High": info.get('fiftyTwoWeekHigh', 'N/A'),
"52 Week Low": info.get('fiftyTwoWeekLow', 'N/A'),
"Dividend Yield": info.get('dividendYield', 'N/A')
}
for metric, value in metrics.items():
col_metric, col_value = st.columns([1, 1])
with col_metric:
st.write(metric)
with col_value:
if isinstance(value, (int, float)):
if metric == "Market Cap":
st.write(f"${value:,.0f}")
elif metric == "Dividend Yield":
st.write(f"{value:.2%}")
else:
st.write(f"{value:.2f}")
else:
st.write(value)
# Additional charts in lower section
col1, col2, col3 = st.columns(3)
with col1:
# Daily returns histogram
df['Daily Return'] = df['Close'].pct_change()
returns_fig = px.histogram(df, x='Daily Return',
title='Distribution of Daily Returns')
returns_fig.update_layout(template="plotly_dark", height=300)
st.plotly_chart(returns_fig, use_container_width=True)
with col2:
# Moving averages
df['MA20'] = df['Close'].rolling(window=20).mean()
df['MA50'] = df['Close'].rolling(window=50).mean()
ma_fig = go.Figure()
ma_fig.add_trace(go.Scatter(x=df.index, y=df['Close'],
name='Price', line=dict(color='white')))
ma_fig.add_trace(go.Scatter(x=df.index, y=df['MA20'],
name='20 Day MA', line=dict(color='orange')))
ma_fig.add_trace(go.Scatter(x=df.index, y=df['MA50'],
name='50 Day MA', line=dict(color='blue')))
ma_fig.update_layout(title='Moving Averages', template="plotly_dark", height=300)
st.plotly_chart(ma_fig, use_container_width=True)
with col3:
# RSI calculation and plot
delta = df['Close'].diff()
gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
rs = gain / loss
df['RSI'] = 100 - (100 / (1 + rs))
rsi_fig = go.Figure()
rsi_fig.add_trace(go.Scatter(x=df.index, y=df['RSI'],
name='RSI', line=dict(color='purple')))
rsi_fig.add_hline(y=70, line_dash="dash", line_color="red")
rsi_fig.add_hline(y=30, line_dash="dash", line_color="green")
rsi_fig.update_layout(title='Relative Strength Index (RSI)',
template="plotly_dark", height=300)
st.plotly_chart(rsi_fig, use_container_width=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?