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
def generate_mock_data():
dates = pd.date_range(start='2022-01-01', end='2023-01-01', freq='B')
tickers = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'FB']
data = {}
for ticker in tickers:
prices = np.random.normal(loc=100, scale=10, size=len(dates))
volume = np.random.randint(1000000, 10000000, len(dates))
data[ticker] = pd.DataFrame({
'Date': dates,
'Open': prices * np.random.uniform(0.95, 1.05, len(dates)),
'Close': prices,
'High': prices * np.random.uniform(1.01, 1.1, len(dates)),
'Low': prices * np.random.uniform(0.9, 0.99, len(dates)),
'Volume': volume,
})
data[ticker].set_index('Date', inplace=True)
return data
mock_data = generate_mock_data()
st.title('Stock Analysis App')
selected_stock = st.selectbox('Select Stock:', list(mock_data.keys()))
st.sidebar.header('Price Trend Settings')
date_range = st.sidebar.date_input('Select Date Range', [pd.to_datetime('2022-01-01'), pd.to_datetime('2023-01-01')])
indicator = st.sidebar.checkbox('Show Moving Averages')
chart_type = st.sidebar.selectbox('Chart Type:', ['Line', 'Bar'])
selected_data = mock_data[selected_stock].loc[date_range[0]:date_range[1]]
st.subheader(f'Price Trend for {selected_stock}')
if chart_type == 'Line':
plt.figure(figsize=(10, 5))
plt.plot(selected_data.index, selected_data['Close'], label='Close')
if indicator:
plt.plot(selected_data.index, selected_data['Close'].rolling(window=5).mean(), label='5-Day MA', linestyle='--')
plt.plot(selected_data.index, selected_data['Close'].rolling(window=20).mean(), label='20-Day MA', linestyle='--')
plt.title('Price Trend')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
st.pyplot()
elif chart_type == 'Bar':
plt.figure(figsize=(10, 5))
plt.bar(selected_data.index, selected_data['Close'], label='Close')
plt.title('Price Trend')
plt.xlabel('Date')
plt.ylabel('Price')
plt.legend()
st.pyplot()
st.subheader('Basic Statistics')
mean_price = selected_data['Close'].mean()
volatility = selected_data['Close'].std()
st.write(f'Mean Price: ${mean_price:.2f}')
st.write(f'Price Volatility: ${volatility:.2f}')
st.subheader('Volume Analysis')
plt.figure(figsize=(10, 5))
plt.bar(selected_data.index, selected_data['Volume'], color='orange', label='Volume')
plt.title('Volume Analysis')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.legend()
st.pyplot()
st.subheader('Compare Stocks')
compare_stocks = st.multiselect('Select Stocks to Compare:', list(mock_data.keys()), default=[selected_stock])
for stock in compare_stocks:
data_to_compare = mock_data[stock].loc[date_range[0]:date_range[1]]
plt.plot(data_to_compare.index, data_to_compare['Close'], label=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?