Stock Market Analysis (Finance)
Given daily stock prices, compute:
- Daily returns
- Rolling averages
- Volatility
- Compare multiple stocks
Analysis pipeline
Section titled “Analysis pipeline”flowchart LR A["Raw prices
(date, close)"] --> B["Returns
(pct_change)"] B --> C["Smooth
(20/50-day MA)"] C --> D["Visualize
(price + MA lines)"] D --> E["Volatility
(rolling std)"]
Dataset columns
Section titled “Dataset columns”date,close(and optionally open/high/low/volume)
Step 1: Load and parse
Section titled “Step 1: Load and parse”import pandas as pd
df = pd.read_csv("data/stock.csv")
df["date"] = pd.to_datetime(df["date"], errors="coerce")
df = df.sort_values("date")
print(df.head())Step 2: Daily returns
Section titled “Step 2: Daily returns”df["return"] = df["close"].pct_change()
print(df[["date", "close", "return"]].head(10))Step 3: Moving averages
Section titled “Step 3: Moving averages”df["ma20"] = df["close"].rolling(20).mean()
df["ma50"] = df["close"].rolling(50).mean()Step 4: Plot
Section titled “Step 4: Plot”import matplotlib.pyplot as plt
plt.figure(figsize=(10, 4))
plt.plot(df["date"], df["close"], label="close")
plt.plot(df["date"], df["ma20"], label="MA20")
plt.plot(df["date"], df["ma50"], label="MA50")
plt.title("Price and moving averages")
plt.legend()
plt.xticks(rotation=20)
plt.tight_layout()
plt.show()Step 5: Volatility (rolling std)
Section titled “Step 5: Volatility (rolling std)”df["volatility_20"] = df["return"].rolling(20).std()Visualize it
Section titled “Visualize it”Deliverable
Section titled “Deliverable”Summarize:
- Trend periods
- High-volatility periods and possible reasons
- Compare performance across stocks (optional)
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Daily returns
Section titled “Exercise 1 – Daily returns”Exercise 2 – Moving average
Section titled “Exercise 2 – Moving average”Exercise 3 – Rolling volatility
Section titled “Exercise 3 – Rolling volatility”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading