Skip to content

Commit b9eb8ca

Browse files
committed
#250 tq_fund_holdings()
1 parent 13402a0 commit b9eb8ca

File tree

5 files changed

+153
-16
lines changed

5 files changed

+153
-16
lines changed

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ export(tidyquant_conflicts)
162162
export(tiingo_api_key)
163163
export(tq_exchange)
164164
export(tq_exchange_options)
165+
export(tq_fund_holdings)
166+
export(tq_fund_source_options)
165167
export(tq_get)
166168
export(tq_get_options)
167169
export(tq_index)

NEWS.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# tidyquant 1.0.9
22

3-
- Fixes to CRAN's API policy:
3+
## New Function:
4+
5+
- `tq_fund_holdings()`: Retrieves the fund holdings and compositions for a fund and source combination. Example: `tq_fund_holdings("SPY", source = "SSGA")` #250
6+
7+
## Fixes and Improvements:
8+
9+
- Fixes to CRAN's API policy #249:
410

511
> "Packages which use Internet resources should fail gracefully with an
612
informative message if the resource is not available or has changed (and
713
not give a check warning nor error)."
814

915

1016

17+
1118
# tidyquant 1.0.8
1219

1320
## TQ INDEX AND EXCHANGE:

R/tq_stock_list.R

+110-12
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,21 @@
2222
#' `tq_exchange_options()` returns a list of stock exchanges you can
2323
#' choose from. The options are AMEX, NASDAQ and NYSE.
2424
#'
25+
#' `tq_fund_holdings()` returns the the stock symbol, company name, weight, and sector of every stock
26+
#' in an fund. The `source` parameter specifies which investment management company to use.
27+
#' Example: `source = "SSGA"` connects to State Street Global Advisors (SSGA).
28+
#' If `x = "SPY"`, then SPDR SPY ETF holdings will be returned.
29+
#'
30+
#' `tq_fund_source_options()`: returns the options that can be used for the `source` API for `tq_fund_holdings()`.
31+
#'
2532
#' @seealso
2633
#' [tq_get()] to get stock prices, financials, key stats, etc using the stock symbols.
2734
#'
2835
#'
2936
#' @examples
3037
#'
38+
#' # Stock Indexes:
39+
#'
3140
#' # Get the list of stock index options
3241
#' tq_index_options()
3342
#'
@@ -36,6 +45,8 @@
3645
#' tq_index("DOW")
3746
#' }
3847
#'
48+
#' # Stock Exchanges:
49+
#'
3950
#' # Get the list of stock exchange options
4051
#' tq_exchange_options()
4152
#'
@@ -44,6 +55,16 @@
4455
#' tq_exchange("NYSE")
4556
#' }
4657
#'
58+
#' # Mutual Funds and ETFs:
59+
#'
60+
#' # Get the list of stock exchange options
61+
#' tq_fund_source_options()
62+
#'
63+
#' # Get all stocks in a fund
64+
#' \dontrun{
65+
#' tq_fund_holdings("SPY", source = "SSGA")
66+
#' }
67+
#'
4768
#' @name tq_index
4869
#' @export
4970

