\(~~~~~~~~\)Data Structure in R: XTS\(~~~~~~~~\)

Somsak Chanaim

International College of Digital Innovation, CMU

October 30, 2024

Warning

In this chapter, we cannot use the quantmod package because the technology is still under development.”

Why we need quantmod

Download daily price of AAPL from Yahoo finance, from \(~~\)1 Jan 2020 to 30 Sep 2024, and upload to Rstudio and make this plot

Quantitative Financial Modelling Framework (quantmod)

The quantmod is useful for quantitative financial modelling framework and have three goals:

  • download data

  • charting

  • technical indicator

install.packages("quantmod")
library(quantmod)

Download Data

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.

Example

getSymbols(Symbols = "AAPL",
           src = "yahoo",         
           periodicity = "daily",# "weekly", "month"
           from = "2020-01-01",  # Year-month-day
           to = "2024-09-30")    # Year-month-day

or download multiple stocks price

getSymbols(Symbols = c("MSFT","META"),
           src = "yahoo",         
           periodicity = "weekly",# "weekly", "month"
           from = "2020-01-01",  # Year-month-day
           to = "2024-09-30")    # Year-month-day

The xts package

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, …).

Selecting, Subsetting and Indexing

We have 3 xts objects, AAPL, META, and MSFT

Data Frame command

Warning

In this study, We don’t need the variables AAPL.Volume amd AAPL.Adjected

Selecting data by year

Selecting data by year and month

Selecting data from starting date to any date

Selecting data from any date to last date

Selecting data for the first n weeks

Selecting data for the first n days

Selecting data for the last n week

Selecting data for the last n days

From daily price to weekly price

From daily price to monthly price

From daily price to monthly price

Exercise

Exercise 1: Extract Data for a Specific Year

Extract and display the stock data for “MSFT” for the year 2021. Use the xts package’s subsetting techniques to filter by date.

Solution:

   msft_2021 <- MSFT["2021"]
   head(msft_2021)

Exercise 2: Filter Data Between Two Dates

Subset the “META” data to include only the observations between January 1, 2022, and December 31, 2022.

Solution:

   meta_2022 <- META["2022-01-01/2022-12-31"]
   head(meta_2022)

Exercise 3: Select Data for the First Quarter

Extract the data for “MSFT” for the first quarter of 2023 (January to March) and display the results.

Solution:

   msft_q1_2023 <- MSFT["2023-01/2023-03"]
   head(msft_q1_2023)

Exercise 4: Subset Data for the First N Weeks

Create a subset of “META” data for the first 10 weeks of 2024 and calculate the average closing price during that period.

Solution:

mean(META$META.Close["2024"])

Exercise 5: Compare Data Over Two Periods

Extract “MSFT” data for the first 6 months of 2021 and 2023, and compare the average closing prices for these two periods.

Solution:

   avg_2021 <- mean(MSFT$MSFT.Close["2021-01/2021-06"])
   avg_2023 <- mean(MSFT$MSFT.Close["2023-01/2023-06"])
   c(avg_2021, avg_2023)

Exercise 6: Select the Last N Observations

Subset the “META” data to get the last 8 weeks of observations before September 30, 2024.

Solution:

tail(META["/2024-09-30"], n=8)