quantmod - getQuote() not working for tickers on different exchanges

Viewed 1009

I am going through the quantmod-getQuote() to find a solution to the problem I am running into (link).

When I run the following;

metrics <- yahooQF(c("Name", "Volume", "P/E Ratio", "Dividend Yield", "Shares Outstanding"))
symbols2 <- c("GOOG", "MSFT")
stats <- getQuote(symbols2, what = metrics)

I am able to obtain some yahoo finance statistics on GOOG and MSFT. However when I add the symbol 3888.HK to the list I obtain an error.

symbols2 <- c("GOOG", "MSFT", "3888.HK")
stats <- getQuote(symbols2, what = metrics)

Error message:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 2, 3
In addition: Warning message:
In getQuote.yahoo(Symbols = c("GOOG", "MSFT", "3888.HK"), what = list( :
  symbols have different timezones; converting to local time

Does anybody know of a work around for this, I cannot seem to find what to change in the getQuote() function.

2 Answers

The issue is not with the dot in the ticker name. You actually found a bug in the function.

If you run getQuote("3888.HK", what = metrics) you get a result:

                 Trade Time     Name  Volume P/E Ratio Dividend Yield Shares Outstanding
3888.HK 2018-09-20 16:08:17 KINGSOFT 5549058  4.991025    0.000952381         1365229952

The issue is the different timezone. And if the tickers per timezone are unequal you will get an error.

This works:

symbols2 <- c("GOOG", "MSFT", "0005.HK", "3888.HK")
symbols2 <- c("GOOG", "MSFT", "0005.HK", "3888.HK", "FAGR.BR", "AGFB.BR")

this doesn't

symbols2 <- c("GOOG", "MSFT", "FB", "0005.HK", "3888.HK")
symbols2 <- c("GOOG", "MSFT", "0005.HK", "3888.HK", "FAGR.AB")

The problem is in this part of the function:

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

This returns a list instead of a vector of timezones. And if the number of stocks in per stock exchange are not equal you will get this error. Adjusting this is simple, but needs a change in the quantmod package. I have opened an issue on github.

The workaround for now is to run your code per exchange instead of a big list of tickers in one go.

edit: added github issue.

I may be wrong, but I think the error occurred as you have 2 counters from the NASDAQ but only one counter from the HK Exchange (e.g. "arguments imply differing number of rows" error).

When I tried to get the financials of two counters from NASDAQ and two counters from HK Exchange, the problem gets resolved:

metrics <- yahooQF(c("Name", "Volume", "P/E Ratio", "Dividend Yield", "Shares Outstanding"))
symbols2 <- c("GOOG", "MSFT", "0005.HK", "3888.HK")
stats <- getQuote(symbols2, what = metrics)

Warning message:
In getQuote.yahoo(Symbols = c("GOOG", "MSFT", "0005.HK", "3888.HK" :
  symbols have different timezones; converting to local time

stats
             Trade Time                  Name   Volume P/E Ratio Dividend Yield Shares Outstanding
GOOG    2018-09-20 04:00:01         Alphabet Inc.  1077192 50.576115             NA          349883008
MSFT    2018-09-20 04:00:01 Microsoft Corporation 18559784 52.441310    0.014839678         7668219904
0005.HK 2018-09-20 16:08:17         HSBC HOLDINGS 15168181 18.303152    0.007505519        19422699520
3888.HK 2018-09-20 16:08:17              KINGSOFT  5549058  4.991025    0.000952381         1365229952

Another alternative is you could obtain the financial information by exchange; you do not have to get all counters from all exchanges you are interested in concurrently. Meaning, you could do something like the following:

US_symbols <- c("GOOG", "MSFT")
HK_symbols <- c("0005.HK", "3888.HK")

US_stats <- getQuote(US_symbols, what = metrics)
HK_stats <- getQuote(HK_symbols, what = metrics)

US_stats
          Trade Time                  Name  Volume P/E Ratio Dividend Yield Shares Outstanding
GOOG 2018-09-20 09:39:47         Alphabet Inc.   91776  50.93889             NA          349883008
MSFT 2018-09-20 09:40:07 Microsoft Corporation 1956869  52.62911     0.01504029         7668219904

HK_stats
             Trade Time          Name   Volume P/E Ratio Dividend Yield Shares Outstanding
0005.HK 2018-09-20 16:08:17 HSBC HOLDINGS 15168181 18.303152    0.007505519        19422699520
3888.HK 2018-09-20 16:08:17      KINGSOFT  5549058  4.991025    0.000952381         1365229952
Related