joshuaulrich / quantmod

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

getQuote does not work when some metrics are not requested #255

Closed mehdiMBH closed 1 year ago

mehdiMBH commented 5 years ago

Description

The function getQuote does not work when I don't request some specific metrics in the what argument. After taking a deeper look at the code and the JSON, it turns out that Yahoo does not return (anymore?) the regularMarketTime when metrics - such as the price, bid, ask and I don't know what else - are not requested.

For example, if you request only the shares outstanding and earnings per share, then you get an error.

what_metrics <- yahooQF(c("Shares Outstanding",
                          "Earnings/Share"))

getQuote("AAPL", what=what_metrics)

Error returned:

Error in [.data.frame(sq, , "regularMarketTime") : undefined columns selected

That's normal because if you look at the JSON, there is not regularMarketTime variable: https://query1.finance.yahoo.com/v7/finance/quote?symbols=AAPL&fields=sharesOutstanding,epsTrailingTwelveMonths

However, if you request also, say, the bid price, then all works well:

what_metrics <- yahooQF(c("Shares Outstanding",
                          "Earnings/Share",
                           "Bid"))

getQuote("AAPL", what=what_metrics)

That's because the JSON returned by Yahoo actually provides regularMarketTime: https://query1.finance.yahoo.com/v7/finance/quote?symbols=AAPL&fields=sharesOutstanding,epsTrailingTwelveMonths,bid

I guess this should not be too difficult to fix in getQuote's code. A possible solution would be tto automatically query the regularMarketTime from Yahoo when the user asks for a customized list of metrics. So if I ask for shares outstanding only, we would append ",regularMarketTime" to the URL, which would then look like: https://query1.finance.yahoo.com/v7/finance/quote?symbols=AAPL&fields=sharesOutstanding,epsTrailingTwelveMonths,regularMarketTime This could be done in the getQuote function or the YahooQF function I guess.

FYI, here is my R session info:

R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS  10.13.3

locale:
[1] C

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

other attached packages:
[1] rvest_0.3.2      xml2_1.1.1       lubridate_1.7.1  testit_0.8       finreportr_1.0.1 quantmod_0.4-13  TTR_0.23-2      
[8] xts_0.11-0       zoo_1.7-14      

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     bindr_0.1.1      magrittr_1.5     tidyselect_0.2.4 lattice_0.20-34  R6_2.2.2         rlang_0.2.0     
 [8] httr_1.3.1       stringr_1.3.1    dplyr_0.7.6      tools_3.3.2      grid_3.3.2       selectr_0.3-1    assertthat_0.2.0
[15] tibble_1.3.4     Matrix_1.2-12    bindrcpp_0.2.2   purrr_0.2.5      curl_2.3         glue_1.3.0       stringi_1.2.4   
[22] compiler_3.3.2   jsonlite_1.5     pkgconfig_2.0.2 

Cheers,

Mehdi

joshuaulrich commented 5 years ago

Thanks for the report and suggestion! I'm working on getQuote.tiingo() (#247, #250), and I also see the need for some validation of the results returned from the various providers.

joshuaulrich commented 1 year ago

...better late than never...