# 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_transform(ohlc_fun = Cl, transform_fun = periodReturn,
period = "daily", type = "log")
# Example 2: Use tq_transform_xy to use functions with two columns required
fb_stock_prices %>%
tq_transform_xy(x = close, y = volume, transform_fun = EVWMA,
col_rename = "EVMWA")
# Example 3: Using tq_transform_xy to work with non-OHLC data
tq_get("DCOILWTICO", get = "economic.data") %>%
tq_transform_xy(x = price, transform_fun = to.period, period = "months")
# Example 4: Non-standard evaluation:
# Programming with tq_tranform_() and tq_transform_xy_()
col_name <- "adjusted"
transform <- "periodReturn"
period <- c("daily", "weekly", "monthly")
tq_transform_xy_(fb_stock_prices, x = col_name, transform_fun = transform,
period = period[[1]])
Run the code above in your browser using DataLab