A comprehensive R script that fetches daily adjusted closing prices for a selection of major tickers from Yahoo Finance and generates a suite of exploratory and analytical plots, including moving averages, returns, volatility, cumulative performance, and comparative normalized charts.
Author: David Nguyen
- Project Overview
- Features & Plots
- Prerequisites
- Installation
- Usage
- Script Breakdown
- Selected Visualizations
- Interpreting the Outputs
- Extending & Customizing
- Data Source & Citations
- License
This repository provides a single R script (Stock_Market_Analysis.R
) which:
- Automatically installs required packages (
quantmod
,PerformanceAnalytics
,zoo
) if missing - Downloads daily adjusted closing prices for tickers (
AAPL
,MSFT
,GOOG
,AMZN
,TSLA
,NFLX
,NVDA
) - Computes 20-day & 50-day simple moving averages, daily log returns, 20-day rolling volatility, and monthly returns
- Renders 43 separate plots, each saved as
plot-<n>.png
in the working directory - Performs a final comparative normalized performance chart (base = 100)
- Price Chart with SMAs
- Daily Log Returns Time Series
- Histogram of Daily Returns
- 20-Day Rolling Volatility
- Cumulative Returns
- Monthly Returns Boxplot
- Comparative Normalized Performance
Each ticker produces six individual plots, plus one comparative chart, for a total of 43 PNG files.
- R (version ≥ 4.0)
- Internet connection to fetch data from Yahoo Finance
-
Clone this repository:
git clone https://github.com/yourusername/stock-market-analysis.git cd stock-market-analysis
-
Open R or RStudio in this folder.
In an R console or RStudio:
# Source the analysis script
source("Stock_Market_Analysis.R")
The script will:
- Install any missing packages.
- Download and process each ticker’s data.
- Display each plot interactively (with a white background).
- Save each plot as
plot-1.png
…plot-43.png
in the project directory. - Print summary statistics for each ticker’s returns.
-
Setup
- Defines and installs package list
- Loads libraries
-
Data Fetch & Preparation
- Defines tickers & date range
- Downloads via
quantmod::getSymbols()
- Extracts adjusted close, SMAs, returns, volatility, monthly returns
-
Plotting Sections (per ticker)
- Price + SMAs
- Daily Returns
- Histogram of Returns
- Rolling Volatility
- Cumulative Returns
- Monthly Returns Boxplot
-
Comparative Performance
- Merges all adjusted series
- Normalizes each to 100 at the first date
- Plots on a single chart with legend
-
Summary Statistics
- Prints mean, SD, skewness, kurtosis, etc., via
PerformanceAnalytics::table.Stats()
- Prints mean, SD, skewness, kurtosis, etc., via
Below are some of the 43 plots generated to visualize the stock market data better:
- SMAs vs. Price: Identify trends and potential crossovers
- Returns Series & Histogram: Assess volatility clustering & distribution shape
- Rolling Volatility: Track risk over time (annualized)
- Cumulative Returns: Evaluate total growth from start date
- Monthly Boxplot: Compare variability across months
- Normalized Chart: Compare relative performance on a common scale
- Add tickers: Update
tickers <- c(...)
- Change SMAs: Modify
n
inSMA()
calls - Alternative returns: Use
type = "arithmetic"
indailyReturn()
- Additional analytics: Compute drawdowns, Sharpe ratios via
PerformanceAnalytics
- Export formats: Switch
png()
topdf()
orsvg()
-
Yahoo Finance (via
quantmod::getSymbols
) -
R & Packages:
- R Core Team (2023). R: A language and environment for statistical computing.
- Jeffrey Ryan and Jim Lemon et al., quantmod package.
- Brian Peterson, PerformanceAnalytics package.
Distributed under the MIT License. See LICENSE for details.