title: “Business Analytics Mini-bootcamp - Summer 2025 (day 2) - Stock Market Data Analysis using quantmod and R” author: “Justin Maliska, taught by Zhenning ‘Jimmy’ Xu, Ph.D.” format: html editor: visual —

Introduction

This Quarto notebook explores stock market data using R packages like quantmod, PerformanceAnalytics, and tidyverse. We analyze and visualize data for multiple tickers including F, TSLA, GOOGL, and AMZN.

Context: Tools like quantmod are widely used in algorithmic trading platforms and fintech firms. For example:

  • Lemon Markets, a European brokerage API platform, uses similar R-based or Python-based workflows to empower retail and institutional clients.
  • QuantInsti, a leading training provider in algorithmic trading, uses packages like quantmod and PerformanceAnalytics in strategy design, backtesting, and risk analysis.

Load Stock Data

F <- getSymbols("F", src = "yahoo", from = "2020-01-01", to = "2025-06-01", auto.assign = FALSE)
TSLA <- getSymbols("TSLA", src = "yahoo", from = "2015-01-01", to = "2025-06-01", auto.assign = FALSE)
GOOGL <- getSymbols("GOOGL", src = "yahoo", from = "2015-01-01", to = "2025-06-01", auto.assign = FALSE)

Reflection Question:

What are some advantages of using programmatic data collection (via getSymbols) over manually downloading CSV files from Yahoo Finance?

Answer

I think that an advantage of using programmatic data collection is that is it being updated in real time instead of using a previously downloaded CSV file that may have old data.

View Sample Data

head(GOOGL)
           GOOGL.Open GOOGL.High GOOGL.Low GOOGL.Close GOOGL.Volume
2015-01-02    26.6300    26.7900   26.3940     26.4775     26480000
2015-01-05    26.3575    26.3995   25.8875     25.9730     41182000
2015-01-06    26.0250    26.0605   25.2775     25.3320     54456000
2015-01-07    25.5475    25.5745   25.1825     25.2575     46918000
2015-01-08    25.0755    25.3750   24.7510     25.3455     73054000
2015-01-09    25.4090    25.4300   24.9325     25.0360     42000000
           GOOGL.Adjusted
2015-01-02       26.31965
2015-01-05       25.81816
2015-01-06       25.18098
2015-01-07       25.10692
2015-01-08       25.19440
2015-01-09       24.88675
barChart(GOOGL, name = "GOOGL Volume Bar Chart")

chartSeries(GOOGL, type = "bar", theme = chartTheme("white"))

chartSeries(GOOGL, type = "line", theme = chartTheme("white"))

chartSeries(GOOGL, type = "candlesticks", theme = chartTheme("white"))

Reflection Question:

Explore each chart type. How do line, bar, and candlestick charts differ in the kind of information they emphasize?

Answer

These different charts give you different ways of seeing the average selling price.

Daily and Yearly Returns

plot(dailyReturn(GOOGL), main = "GOOGL Daily Returns")

plot(yearlyReturn(GOOGL), main = "GOOGL Yearly Returns")

Discussion Idea:

QuantInsti trains traders to analyze returns across multiple timeframes. Why might a yearly return plot give a different impression than a daily return plot?

write.table(GOOGL, file = "GOOGL_stock.csv", row.names = FALSE, col.names = TRUE, sep = ",")

Convert to Data Frame and Visualize

GOOGL_df <- data.frame(GOOGL)
GOOGL_df$time <- index(GOOGL)

head(GOOGL_df)
           GOOGL.Open GOOGL.High GOOGL.Low GOOGL.Close GOOGL.Volume
2015-01-02    26.6300    26.7900   26.3940     26.4775     26480000
2015-01-05    26.3575    26.3995   25.8875     25.9730     41182000
2015-01-06    26.0250    26.0605   25.2775     25.3320     54456000
2015-01-07    25.5475    25.5745   25.1825     25.2575     46918000
2015-01-08    25.0755    25.3750   24.7510     25.3455     73054000
2015-01-09    25.4090    25.4300   24.9325     25.0360     42000000
           GOOGL.Adjusted       time
2015-01-02       26.31965 2015-01-02
2015-01-05       25.81816 2015-01-05
2015-01-06       25.18098 2015-01-06
2015-01-07       25.10692 2015-01-07
2015-01-08       25.19440 2015-01-08
2015-01-09       24.88675 2015-01-09
## Do you still remember the ggplot functions we used yesterday?
ggplot(GOOGL_df, aes(x = time, y = GOOGL.Adjusted)) +
  geom_point() +
  labs(title = "GOOGL Adjusted Closing Price",
       x = "Date", y = "Adjusted Price")

Critical Thinking:

Why might financial analysts prefer adjusted closing prices over raw closing prices when visualizing or modeling stock performance?

Technical Indicators: Bollinger Bands

What Are Bollinger Bands?

Bollinger Bands are a technical analysis tool used to measure a stock’s price volatility and identify potential buying or selling opportunities.

They consist of three lines:

Middle Band: This is usually a simple moving average (SMA) — often a 20-day average.

Upper Band: This is the middle band plus 2 standard deviations.

Lower Band: This is the middle band minus 2 standard deviations.

These bands move with the stock price and expand or contract based on how volatile the stock is.

🧠 Why Use Bollinger Bands? They help answer questions like:

Is the stock price relatively high or low compared to recent history?

Is the stock entering a period of increased volatility?

📌 How to Read Bollinger Bands When the price touches or moves above the upper band: → The stock might be overbought (price could be too high). → Some traders see this as a sell signal.

When the price touches or falls below the lower band: → The stock might be oversold (price could be too low). → This could be a buy signal.

AMZN <- getSymbols("AMZN", src = "yahoo", auto.assign = FALSE)

chartSeries(AMZN, subset = 'last 3 months', theme = chartTheme("white"))

addBBands(n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1)

What is one key insight or skill you learned today? Briefly explain it in your own words.

Answer

One thing that I learned from this lab was how to import data for stocks.

What is one concept or tool from today’s session that you would like to apply in the future? Describe how you might use it.

Answer

Using R instead of only relying on EXCEL is something I would like to apply to my work life and my personal life.

References

quantmod website: https://www.quantmod.com/

10 great R packages for stock market data:https://dev.to/lemon-markets/10-great-r-packages-for-stock-market-data-1ocp

QuantInsti: Guide to quantmod: https://blog.quantinsti.com/a-guide-on-r-quantmod-package-how-to-get-started/

chartSeries documentation: https://www.rdocumentation.org/packages/quantmod/versions/0.4.27/topics/chartSeries

Shorting strategy in R: https://blog.quantinsti.com/shorting-high-algo-trading-strategy-r/