Sky
Sky

Reputation:

Error "Index vectors are of different classes: numeric date"

I am trying to implement the following code from the following site:

library(quantmod)
library(tseries)
library(timeDate)

symbols <- read.csv("sp500.csv", header = F, stringsAsFactors = F)
nrStocks <- length(symbols[,1])

z <- zoo()
for (i in 1:nrStocks) {
  cat("Downloading ", i, " out of ", nrStocks , "\n")
  x <- get.hist.quote(instrument = symbols[i,], start = dateStart,
    quote = "AdjClose", retclass = "zoo", quiet = T)
  z <- merge(z, x)
}

but I get the error message:

Error in download.file(url, destfile, method = method, quiet = quiet) : 
  cannot open URL 'http://chart.yahoo.com/table.csv?s=MFE&a=0&b=01&c=2008&d=8&e=12&f=2011&g=d&q=q&y=0&z=MFE&x=.csv'
In addition: There were 50 or more warnings (use warnings() to see the first 50)

In merge.zoo(z, x) : Index vectors are of different classes: numeric Date

Any ideas?

Upvotes: 4

Views: 2183

Answers (3)

ronnydw
ronnydw

Reputation: 953

Code to make an accurate list of S&P500 symbols based on the Wikipedia information:

library(XML)
library(RCurl)

url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
sp500html <- getURL(url)
sp500 <- readHTMLTable(sp500html, stringsAsFactors = F)[[1]][,1]

Upvotes: 0

jfmgH
jfmgH

Reputation: 21

  1. Wikipedia allows you to make a more accurate list for sp500.csv

  2. There is an issue with yahoo dates

a. This will work when the markets in US are open.

b. This will probably not work when the markets in US are closed

The complete reference for get.hist.quote can be fount here http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=tseries:get.hist.quote, it did not bring me any help either, After that I looked at the data outside market opening hours: The first downloaded instrument is MMM, the first couple of lines are here (when the market is closed):

:Date,Open,High,Low,Close,Volume,Adj Close
2012-07-16,88.10,88.55,87.78,88.10,2120700,88.10
2012-07-16,88.12,88.65,87.77,88.10,2848100,88.10
2012-07-13,86.32,87.83,86.32,87.59,2599300,87.59

the first two lines appear to be identical. For another instrument, while the markets are open (European instrument), this was not an issue.

x <- get.hist.quote(instrument = "^GDAXI", start = dateStart,
    quote = "AdjClose", retclass = "zoo", quiet = T)

For the DAX the data is below (now is 11H45 CET):

Date,Open,High,Low,Close,Volume,Adj Close
2012-07-17,6583.77,6610.26,6583.57,6593.82,000,6565.72
2012-07-16,6550.95,6577.06,6511.18,6565.72,20388100,6565.72

Looked through the zoo.pdf manual (dated: February 14, 2012):

They suggest to resolve this by adding a line:

x <- aggregate(x, identity, mean) # for an average

or

x <- aggregate(x, identity, tail) # for the last observation

Have not completed my testing but this appears to do the trick without warnings and errors so far

Upvotes: 2

Henry
Henry

Reputation: 6784

Here are a list of possible issues:

  • "sp500.csv": You need to get this file from somewhere. If you have taken this code from here then one possibility is this but that list is out of date

  • z <- zoo(): This will not have the index in Date format and so causes the merge warnings, so might sensibly be followed by a line like index(z) <- as.Date(format(time(z)),tz="")

  • start = dateStart,: You have not specified dateStart. If you want the last month or so then something like start = Sys.Date() - 30, might work

  • Wrong ticker symbols: The error message shows your list contained MFE as one of the symbols. MFE was McAfee Inc, which has been taken over by Intel Corp and so no longer has a quoted share price.

Upvotes: 4

Related Questions