#' get names and weights of the holdings of SPDR ETFs #' @param Symbols character vector of SPDR ETF symbols #' @param env where to store holdings #' @param auto.assign should the results be assigned in \code{env}? #' @return if \code{auto.assign} is TRUE, holdings will be assigned as #' the ETF symbols appended with \dQuote{.h}, and the names of those objects #' will be returned. Otherwise, if \code{Symbols} is only one symbol, its' #' holdings will be returned. If \code{Symbols} is of length greater than #' one, a list will be returned where each element is the holdings of a #' different ETF. #' @author Garrett See #' @seealso \code{FinancialInstrument:::getHoldings}, #' \code{gsee:::getHoldings.iShares.AsOf} #' @references \url{https://www.spdrs.com/} #' @examples #' \dontrun{ #' getHoldings.SPDR("SPY") #' SPY.h #' } #' @export getHoldings.SPDR <- function(Symbols, env=.GlobalEnv, auto.assign=TRUE) { hlist <- lapply(Symbols, function(symbol) { lnk <- paste0("https://www.spdrs.com/site-content/csv/", symbol, "_All_Holdings.csv?fund=", symbol, "&docname=All+Holdings&", "onyx_code1=1286&onyx_code2=1521") lnk <- paste0("https://www.spdrs.com/site-content/csv/", symbol, "_All_Holdings.csv?fund=", symbol, "&docname=All+Holdings") tmp <- tempfile() download.file(lnk, destfile=tmp, method='curl') fr <- read.csv(tmp, skip=3) unlink(tmp) out <- fr[, c(2, 1, 4)] colnames(out)[1] <- paste(symbol, "Weight", sep=".") rownames(out) <- fr[, 3] out }) names(hlist) <- Symbols if (isTRUE(auto.assign)) { lapply(Symbols, function(x) { assign(paste(x, "h", sep="."), hlist[[x]], pos=env) }) return(paste(Symbols, "h", sep=".")) } if (length(hlist) > 1) { return(hlist) } else return(hlist[[1L]]) }