I am trying to pull data into r using quantmod. I am receiving an "Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column" using the "getQuote" function.
I do not get this error when using a hand written list of several stocks (tickers<-c("CCL","RCR","BA")
), but get this error when I try to use a list of tickers pulled from an exchange.
Does anyone know a solution?
Thanks
library(tidyquant) # edited to be included/ it is needed
library(quantmod)
AMEX <-
tq_exchange("AMEX")
tickers <- AMEX$symbol
key_metrics <- what_metrics <- yahooQF(c(
# source information
"Symbol",
"Name",
"Dividend Yield",
"Earnings/Share",
"P/E Ratio",
"trailingPE",
"quoteSourceName",
"Source Interval",
"Exchange Full Name",
"Exchange Data Delay",
"Currency"))
metrics <- getQuote(paste(tickers, sep="", collapse=";"), what=what_metrics)
#downloading set: 1 , 2 , ...done
#Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column
The issue is that you have a lot of tickers that are not valid for yahoo. All the tickers with a "." or a "^" do not return data from yahoo finance. You can check "BIOX.WS" for example on the yahoo finance website. Weirdly enough the json call does not return an error when these are called as yahoo doesn't generate an error but returns an empty list. Otherwise the function would have given a correct error response, instead of the error you are currently getting.
If you remove these tickers the getQuote function will work.
# remove tickers which have . or ^ in the symbol
tickers <- AMEX$symbol[!grepl("\\.|\\^", AMEX$symbol)]
metrics <- getQuote(paste(tickers, collapse=";"), what=what_metrics)