# load packages
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Warning: package 'tidyquant' was built under R version 4.3.3
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## ######################### 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: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
##
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
##
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
Stock index
tq_index_options()
## [1] "DOW" "DOWGLOBAL" "SP400" "SP500" "SP600"
data <- tq_index("SP400")
## Getting holdings for SP400
Stock exchanges
tq_get function
stock price from Yahoo Fincance
stock <- tq_get("TSLA")
economic data from FRED
employment_nh <- wti_price_usd <- tq_get("DCOILWTICO", get = "economic.data")
wti_price_usd
## # A tibble: 2,717 × 3
## symbol date price
## <chr> <date> <dbl>
## 1 DCOILWTICO 2014-01-01 NA
## 2 DCOILWTICO 2014-01-02 95.1
## 3 DCOILWTICO 2014-01-03 93.7
## 4 DCOILWTICO 2014-01-06 93.1
## 5 DCOILWTICO 2014-01-07 93.3
## 6 DCOILWTICO 2014-01-08 91.9
## 7 DCOILWTICO 2014-01-09 91.4
## 8 DCOILWTICO 2014-01-10 92.4
## 9 DCOILWTICO 2014-01-13 91.4
## 10 DCOILWTICO 2014-01-14 92.2
## # ℹ 2,707 more rows