This assignment demonstrates the use of window functions in R to calculate year-to-date averages and moving averages on time series data.
We use daily stock prices for Apple (AAPL) and Microsoft (MSFT) starting from January 1, 2022.
library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
## ✔ PerformanceAnalytics 2.0.8 ✔ TTR 0.24.4
## ✔ quantmod 0.4.28 ✔ xts 0.14.1
## ── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date() masks base::as.Date()
## ✖ zoo::as.Date.numeric() masks base::as.Date.numeric()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary() masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:xts':
##
## first, last
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(zoo)
stocks <- tq_get(
c("AAPL", "MSFT"),
from = "2022-01-01",
to = Sys.Date()
)
head(stocks)
## # A tibble: 6 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2022-01-03 178. 183. 178. 182. 104487900 178.
## 2 AAPL 2022-01-04 183. 183. 179. 180. 99310400 176.
## 3 AAPL 2022-01-05 180. 180. 175. 175. 94537600 171.
## 4 AAPL 2022-01-06 173. 175. 172. 172 96904000 168.
## 5 AAPL 2022-01-07 173. 174. 171. 172. 86709100 168.
## 6 AAPL 2022-01-10 169. 172. 168. 172. 106765600 168.
stocks_metrics <- stocks %>%
arrange(symbol, date) %>%
group_by(symbol) %>%
mutate(
ytd_avg = cummean(adjusted),
ma_6day = rollmean(adjusted, 6, fill = NA, align = "right")
)
head(stocks_metrics)
## # A tibble: 6 × 10
## # Groups: symbol [1]
## symbol date open high low close volume adjusted ytd_avg ma_6day
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2022-01-03 178. 183. 178. 182. 104487900 178. 178. NA
## 2 AAPL 2022-01-04 183. 183. 179. 180. 99310400 176. 177. NA
## 3 AAPL 2022-01-05 180. 180. 175. 175. 94537600 171. 175. NA
## 4 AAPL 2022-01-06 173. 175. 172. 172 96904000 168. 173. NA
## 5 AAPL 2022-01-07 173. 174. 171. 172. 86709100 168. 172. NA
## 6 AAPL 2022-01-10 169. 172. 168. 172. 106765600 168. 172. 172.