2014-12-01 81 views
1

我使用本網站上某個線程的示例代碼創建了一個小程序,該程序使用quantmod包中的getSymbols從yahoo下載股票數據。但是,當我修改代碼以添加tryCatch(我以兩種不同方式執行此操作)時,它不會返回任何數據。我希望我沒有提供太多細節,但希望確保我提供所有信息。如何在tryCatch中包裝quantmod的getSymbols()

這裏是工作的代碼(隨後非工作的代碼不返回任何數據):

###################### Working code################ 
library(quantmod) 
library(plyr) 
library(PerformanceAnalytics) 

#Data structure that contains stock quote objects 
stockData <- new.env() 
#Assign dates to set range for stock quotes 
sDate <- as.Date("2012-09-04") 
eDate <- as.Date("2014-09-02") 

#Assign a vector of ticker symbols 
symbols <- c("BIL","VGSH","VGIT","VGLT","BND","VOO","VTI","VT") 

# download data 
l_ply(symbols, function(sym) try(getSymbols(sym,env = stockData,src = "yahoo", 
              from = sDate, to = eDate))) 
# Now extract the data from the environment and store in a list. 
symbols <- symbols[symbols %in% ls(envir = stockData)] 

# sym.list 
sym.list = llply(symbols, ge 

t,stockData) 
names(sym.list) <- symbols 

以下兩種情況下不返回任何數據:

  1. 創建函數調用getYahooTickerData它調用內tryCatch getSymbols。這不會返回任何數據。

  2. 假設有一個與傳遞函數之間的環境問題,我在上面工作的代碼替換trytryCatch代替try,但它仍然無法正常工作。

首先,殼體1:

###################################Case 1################################ 
library(quantmod) 
library(plyr) 
library(PerformanceAnalytics) 

#Data structure that contains stock quote objects 
stockData <- new.env() 
#Assign dates to set range for stock quotes 
sDate <- as.Date("2012-09-04") 
eDate <- as.Date("2014-09-02") 

#Assign a vector of ticker symbols 
symbols <- c("BIL","VGSH","VGIT","VGLT","BND","VOO","VTI","VT") 

getYahooTickerData <- function(ticker,startDate,endDate,envmt) { 
    tryCatch(
     getSymbols(
       Symbols=ticker, 
       env = envmt, 
       from = startDate, 
       to = endDate, 
       src = "yahoo", 
     ), 
     error=function(error_message) { 
       message("error getting data") 
       message(ticker) 
     }, 
     warning=function(warning_message) { 
       message("there was a warning for ticker ") 
       message(ticker) 
     } 
    ) 
}# end of function getYahooTickerData 

l_ply(symbols, function(sym) getYahooTickerData(sym,sDate,eDate,envmt=stockData)) 

# Now extract the data from the environment and store in a list. 
symbols <- symbols[symbols %in% ls(envir = stockData)] 

# sym.list 
sym.list = llply(symbols, get,stockData) 
names(sym.list) <- symbols 

在這種情況下,sym.list是空的。現在案例2:

################Case 2################################### 
library(quantmod) 
library(plyr) 
library(PerformanceAnalytics) 

#Data structure that contains stock quote objects 
stockData <- new.env() 
#Assign dates to set range for stock quotes 
sDate <- as.Date("2012-09-04") 
eDate <- as.Date("2014-09-02") 

#Assign a vector of ticker symbols 
symbols <- c("BIL","VGSH","VGIT","VGLT","BND","VOO","VTI","VT") 

# download data 
l_ply(symbols, function(sym) { 
    tryCatch(
     getSymbols(sym,env = stockData, 
        src = "yahoo", 
        from = sDate, 
        to = eDate), 
     error=function(error_message) { 
      message("error getting data for symbol") 
      message(sym) 
     }, 
     warning=function(warning_message) { 
      message("warning for symbol") 
      message(sym) 
     } 
    ) 
}) 
# Now extract the data from the environment and store in a list. 
symbols <- symbols[symbols %in% ls(envir = stockData)] 

# sym.list 
sym.list = llply(symbols, get,stockData) 
names(sym.list) <- symbols 

在這種情況下,sym.list是空的。

回答

0

對於錯誤,您將希望在發生錯誤時包含所需的返回值。對於警告,你實際上沒有捕獲錯誤,但使用條件處理程序。因此,一個相對完整的例子是

withCallingHandlers({ 
    tryCatch({ 
     warning("a warning") 
     stop("an error") 
    }, error=function(e) { 
     ## catch the error 
     message(conditionMessage(e)) 
     ## signal with appropriate return value 
     NA 
    }) 
}, warning=function(w) { 
    ## handle the warning... 
    message(conditionMessage(w)) 
    ## then continue from where the warning occurred 
    invokeRestart("muffleWarning") 
}) 

輸出

a warning 
an error 
[1] NA 
+0

Martin,我無法弄清楚如何從您的回覆中修改我的代碼。是否有可能複製我的非工作代碼示例,進行修改並將其發佈到此處。謝謝。 – 2014-12-01 21:50:40

+0

@RajiveJain我很樂意,但可以請你先清理你的代碼包含重現你的'getSymbol()'函數調用所需的最少代碼(無需plyr或quantmod,比如?)。 – 2014-12-01 22:01:02

0

得到的,我想我已經想通了,爲什麼tryCatch沒有與getSymbols工作()。 getSymbols()總是發出警告(即使當選擇warnings = FALSE選項時),這就是始終調用警告函數的原因。我從tryCatch中刪除了警告塊,代碼工作。 由於代碼在我從tryCatch中刪除警告塊時起作用,因此我認爲這是正確的原因。