# Load packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ 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)
## 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_exchange_options()
## [1] "AMEX" "NASDAQ" "NYSE"
#tq_exchange("NYSE")
tq_get
stock prices from Yahoo Finance
stock <- tq_get("TSLA")
economic data from FRED
unemployment_nh <- tq_get("NHUR", get = "economic.data")
unemployment_nh
## # A tibble: 127 × 3
## symbol date price
## <chr> <date> <dbl>
## 1 NHUR 2013-01-01 5.6
## 2 NHUR 2013-02-01 5.5
## 3 NHUR 2013-03-01 5.5
## 4 NHUR 2013-04-01 5.4
## 5 NHUR 2013-05-01 5.3
## 6 NHUR 2013-06-01 5.2
## 7 NHUR 2013-07-01 5.2
## 8 NHUR 2013-08-01 5.2
## 9 NHUR 2013-09-01 5.1
## 10 NHUR 2013-10-01 5.1
## # ℹ 117 more rows