##Prerequisites
# Load the tidyquant package to get started.
#Loads tidyquant, lubridate, xts, quantmod, TTR
library(tidyverse)
library(tidyquant)
#Stock Indexes
tq_index_options()
#Set x as one of the options in the list of options above to get the desired stock index / exchange.
tq_index("SP500")
tq_get_options()
#yahoo finance
aapl_prices <- tq_get("AAPL", get = "stock.prices", from = " 1990-01-01")
aapl_prices
x8411T <- tq_get("8411.T", get = "stock.prices.japan", from = "2016-01-01", to = "2016-12-31")
#FRED Economic Data
wti_price_usd <- tq_get("DCOILWTICO", get = "economic.data")
wti_price_usd
##Quandl API
quandl_api_key("<your-api-key>")
#Mutate Quantitative Data
data("FANG")
FANG
##Transmute Quantitative Data, tq_transmute
FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted, mutate_fun = to.monthly, indexAt = "lastof")
##Mutate Quantitative Data, tq_mutate
FANG %>%
group_by(symbol) %>%
tq_mutate(select = close,
mutate_fun = MACD,
col_rename = c("MACD", "Signal"))
#Quantitative Analysis Package Integrations in tidyquant ##Getting and Charting Annual Returns
FANG_annual_returns <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
type = "arithmetic")
FANG_annual_returns
###Charting annual returns is just a quick use of the ggplot2 package.
FANG_annual_returns %>%
ggplot(aes(x = date, y = yearly.returns, fill = symbol)) +
geom_col() +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
scale_y_continuous(labels = scales::percent) +
labs(title = "FANG: Annual Returns",
subtitle = "Get annual returns quickly with tq_transmute!",
y = "Annual Returns", x = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
theme_tq() +
scale_fill_tq()
##Getting Daily Log Returns
FANG_daily_log_returns <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
type = "log",
col_rename = "monthly.returns")
FANG_daily_log_returns %>%
ggplot(aes(x = monthly.returns, fill = symbol)) +
geom_density(alpha = 0.5) +
labs(title = "FANG: Charting the Daily Log Returns",
x = "Monthly Returns", y = "Density") +
theme_tq() +
scale_fill_tq() +
facet_wrap(~ symbol, ncol = 2)
##Use xts to.period to Change the Periodicity from Daily to Monthly
FANG %>%
group_by(symbol) %>%
tq_transmute(select = open:volume,
mutate_fun = to.period,
period = "months")
FANG_daily <- FANG %>%
group_by(symbol)
FANG_daily %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line(size = 1) +
labs(title = "Daily Stock Prices",
x = "", y = "Adjusted Prices", color = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
#Scaling and Modeling with tidyquant ##Scaling the Mutation of Financial Data
data("FANG")
FANG
FANG_returns_yearly <- FANG %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
col_rename = "yearly.returns")
FANG_returns_yearly %>%
ggplot(aes(x = year(date), y = yearly.returns, fill = symbol)) +
geom_bar(position = "dodge", stat = "identity") +
labs(title = "FANG: Annual Returns",
subtitle = "Mutating at scale is quick and easy!",
y = "Returns", x = "", color = "Blue") +
scale_y_continuous(labels = scales::percent) +
coord_flip() +
theme_tq() +
scale_fill_tq()
#Charting with tidyquant ##Line Chart
#Use FANG data set
data("FANG")
# Get AAPL and AMZN Stock Prices
AAPL <- tq_get("AAPL", get = "stock.prices", from = "2016-09-01", to = "2017-12-31")
AMZN <- tq_get("AMZN", get = "stock.prices", from = "2001-01-01", to = "2017-12-31")
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_line() +
labs(title = "AAPL Line Chart", y = "Closing Price", x = "") +
theme_tq()
end <- as_date("2016-12-31")
AAPL %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close)) +
labs(title = "AAPL Bar Chart", y = "Closing Price", x = "") +
theme_tq()
##Charting Multiple Securities
start <- end - weeks(6)
FANG %>%
filter(date >= start - days(2 * 15)) %>%
ggplot(aes(x = date, y = close, group = symbol)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
labs(title = "FANG Candlestick Chart",
subtitle = "Experimenting with Mulitple Stocks",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2, scale = "free_y") +
theme_tq()
##Adding BBands to multiple stocks
start <- end - weeks(24)
FANG %>%
filter(date >= start - days(2 * 20)) %>%
ggplot(aes(x = date, y = close,
open = open, high = high, low = low, close = close,
group = symbol)) +
geom_barchart() +
geom_bbands(ma_fun = SMA, sd = 2, n = 20, linetype = 5) +
labs(title = "FANG Bar Chart",
subtitle = "BBands with SMA Applied, Experimenting with Multiple Stocks",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
theme_tq()