@@ -95,7 +116,7 @@ tq_index <- function(x, use_fallback = FALSE) {
95116

96117
# Download the index data
97118
dload <- tryCatch({
98-
index_download(x_spdr, index_name = x)
119+
ssga_download(x_spdr, index_name = x)
99120
}, error = function(e) {
100121
warning(paste("Error downloading index data:", e$message), call. = FALSE)
101122
return(NULL)
@@ -118,6 +139,18 @@ tq_index <- function(x, use_fallback = FALSE) {
118139
df
119140
}
120141

142+
#' @rdname tq_index
143+
#' @export
144+
tq_index_options <- function() {
145+
c(
146+
"DOW",
147+
"DOWGLOBAL",
148+
"SP400",
149+
"SP500",
150+
"SP600"
151+
)
152+
}
153+
121154

122155
# tq_exchange ----
123156

@@ -215,21 +248,69 @@ tq_exchange <- function(x) {
215248

216249
#' @rdname tq_index
217250
#' @export
218-
tq_index_options <- function() {
219-
c(
220-
"DOW",
221-
"DOWGLOBAL",
222-
"SP400",
223-
"SP500",
224-
"SP600"
225-
)
251+
tq_exchange_options <- function() {
252+
c("AMEX", "NASDAQ", "NYSE")
226253
}
227254

255+
# tq_fund_holdings ----
228256

229257
#' @rdname tq_index
258+
#' @param source The API source to use.
230259
#' @export
231-
tq_exchange_options <- function() {
232-
c("AMEX", "NASDAQ", "NYSE")
260+
tq_fund_holdings <- function(x, source = "SSGA") {
261+
262+
# Verify index
263+
verified <- tryCatch({
264+
verify_fund_source(source)
265+
}, error = function(e) {
266+
warning(paste("Error verifying index:", e$message), call. = FALSE)
267+
return(NULL)
268+
})
269+
270+
# If verification failed or not a verified index, return a warning and empty tibble
271+
if(is.null(verified) || !verified$is_verified) {
272+
warning(verified$err)
273+
return(tibble::tibble())
274+
}
275+
276+
# Download the index data
277+
dload <- tryCatch({
278+
source <- stringr::str_to_upper(source)
279+
280+
if (source == "SSGA") {
281+
ssga_download(x, index_name = x)
282+
} else {
283+
284+
}
285+
286+
287+
}, error = function(e) {
288+
warning(paste("Error downloading index data:", e$message), call. = FALSE)
289+
return(NULL)
290+
})
291+
292+
# If download failed, return a warning and empty tibble
293+
if(is.null(dload) || !is.null(dload$err)) {
294+
warning(dload$err)
295+
return(tibble::tibble())
296+
}
297+
298+
# Clean holdings
299+
df <- tryCatch({
300+
clean_holdings(dload$df)
301+
}, error = function(e) {
302+
warning(paste("Error cleaning index holdings:", e$message), call. = FALSE)
303+
return(tibble::tibble())
304+
})
305+
306+
df
307+
308+
}
309+
310+
#' @rdname tq_index
311+
#' @export
312+
tq_fund_source_options <- function() {
313+
c("SSGA")
233314
}
234315

235316
# Utility ----------------------------------------------------------------------------------------------------
@@ -261,6 +342,23 @@ verify_index <- function(x) {
261342
verified
262343
}
263344

345+
verify_fund_source <- function(x) {
346+
347+
# Setup with initial values
348+
verified <- list(is_verified = FALSE, err = "")
349+
350+
if(!(x %in% tq_fund_source_options())) {
351+
352+
verified$err <- paste0(x, " must be a character string in the form of a valid Fund Source. ",
353+
"The following are valid options:\n",
354+
stringr::str_c(tq_fund_source_options(), collapse = ", "))
355+
} else {
356+
verified$is_verified <- TRUE
357+
}
358+
359+
verified
360+
}
361+
264362
# Map the index to the SPDR ETF name
265363
spdr_mapper <- function(x) {
266364

@@ -281,7 +379,7 @@ spdr_mapper <- function(x) {
281379
}
282380

283381
# Download the index data from SPDR
284-
index_download <- function(x, index_name) {
382+
ssga_download <- function(x, index_name) {
285383

286384
# Contruct download link
287385
# OLD (< 2019-12-15): https://us.spdrs.com/site-content/xls/SPY_All_Holdings.xls

_pkgdown.yml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ reference:
4949
- starts_with("tq_get")
5050
- starts_with("tq_index")
5151
- starts_with("tq_exchange")
52+
- starts_with("tq_fund")
5253
- subtitle: API Keys
5354
contents:
5455
- starts_with("quandl")

man/tq_index.Rd

+32-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)