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 pandas as pd
import numpy as np
def calculate_moving_averages(df, periods):
"""
Calculate moving averages for given periods
"""
df_copy = df.copy()
for period in periods:
df_copy[f'MA_{period}'] = df_copy['Close'].rolling(window=period).mean()
return df_copy
def calculate_stats(df):
"""
Calculate basic statistics for the stock data
"""
if df.empty:
return {}
current_price = df['Close'].iloc[-1]
previous_price = df['Close'].iloc[-2] if len(df) > 1 else current_price
price_change = current_price - previous_price
price_change_pct = (price_change / previous_price) * 100 if previous_price != 0 else 0
# Calculate volatility (annualized)
returns = df['Close'].pct_change().dropna()
volatility = returns.std() * np.sqrt(252) * 100 # Annualized volatility in %
stats = {
'current_price': current_price,
'price_change': price_change,
'price_change_pct': price_change_pct,
'mean_price': df['Close'].mean(),
'volatility': volatility,
'avg_volume': df['Volume'].mean(),
'high_52w': df['High'].max(),
'low_52w': df['Low'].min(),
'total_return': ((current_price - df['Close'].iloc[0]) / df['Close'].iloc[0]) * 100
}
return stats
def calculate_rsi(df, period=14):
"""
Calculate Relative Strength Index (RSI)
"""
df_copy = df.copy()
# Calculate price changes
delta = df_copy['Close'].diff()
# Calculate gains and losses
gains = delta.where(delta > 0, 0)
losses = -delta.where(delta < 0, 0)
# Calculate average gains and losses
avg_gains = gains.rolling(window=period).mean()
avg_losses = losses.rolling(window=period).mean()
# Calculate RS and RSI
rs = avg_gains / avg_losses
rsi = 100 - (100 / (1 + rs))
df_copy['RSI'] = rsi
return df_copy
def calculate_macd(df, fast_period=12, slow_period=26, signal_period=9):
"""
Calculate MACD (Moving Average Convergence Divergence)
"""
df_copy = df.copy()
# Calculate EMAs
ema_fast = df_copy['Close'].ewm(span=fast_period).mean()
ema_slow = df_copy['Close'].ewm(span=slow_period).mean()
# Calculate MACD line
macd_line = ema_fast - ema_slow
# Calculate signal line
signal_line = macd_line.ewm(span=signal_period).mean()
# Calculate histogram
histogram = macd_line - signal_line
df_copy['MACD'] = macd_line
df_copy['MACD_Signal'] = signal_line
df_copy['MACD_Histogram'] = histogram
return df_copy
def calculate_bollinger_bands(df, period=20, std_dev=2):
"""
Calculate Bollinger Bands
"""
df_copy = df.copy()
# Calculate moving average and standard deviation
ma = df_copy['Close'].rolling(window=period).mean()
std = df_copy['Close'].rolling(window=period).std()
# Calculate bands
df_copy['BB_Upper'] = ma + (std * std_dev)
df_copy['BB_Lower'] = ma - (std * std_dev)
df_copy['BB_Middle'] = ma
return df_copy
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?