Error while retrieving Volume data from Yahoo

111 views Asked by At

I am trying to retrieve the volume from yahoo by creating a function (that I can later apply to a list of stocks). RetrieveVolume <- function(x){ volume_Ind2 <- getQuote(x, src = "yahoo", "v") volume_Ind2 <- as.data.frame(volume_Ind2) volume_Ind2$Qposix <- NULL volume_Ind2 <- volume$sq...3.NCOL.sq.. }

When I try using the formula for 0001.HK for example,

RetrieveVolume("0001.HK")

I get the following:

Error in RetrieveVolume("0001.HK") : object 'volume' not found

Do you have any idea what I am doing wrong please?

1

There are 1 answers

3
Adrian Prayoga On

There are 2 problems here:

  • you're calling a variable volume which is not defined yet.
  • you do not return any value through your function

a quick solution to the problem will be

RetrieveVolume <- function(x){
   volume_Ind2 <- getQuote(x, src = "yahoo", "v")
   return(volume_Ind2[,2])
}

or a simpler solution would be

RetrieveVolume <- function(x){
   return(getQuote(x, what=yahooQF("Volume"))[,"Volume"])
}