q_return <- function(ticker, start_date = NULL, end_date = NULL, frequency = NULL){
if(is.null(frequency)){
frequency = "daily"
}
prices <- q_price(ticker = ticker,
start_date = start_date,
end_date = end_date,
frequency = frequency)
if(length(unique(prices$q_ticker)) > 1){
prices <- prices %>%
group_by(q_ticker)
}
return_name <- paste0(frequency,"_return")
df <- prices %>%
dplyr::select(q_ticker,date,adjClose) %>%
tq_mutate(select = adjClose,
mutate_fun = periodReturn,
period = frequency,
col_rename = "return") %>%
mutate(cumulative_return = with_order(date,cumprod,1+return)-1) %>%
ungroup() %>%
rename(!!return_name := return)
df <- df %>%
dplyr::filter(date != min(date, na.rm = T))
df
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.