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_volatility(df, window=30):
"""Calculate rolling volatility (annualized)"""
returns = df['Close'].pct_change().dropna()
volatility = returns.rolling(window=window).std().iloc[-1] * np.sqrt(252) * 100
return volatility
def calculate_basic_stats(df):
"""Calculate basic statistical measures"""
close_prices = df['Close']
# Calculate returns
total_return = ((close_prices.iloc[-1] - close_prices.iloc[0]) / close_prices.iloc[0]) * 100
# Annualized return (assuming 252 trading days)
days = len(df)
years = days / 252
annualized_return = ((close_prices.iloc[-1] / close_prices.iloc[0]) ** (1/years) - 1) * 100
stats = {
'mean': close_prices.mean(),
'median': close_prices.median(),
'std': close_prices.std(),
'min': close_prices.min(),
'max': close_prices.max(),
'total_return': total_return,
'annualized_return': annualized_return
}
return stats
def calculate_rsi(df, window=14):
"""Calculate Relative Strength Index"""
delta = df['Close'].diff()
gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean()
rs = gain / loss
rsi = 100 - (100 / (1 + rs))
return rsi
def calculate_bollinger_bands(df, window=20, num_std=2):
"""Calculate Bollinger Bands"""
rolling_mean = df['Close'].rolling(window=window).mean()
rolling_std = df['Close'].rolling(window=window).std()
upper_band = rolling_mean + (rolling_std * num_std)
lower_band = rolling_mean - (rolling_std * num_std)
return upper_band, lower_band, rolling_mean
def calculate_macd(df, fast=12, slow=26, signal=9):
"""Calculate MACD (Moving Average Convergence Divergence)"""
ema_fast = df['Close'].ewm(span=fast).mean()
ema_slow = df['Close'].ewm(span=slow).mean()
macd = ema_fast - ema_slow
signal_line = macd.ewm(span=signal).mean()
histogram = macd - signal_line
return macd, signal_line, histogram
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?