22
22
# ' `tq_exchange_options()` returns a list of stock exchanges you can
23
23
# ' choose from. The options are AMEX, NASDAQ and NYSE.
24
24
# '
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
+ # '
25
32
# ' @seealso
26
33
# ' [tq_get()] to get stock prices, financials, key stats, etc using the stock symbols.
27
34
# '
28
35
# '
29
36
# ' @examples
30
37
# '
38
+ # ' # Stock Indexes:
39
+ # '
31
40
# ' # Get the list of stock index options
32
41
# ' tq_index_options()
33
42
# '
36
45
# ' tq_index("DOW")
37
46
# ' }
38
47
# '
48
+ # ' # Stock Exchanges:
49
+ # '
39
50
# ' # Get the list of stock exchange options
40
51
# ' tq_exchange_options()
41
52
# '
44
55
# ' tq_exchange("NYSE")
45
56
# ' }
46
57
# '
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
+ # '
47
68
# ' @name tq_index
48
69
# ' @export
49
70
@@ -95,7 +116,7 @@ tq_index <- function(x, use_fallback = FALSE) {
95
116
96
117
# Download the index data
97
118
dload <- tryCatch({
98
- index_download (x_spdr , index_name = x )
119
+ ssga_download (x_spdr , index_name = x )
99
120
}, error = function (e ) {
100
121
warning(paste(" Error downloading index data:" , e $ message ), call. = FALSE )
101
122
return (NULL )
@@ -118,6 +139,18 @@ tq_index <- function(x, use_fallback = FALSE) {
118
139
df
119
140
}
120
141
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
+
121
154
122
155
# tq_exchange ----
123
156
@@ -215,21 +248,69 @@ tq_exchange <- function(x) {
215
248
216
249
# ' @rdname tq_index
217
250
# ' @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" )
226
253
}
227
254
255
+ # tq_fund_holdings ----
228
256
229
257
# ' @rdname tq_index
258
+ # ' @param source The API source to use.
230
259
# ' @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" )
233
314
}
234
315
235
316
# Utility ----------------------------------------------------------------------------------------------------
@@ -261,6 +342,23 @@ verify_index <- function(x) {
261
342
verified
262
343
}
263
344
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
+
264
362
# Map the index to the SPDR ETF name
265
363
spdr_mapper <- function (x ) {
266
364
@@ -281,7 +379,7 @@ spdr_mapper <- function(x) {
281
379
}
282
380
283
381
# Download the index data from SPDR
284
- index_download <- function (x , index_name ) {
382
+ ssga_download <- function (x , index_name ) {
285
383
286
384
# Contruct download link
287
385
# OLD (< 2019-12-15): https://us.spdrs.com/site-content/xls/SPY_All_Holdings.xls
0 commit comments