# Load libraries
library(tidyquant)
##### Basic Functionality
fb_stock_prices <- tq_get("FB", get = "stock.prices")
# Example 1: Return logarithmic daily returns using periodReturn()
fb_stock_prices %>%
tq_mutate(ohlc_fun = Cl, mutate_fun = periodReturn,
period = "daily", type = "log")
# Example 2: Use tq_mutate_xy to use functions with two columns required
fb_stock_prices %>%
tq_mutate_xy(x = close, y = volume, mutate_fun = EVWMA,
col_rename = "EVWMA")
# Example 3: Using tq_mutate_xy to work with non-OHLC data
tq_get("DCOILWTICO", get = "economic.data") %>%
tq_mutate_xy(x = price, mutate_fun = lag.xts, k = 1, na.pad = TRUE)
# Example 4: Non-standard evaluation:
# Programming with tq_mutate_() and tq_mutate_xy_()
col_name <- "adjusted"
mutate <- c("MACD", "SMA")
tq_mutate_xy_(fb_stock_prices, x = col_name, mutate_fun = mutate[[1]])
Run the code above in your browser using DataLab