date_start <- "2011-01-01"
date_end <- "2024-08-31"
# Collect index_data for S&P 500 and VIX indexes ----
# Source: http://finance.yahoo.com
#
index_data <- tq_get(c("^GSPC", "^VIX"),
get = "stock.prices",
from = date_start,
to = date_end)
# Collect stock_data for stocks ----
# Source: http://finance.yahoo.com
stock_data <- tq_get(c("NVDA",
"GE",
"AAPL",
"GOOG",
"AMZN",
"XOM",
"GME",
"ARKK",
"AMC",
"BTC-USD",
"XLF"),
get = "stock.prices",
from = date_start,
to = date_end)
# Source: St. Louis Federal Reserve
## http://research.stlouisfed.org/fred2/
#
# Series name | Description
# ------------ --------------
# SP500 | SP500 Stock market index
# VIXCLS | Vix volatility index
#
# DGS3MO | 3-Month Treasury, constant maturity rate
# DGS1 | 1-Year Treasury, constant maturity rate
# DGS5 | 5-Year Treasury, constant maturity rate
# DGS10 | 10-Year Treasury, constant maturity rate
#
# DAAA | Moody's Seasoned Aaa Corporate Bond Yield
# DBAA | Moody's Seasoned Baa Corporate Bond Yield
#
# DCOILWTICO | Crude Oil Prices: West Text Intermediate (WTI) - Cushing, Oklahoma
# CBBTCUSD | Coinbase Bitcoin
economic_data <- tq_get(c(
"SP500",
"VIXCLS",
"DGS3MO",
"DGS1",
"DGS5",
"DGS10",
"DAAA",
"DBAA",
"DCOILWTICO",
"CBBTCUSD"),
get = "economic.data",
from = date_start,
to = date_end)
The following code creates the object market_data. For each dataset:
## 2.1 Select three columns (symbol, date, price)
## 2.2 Rename adjusted to price (for stocks/indexes)----
## 2.3 Remove NA observations
index_data1 <- index_data %>%
dplyr::select(symbol, date, adjusted) %>%
dplyr::filter(!is.na(adjusted)) %>%
dplyr::rename(price = adjusted)
stock_data1 <- stock_data %>%
dplyr::select(symbol, date, adjusted) %>%
dplyr::filter(!is.na(adjusted)) %>%
dplyr::rename(price = adjusted)
# economic_data has same 3 columns (no select/rename needed)
economic_data1 <- economic_data %>%
dplyr::filter(!is.na(price))
market_data <- index_data1 %>%
full_join(stock_data1) %>%
full_join(economic_data1)
## Joining with `by = join_by(symbol, date, price)`
## Joining with `by = join_by(symbol, date, price)`
market_data %>%
filter(symbol == "SP500") %>%
ggplot(market_data, mapping = aes(date, price)) +
geom_line() +
labs(title="S&P 500 Index", x="Date", y="Price")
market_data %>%
filter(symbol == "VIXCLS") %>%
ggplot(market_data, mapping = aes(date, price)) +
geom_line() +
labs(title="VIX Index", x="Date", y="Value")
market_data %>%
filter(symbol %in% c("SP500","VIXCLS")) %>%
ggplot( mapping = aes(date,price),col=symbol) +
geom_line() +
facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
labs(x="Date", y="Price/Value", title="Index Time Series: S&P 500 and VIX")
market_data %>%
filter(symbol %in% c("ARKK")) %>%
ggplot( mapping = aes(date,price)) +
geom_line() +
facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
labs(x="Date", y="Price",
title="Price Time Series: ARKK")
market_data %>%
filter(symbol %in% c("GME")) %>%
ggplot( mapping = aes(date,price)) +
geom_line() +
facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
labs(x="Date", y="Price",
title="Price Time Series: GME")
market_data %>%
filter(symbol %in% c("BTC-USD")) %>%
ggplot( mapping = aes(date,price)) +
geom_line() +
facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
labs(x="Date", y="Price",
title="Price Time Series: Bitcoin")
market_data %>%
filter(symbol %in% c("NVDA")) %>%
ggplot( mapping = aes(date,price)) +
geom_line() +
facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
labs(x="Date", y="Price",
title="Price Time Series: Nvidia")
list_yields<-c("DGS3MO","DGS1", "DGS5","DGS10","DAAA","DBAA")
market_data %>%
filter(symbol %in% list_yields) %>%
mutate(Series = factor(symbol, levels=rev(list_yields))) %>%
ggplot(market_data,
mapping = aes(date, price, color = Series)) +
geom_line() +
labs(x="Date", y="Yield (Percent)",
title="Yield Time Series from Federal Reserve Economic Database (FRED) \nUS Treasurys (3mo,1,5,10 yr) \nCorporates (AAA, BAA)")
market_data %>%
filter(symbol == "DCOILWTICO") %>%
ggplot(market_data, mapping = aes(date, price)) +
geom_line() +
geom_abline(intercept=0,slope=0,col='red',lwd=2)+
labs(x="Date", y="Price", title="Time Series of Crude Oil Future (DCOILWTICO)")
save(file="data_fm_intro1.Rdata",list=ls())