Create an app for stock analysis with mock data. Generate mock daily price data for 5 tech stocks over the past year with standard fields (Open, Close, etc). Features: - Stock selector dropdown - Price trend charts - Basic stats (mean, volatility) - Moving averages - Volume analysis - Stock comparison Interactive elements: - Date range picker - Technical indicators - Chart type toggle Focus on clean visualization of realistic mock data.
To upload files, please first save the app
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Generate mock data function
def generate_mock_data(tickers, start_date, end_date):
dates = pd.date_range(start=start_date, end=end_date)
data = {}
for ticker in tickers:
price_data = pd.DataFrame(index=dates)
price_data['Open'] = np.random.uniform(100, 500, len(dates))
price_data['Close'] = price_data['Open'] + np.random.normal(0, 10, len(dates))
price_data['High'] = price_data[['Open', 'Close']].max(axis=1) + np.random.uniform(0, 5, len(dates))
price_data['Low'] = price_data[['Open', 'Close']].min(axis=1) - np.random.uniform(0, 5, len(dates))
price_data['Volume'] = np.random.randint(1000, 10000, len(dates))
data[ticker] = price_data
return data
# Generate mock stock data
stock_tickers = ['AAPL', 'GOOGL', 'MSFT', 'AMZN', 'FB']
start_date = '2022-01-01'
end_date = '2023-01-01'
stock_data = generate_mock_data(stock_tickers, start_date, end_date)
st.title('Stock Analysis Dashboard')
# Stock Selector
selected_stock = st.selectbox('Select Stock:', stock_tickers)
# Date Range Picker
start_date = st.date_input('Start Date', pd.to_datetime('2022-01-01'))
end_date = st.date_input('End Date', pd.to_datetime('2023-01-01'))
# Filter data based on date range
stock_df = stock_data[selected_stock].loc[start_date:end_date]
# Plotting
st.subheader(f'{selected_stock} Price Trend')
chart_type = st.radio('Chart Type:', ['Line', 'Bar'])
if chart_type == 'Line':
st.line_chart(stock_df[['Open', 'Close', 'High', 'Low']])
else:
# Bar chart
fig, ax = plt.subplots()
stock_df[['Open', 'Close', 'High', 'Low']].plot(kind='bar', ax=ax)
st.pyplot(fig)
# Basic Stats
st.subheader('Basic Statistics')
mean_price = stock_df['Close'].mean()
volatility = stock_df['Close'].std()
st.write(f'Mean Price: ${mean_price:.2f}')
st.write(f'Volatility: ${volatility:.2f}')
# Moving Averages
ma_period = st.number_input('MA Period:', min_value=1, max_value=30, value=5)
stock_df[f'MA_{ma_period}'] = stock_df['Close'].rolling(window=ma_period).mean()
st.line_chart(stock_df[['Close', f'MA_{ma_period}']])
# Volume Analysis
st.subheader('Volume Analysis')
st.bar_chart(stock_df['Volume'])
# Stock Comparison
st.subheader('Compare with Other Stocks')
comparison_stocks = st.multiselect('Select Other Stocks:', stock_tickers)
if comparison_stocks:
comparison_data = stock_data[comparison_stocks]
for comparison_stock in comparison_stocks:
stock_data[comparison_stock].loc[start_date:end_date]['Close'].plot(label=comparison_stock)
plt.title('Stock Comparison')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
st.pyplot()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?