Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getQuote.yahoo doesn't work correctly with tickers from different exchanges in other timezones #246

Closed
pverspeelt opened this issue Sep 20, 2018 · 3 comments
Labels

Comments

@pverspeelt
Copy link
Contributor

pverspeelt commented Sep 20, 2018

Description

When retrieving stock quote information from yahoo, you get an error when you have tickers from different exchanges with different timezones. If the number of tickers from each exchange are the same the function works (by accident). But if they have a different length, the function fails.

Minimal, reproducible example

library(quantmod)

# works
metrics <- yahooQF(c("Name", "Volume", "P/E Ratio", "Dividend Yield", "Shares Outstanding"))
symbols <- c("GOOG", "FAGR.BR")
stats <- getQuote(symbols, what = metrics)

# doesn't work
symbols <- c("MSFT", "GOOG", "FAGR.BR")
stats <- getQuote(symbols, what = metrics)

I traced the problem to the part of the getQuote.yahoo function where the timezones are converted.

    Qposix <-sapply(split(sq, sq$exchangeTimezoneName), 
                     convertTZ)

This part returns a list of timezones, instead of a vector of timezones and when the data is transformed into a data.frame this part errors as the lengths of this variable are not the same as the length of the symbols.

If this part of the function is changed to the following code, the getQuote.yahoo works again as expected.
Qposix <- unlist(sapply(split(sq, sq$exchangeTimezoneName), convertTZ))

More info on this SO post. A workaround is the request the info per exchange.

If you want I can create a pull request to fix this.

Session Info

R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252    LC_MONETARY=Dutch_Netherlands.1252
[4] LC_NUMERIC=C                       LC_TIME=Dutch_Netherlands.1252    

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

other attached packages:
[1] quantmod_0.4-13 TTR_0.23-3      xts_0.11-1      zoo_1.8-3       xml2_1.2.0     

loaded via a namespace (and not attached):
[1] compiler_3.5.0  tools_3.5.0     curl_3.2        yaml_2.2.0      Rcpp_0.12.18    grid_3.5.0      jsonlite_1.5    lattice_0.20-35
@joshuaulrich
Copy link
Owner

Thanks for the report! I can replicate. I don't believe I had a use case in mind when I wrote that code, but I anticipated it would be a problem.

I'm not sure the current approach is required. It looks like regularMarketTime is UTC, so we might be able to do something as simple as .POSIXct(sq$regularMarketTime, tz = NULL) to get all the exchange-local market times into the user-local time. What do you think?


Also, thanks for all your great answers on SO! I don't check the site as often as I used to, and I often find that you have answered questions quickly and adequately.

@pverspeelt
Copy link
Contributor Author

You are welcome.

I tested your suggestion. The outcome is indeed to simply change all of the following:

  convertTZ <- function(x) {
     tz <- x$exchangeTimezoneName[1]
     times <- .POSIXct(x$regularMarketTime, tz)
     attr(times, "tzone") <- NULL
     times
   }
   Qposix <- sapply(split(sq, sq$exchangeTimezoneName), 
     convertTZ)
   Qposix <- .POSIXct(Qposix, tz = NULL)

with:
Qposix <- .POSIXct(sq$regularMarketTime, tz = NULL)

That returns everything in (the user's) local time.

pverspeelt added a commit to pverspeelt/quantmod that referenced this issue Oct 8, 2018
When getQuote.yahoo was called it genereted errors when quotes from multiple timezones were involved. Fixed so everything is forced to the local timezone

Fixes joshuaulrich#246
@joshuaulrich joshuaulrich added this to the Release 0.4-14 milestone Nov 24, 2018
@pedrojsolo
Copy link

Hi, It is very nice to read you both

Nowadays I have same problem, I read the post "quantmod - getQuote() not working for tickers on different exchanges"
And I understood that the problem is in this part of the function:
Qposix <- sapply(split(sq, sq$exchangeTimezoneName), convertTZ)

So my question is, how can I fix this bug in the function that needs a change in the quantmod package?

I'd thank you a lot,
regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants