joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
818 stars 224 forks source link

getQuote fails if any symbols are not found #279

Closed ethanbsmith closed 4 years ago

ethanbsmith commented 5 years ago

Description

getQuote.yahoo fails if yahoo does not recognize any of the symbols. This is particularly problematic when a very large list of quotes is requested as identifying the offending symbol is near impossible. Since the identifying Symbol may be perfectly valid, but yahoo just doesn't recognize it. eg:

DEENF which is a valid OTC ticker: https://www.otcmarkets.com/stock/DEENF/overview

Expected behavior

ideally getQuote would return the values for the found symbols and NAs for the ones that weren't found

Minimal, reproducible example

> getQuote(c("DE", "DEENF"), src = "yahoo")
Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length

Session Info

> sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] doParallel_1.0.14    iterators_1.0.11     foreach_1.5.1        data.table_1.12.2    curl_3.3             rvest_0.3.4          xml2_1.2.0           quantmod_0.4-15      TTR_0.23-4          
[10] xts_0.11-2           zoo_1.8-6            RODBC_1.3-15         plotrix_3.7-6        RevoUtils_11.0.3     checkpoint_0.4.4     RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1       magrittr_1.5     lattice_0.20-38  R6_2.3.0         stringr_1.4.0    httr_1.4.0       tools_3.5.3      grid_3.5.3       selectr_0.4-1    codetools_0.2-16 stringi_1.4.3    compiler_3.5.3  
[13] jsonlite_1.5 
ethanbsmith commented 5 years ago

I think the main jist of the fix is to replace line 59: sq <- response$quoteResponse$result with: sq <- merge.data.frame(data.frame(symbol = Symbols), response$quoteResponse$result, by = "symbol", all = T)

if I can find some time, I'll try cruft up a pr

joshuaulrich commented 4 years ago

Thanks for the report and patch! Your suggestion is very close, but wouldn't quite work because Symbols would be "DE,DEENF" at line 59. You would need to do it after line 80.

It might also be good to only do the merge conditioned on NROW(sq) != length(Symbols). Also, it's not good practice to call methods directly, so I would change merge.data.frame() to just be merge(), and I would add a stringsAsFactors = FALSE to the data.frame constructor, just to be safe.

So something like:

if (length(Symbols) != NROW(sq)) {
  sq <- merge(data.frame(symbol = Symbols, stringsAsFactors = FALSE),
              sq, by = "symbol", all = TRUE)
}

Please double-check that works, and make a PR when you're ready. Thanks!

joshuaulrich commented 4 years ago

This was fixed in #282. Closing.