[R] get.hist.quote problem yahoo
Rene Braeckman
RMan54 at cox.net
Fri Feb 9 19:31:23 CET 2007
I had the same problem some time ago. Below is a function that I picked up
on the web somewhere (can't remember where; may have been a newsletter).
It's based on the tseries function but the difference is that this function
produces a data frame with a column containing the dates of the quotes,
instead of a time series object. I had to replace "%d-%b-%y" by "%Y-%m-%d"
to make it work, probably as you stated because the format was changed by
Yahoo.
Hope this helps.
Rene
# ----------------------------------------------------------------------
# "df.get.hist.quote()" function
#
# Based on code by A. Trapletti (package tseries)
#
# The main difference is that this function produces a data frame with
# a column containing the dates of the quotes, instead of a time series
# object.
df.get.hist.quote <- function (instrument = "ibm",
start, end,
quote = c("Open","High", "Low",
"Close","Volume"),
provider = "yahoo", method = "auto")
{
if (missing(start))
start <- "1970-01-02"
if (missing(end))
end <- format(Sys.time() - 86400, "%Y-%m-%d")
provider <- match.arg(provider)
start <- as.POSIXct(start, tz = "GMT")
end <- as.POSIXct(end, tz = "GMT")
if (provider == "yahoo") {
url <- paste("http://chart.yahoo.com/table.csv?s=", instrument,
format(start, "&a=%m&b=%d&c=%Y"), format(end,
"&d=%m&e=%d&f=%Y"),
"&g=d&q=q&y=0&z=", instrument, "&x=.csv", sep = "")
destfile <- tempfile()
status <- download.file(url, destfile, method = method)
if (status != 0) {
unlink(destfile)
stop(paste("download error, status", status))
}
status <- scan(destfile, "", n = 1, sep = "\n", quiet = TRUE)
if (substring(status, 1, 2) == "No") {
unlink(destfile)
stop(paste("No data available for", instrument))
}
x <- read.table(destfile, header = TRUE, sep = ",")
unlink(destfile)
nser <- pmatch(quote, names(x))
if (any(is.na(nser)))
stop("This quote is not available")
n <- nrow(x)
lct <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
on.exit(Sys.setlocale("LC_TIME", lct))
dat <- gsub(" ", "0", as.character(x[, 1]))
dat <- as.POSIXct(strptime(dat, "%Y-%m-%d"), tz = "GMT")
if (dat[n] != start)
cat(format(dat[n], "time series starts %Y-%m-%d\n"))
if (dat[1] != end)
cat(format(dat[1], "time series ends %Y-%m-%d\n"))
return(data.frame(cbind(Date=I(format(dat[n:1],"%Y-%m-%d")),x[n:1,nser]),row
.names=1:n))
}
else stop("Provider not implemented")
}
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Daniele Amberti
Sent: Friday, February 09, 2007 5:22 AM
To: r-help
Subject: [R] get.hist.quote problem yahoo
I have functions using get.hist.quote() from library tseries.
It seems that something changed (yahoo) and function get broken.
try with a simple
get.hist.quote('IBM')
and let me kow if for someone it is still working.
I get this error:
Error in if (!quiet && dat[n] != start) cat(format(dat[n], "time series
starts %Y-%m-%d\n")) :
missing value where TRUE/FALSE needed
Looking at the code it seems that before the format of dates in yahoo's cv
file was not iso.
Now it is iso standard year-month-day
Anyone get the same problem?
------------------------------------------------------
Passa a Infostrada. ADSL e Telefono senza limiti e senza canone Telecom
http://click.libero.it/infostrada9feb07
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list