Reads historical stock market data from a CSV file and calculates technical indicators including SMA(30), EMA(30), daily returns, and 20-period volatility for multiple stocks.
import numpy as np
import pandas as pd
python danieloheb_assignment_2.py
The script reads the csv file stock_data_july_to_september.csv and outputs a JSON dictionary with analysis results based on the computational findings.
The CSV should contain the following columns for each row:
- Date: Trading date (YYYY-MM-DD)
- Symbol: Stock ticker (AAPL, MSFT, GOOGL, etc.)
- Open, High, Low, Close: Price data (Price at Opening, Highest Daily Price, Lowest Daily Price, and Price at Closing)
- Volume: Trading volume (Total Shares Moved During Period)
Example:
Date,Symbol,Open,High,Low,Close,Volume
2025-07-01,AAPL,150.5,152.3,150.0,151.8,1000000
read_and_parse_csv(filepath): Reads CSV and returns list of dictionariesstock_summary(rows_for_symbol): Extracts and returns (symbol, highest_high, lowest_low) as tuple from CSVtechnical_analysis(closes): Returns dict with sma_30, ema_30, three_month_return, average_vol (Rolling Average, Exponential Average, Last Daily Return, and Average Standard Deviation of Returns Over Last 20 periods)main(path): Orchestrates full pipeline and outputs formatted JSON report for reader to analyze
- CSV headers match exactly in syntax and spelling: Date, Symbol, Open, High, Low, Close, Volume
- Data is in proper chronological order (oldest to newest)
- Each stock has sufficient data (30+ daily closes for analysis in both SMA and EMA)
- Incomplete rows are silently skipped for proper analysis
- Numeric fields contain valid numeric values (Floats, Integers)
{
"AAPL": {
"highest_high": 257.6,
"lowest_low": 201.27,
"tech": {
"sma_30": 238.44,
"ema_30": 240.11,
"three_month_return": 0.0008,
"average_vol": 0.0182
}
}
}