International College of Digital Innovation, CMU
August 7, 2025
In this chapter, we cannot use the quantmod package because the technology is still under development.”
Download daily price of AAPL from Yahoo finance, from 1 Jan 2020 to 30 Sep 2024, and upload to Rstudio and make this plot
The quantmod is useful for quantitative financial modelling framework and have three goals:
download data
charting
technical indicator
getSymbols() function
getSymbols: The function for load and manage data from multiple source
Recommend the arguments
Symbols : vector of stock or asset name
src : “yahoo”, FRED” , etc (this course use yahoo)
periodicity : “daily”, “weekly”, or monthly”
from : Starting day: Year-month-day (2020-11-31)
to : Ending day: Year-month-day (2022-12-31)
The object from yahoo finance is XTS.
or download multiple stocks price
eXtensible Time Series (xts)
eXtensible Time Series (xts) is a powerful package that provides an extensible time series class, enabling uniform handling of many R time series classes by extending zoo.
The main benefit of using xts is the compatibility with other packages that use different time-series classes (timeSeries, zoo, …).
We have 3 xts objects, AAPL, META, and MSFT
Warning
In this study, We don’t need the variables AAPL.Volume
and AAPL.Adjusted
Step 1: Convert the xts object to a data frame.
coredata()
extracts the data values from the xts object, excluding the time index.
as.data.frame()
converts the extracted data into a standard data frame.
DF_AAPL$Date <- index(AAPL)
adds a new column called Date, containing the time index from the original xts object.
Step 2: Save data frame DF_AAP
to xlsx
Step 1: Convert the xts object to a data frame.
Step 2: Save every data frame to xlsx
You can download financial time series data from my webapp.
Assume we have excel file file.xlsx
library(readxl)
# TS = time series data frame
TS <- read_excel("file.xlsx",
col_types = c("date", "numeric", "numeric",
"numeric", "numeric", "numeric",
"numeric"))
str(TS)
tibble [968 × 7] (S3: tbl_df/tbl/data.frame)
$ date : POSIXct[1:968], format: "2021-01-04" "2021-01-05" ...
$ PTT.BK.Open : num [1:968] 41 42.2 43 43 42.8 ...
$ PTT.BK.High : num [1:968] 42.8 42.5 43.5 43.2 43.5 ...
$ PTT.BK.Low : num [1:968] 40.8 41.2 42.5 42.2 42 ...
$ PTT.BK.Close : num [1:968] 42.5 42 42.8 42.5 42.8 ...
$ PTT.BK.Volume : num [1:968] 74638600 75867500 88565400 63128900 83698400 ...
$ PTT.BK.Adjusted: num [1:968] 33 32.6 33.1 33 33.1 ...
Convert the data frame TS to an XTS object using this function.
library(xts)
TS <- xts(x = TS[,2:6],
order.by = TS$date)
first(TS, n = 7)
PTT.BK.Open PTT.BK.High PTT.BK.Low PTT.BK.Close PTT.BK.Volume
2021-01-04 41.00 42.75 40.75 42.50 74638600
2021-01-05 42.25 42.50 41.25 42.00 75867500
2021-01-06 43.00 43.50 42.50 42.75 88565400
2021-01-07 43.00 43.25 42.25 42.50 63128900
2021-01-08 42.75 43.50 42.00 42.75 83698400
2021-01-11 42.75 43.00 42.50 43.00 45553200
2021-01-12 43.00 44.00 42.50 43.75 101081900
Extract and display the stock data for “MSFT” for the year 2021. Use the xts
package’s subsetting techniques to filter by date.
Subset the “META” data to include only the observations between January 1, 2022, and December 31, 2022.
Extract the data for “MSFT” for the first quarter of 2023 (January to March) and display the results.
Create a subset of “META” data for the first 10 weeks of 2024 and calculate the average closing price during that period.
Extract “MSFT” data for the first 6 months of 2021 and 2023, and compare the average closing prices for these two periods.
Subset the “META” data to get the last 8 weeks of observations before September 30, 2024.
This component is an instance of the CodeMirror interactive text editor. The editor has been configured so that the Tab key controls the indentation of code. To move focus away from the editor, press the Escape key, and then press the Tab key directly after it. Escape and then Shift-Tab can also be used to move focus backwards.