#' Rollapply indicator function parallely by calculation windows
#'
#' @param data xts data
#' @param windows indicator calculation window (e.g. SMA window)
#' @param ... indicator function and additional params
#'
#' @importFrom foreach %dopar%
#' @importFrom foreach %do%
#'
#' @return All stock data of specified column as data.table format
#' @export
calcIndicatorParallel <- function(data, windows, ...) {
# Indicator window must be greater than 1
windows <- windows[windows > 1]
# Skip
if (length(windows) == 0) {
return (NULL)
# Parallel
} else if (length(windows) >= 4) {
# Prepare parallel foreach
pkgs <- utils::sessionInfo()$otherPkgs %>% names()
cl <- parallel::makeCluster(parallel::detectCores())
doParallel::registerDoParallel(cl)
on.exit(parallel::stopCluster(cl))
result <- foreach::foreach(i = 1:length(windows),
.packages = pkgs) %dopar% {
zoo::rollapply(data, windows[i], ...)
}
# Not parallel
} else {
result <- foreach::foreach(i = 1:length(windows)) %do% {
zoo::rollapply(data, windows[i], ...)
}
}
names(result) <- windows
return(result)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.