<- getSymbols("QQQ", src = "yahoo", from = "2020-01-01", to = "2025-06-01", auto.assign = FALSE)
QQQ <- getSymbols("TSLA", src = "yahoo", from = "2015-01-01", to = "2025-06-01", auto.assign = FALSE)
TSLA <- getSymbols("NVDA", src = "yahoo", from = "2015-01-01", to = "2025-06-01", auto.assign = FALSE) NVDA
Business Analytics Mini-bootcamp - Summer 2025 (day 2) - Stock Market Data Analysis using quantmod and R
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 QQQ, TSLA, NVDA, 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
andPerformanceAnalytics
in strategy design, backtesting, and risk analysis.
Load Stock Data
Reflection Question:
What are some advantages of using programmatic data collection (via getSymbols) over manually downloading CSV files from Yahoo Finance?
View Sample Data
head(NVDA)
NVDA.Open NVDA.High NVDA.Low NVDA.Close NVDA.Volume NVDA.Adjusted
2015-01-02 0.50325 0.50700 0.49525 0.50325 113680000 0.4830991
2015-01-05 0.50325 0.50475 0.49250 0.49475 197952000 0.4749394
2015-01-06 0.49550 0.49600 0.47925 0.47975 197764000 0.4605401
2015-01-07 0.48325 0.48750 0.47700 0.47850 321808000 0.4593401
2015-01-08 0.48400 0.49950 0.48375 0.49650 283780000 0.4766193
2015-01-09 0.49825 0.50225 0.49150 0.49850 209540000 0.4785393
barChart(NVDA, name = "NVDA Volume Bar Chart")
chartSeries(NVDA, type = "bar", theme = chartTheme("white"))
chartSeries(NVDA, type = "line", theme = chartTheme("white"))
chartSeries(NVDA, 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?
Daily and Yearly Returns
plot(dailyReturn(NVDA), main = "NVDA Daily Returns")
plot(yearlyReturn(NVDA), main = "NVDA 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(NVDA, file = "NVDA_stock.csv", row.names = FALSE, col.names = TRUE, sep = ",")
Convert to Data Frame and Visualize
<- data.frame(NVDA)
NVDA_df $time <- index(NVDA)
NVDA_df
head(NVDA_df)
NVDA.Open NVDA.High NVDA.Low NVDA.Close NVDA.Volume NVDA.Adjusted
2015-01-02 0.50325 0.50700 0.49525 0.50325 113680000 0.4830991
2015-01-05 0.50325 0.50475 0.49250 0.49475 197952000 0.4749394
2015-01-06 0.49550 0.49600 0.47925 0.47975 197764000 0.4605401
2015-01-07 0.48325 0.48750 0.47700 0.47850 321808000 0.4593401
2015-01-08 0.48400 0.49950 0.48375 0.49650 283780000 0.4766193
2015-01-09 0.49825 0.50225 0.49150 0.49850 209540000 0.4785393
time
2015-01-02 2015-01-02
2015-01-05 2015-01-05
2015-01-06 2015-01-06
2015-01-07 2015-01-07
2015-01-08 2015-01-08
2015-01-09 2015-01-09
## Do you still remember the ggplot functions we used yesterday?
ggplot(NVDA_df, aes(x = time, y = NVDA.Adjusted)) +
geom_point() +
labs(title = "NVDA 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.
<- getSymbols("AMZN", src = "yahoo", auto.assign = FALSE)
AMZN
chartSeries(AMZN, subset = 'last 3 months', theme = chartTheme("white"))
addBBands(n = 20, sd = 2, maType = "SMA", draw = "bands", on = -1)
Reflection:
Bollinger Bands are popular in technical analysis. What kind of market behavior do the upper and lower bands signal? How might a quant fund use this indicator?
What is one key insight or skill you learned today? Briefly explain it in your own words.
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.
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/