Path: blob/main/finance/R-cran-RFinanceYJ/files/patch-R-quoteStockTsData.R
16461 views
--- R/quoteStockTsData.R.orig 2013-08-12 15:05:58 UTC1+++ R/quoteStockTsData.R2@@ -46,42 +46,34 @@ quoteFXTsData <- function(x, since=NULL,3#get time series data from Yahoo! Finance.4quoteTsData <- function(x,function.financialproduct,since,start.num,date.end,time.interval,type="stock"){5r <- NULL6- result.num <- 517financial.data <- data.frame(NULL)8- #start <- (gsub("([0-9]{4,4})-([0-9]{2,2})-([0-9]{2,2})","&c=\\1&a=\\2&b=\\3",since))9- #end <- (gsub("([0-9]{4,4})-([0-9]{2,2})-([0-9]{2,2})","&f=\\1&d=\\2&e=\\3",date.end))10start <- (gsub("([0-9]{4,4})-([0-9]{2,2})-([0-9]{2,2})","&sy=\\1&sm=\\2&sd=\\3",since))11end <- (gsub("([0-9]{4,4})-([0-9]{2,2})-([0-9]{2,2})","&ey=\\1&em=\\2&ed=\\3",date.end))1213if(!any(time.interval==c('d','w','m'))) stop("Invalid time.interval value")14-15- extractQuoteTable <- function(r,type){16- if(type %in% c("fund","fx")){17- tbl <- r[[2]][[2]][[7]][[3]][[3]][[9]][[2]]18- }19- else{20- tbl <- r[[2]][[2]][[7]][[3]][[3]][[10]][[2]]21- }22- return(tbl)23- }24-25- while( result.num >= 51 ){26+ while( 1 ){27start.num <- start.num + 128quote.table <- NULL29- quote.url <- paste('http://info.finance.yahoo.co.jp/history/?code=',x,start,end,'&p=',start.num,'&tm=',substr(time.interval,1,1),sep="")30+ quote.url <- paste('https://info.finance.yahoo.co.jp/history/?code=',x,start,end,'&p=',start.num,'&tm=',substr(time.interval,1,1),sep="")31+ quote.html <- getURL(quote.url)3233- try( r <- xmlRoot(htmlTreeParse(quote.url,error=xmlErrorCumulator(immediate=F))), TRUE)34+ try( r <- htmlParse(quote.html) )35if( is.null(r) ) stop(paste("Can not access :", quote.url))3637- #try( quote.table <- r[[2]][[1]][[1]][[16]][[1]][[1]][[1]][[4]][[1]][[1]][[1]], TRUE )38- try( quote.table <- extractQuoteTable(r,type), TRUE )39+ try( quote.table <- xpathApply(r,"//table")[[2]], TRUE )40+41+ quote.size <- xmlSize(quote.table)42+43+ if( xmlSize(quote.table) <= 1 ){44+ return(financial.data)45+ }4647if( is.null(quote.table) ){48if( is.null(financial.data) ){49stop(paste("Can not quote :", x))50}else{51- financial.data <- financial.data[order(financial.data$date),]52- return(financial.data)53+ financial.data <- financial.data[order(financial.data$date),]54+ return(financial.data)55}56}5758@@ -90,7 +82,6 @@ quoteTsData <- function(x,function.finan59financial.data <- rbind(financial.data,function.financialproduct(quote.table[[i]]))60}6162- result.num <- xmlSize(quote.table)63Sys.sleep(1)64}65financial.data <- financial.data[order(financial.data$date),]66@@ -120,5 +111,3 @@ endOfMonth <- function(date.obj)67return(startOfNextMonth-1)68}6970-71-727374