2015-12-22 38 views
1

我是R,loops和quantmod的新手。我試圖說服quantmod跳過它無法處理的任何股票,並繼續到下一個股票代碼,而不是停止。我想我找到了我的答案在這裏how do I loop through all the stocks with quantmod and ttr?但我不能夠得到霧凇的解決方案的工作:使用quantmod循環

If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following

# Actual loop: 
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51 
for(i in 51:length(symbols)) { 
    symbols[i]-> symbol 
... 

下面是我的原代碼,只返回多個值的8(所以我假設9是麻煩點)。

library(gdata) 
d = read.xls("~/Documents/TEST.xlsx", sheet = 1, stringsAsFactors=F) 

library(quantmod) 
sym <- as.character(d[,1]) 

results <- NULL 

for (ii in sym){ 
    data1 <- getSymbols(Symbols = ii, 
         src = "yahoo", 
         from = Sys.Date() - 100, 
         auto.assign = FALSE) 
    de = head(data1,150) 
    colnames(de) <- c("open","high","low","close","volume","adj.") 
    overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1 

    results <- rbind(results,cbind(
    paste(round(min(overnightRtn,na.rm=T),5),"%",sep=""))) 

} 

colnames(results) <- c("overnightRtn2") 
rownames(results) <- sym 
View(results) 

當我改變for(ii in sym)for(ii in 9:length(sym))我得到一個錯誤:

could not find function "getSymbols.9"

這裏是d[,1]開始:

[1] "ABX" "ACC" "ACCO" "ACE" "ACG" "ACH" "ACI" "ACM" "ACMP" "ACN" 

回答

1

有一些解決方法錯誤R中循環時,一種做法是使用tryCatch函數,juba函數顯示here如何做到這一點。我也確保for循環只會在data1變量被分配一些值時繼續。

更改您的for loop以下代碼,它應該適用於您所要求的內容。

for (ii in sym){ 
    data1 <- NULL        # NULL data1 
    data1 <- tryCatch(getSymbols(Symbols = ii, 
         src = "yahoo", 
         from = Sys.Date() - 100, 
         auto.assign = FALSE), 
        error=function(e){})  # empty function for error handling 
    if(is.null(data1)) next()     # if data1 is still NULL go to next ticker 
    de = head(data1,150) 
    colnames(de) <- c("open","high","low","close","volume","adj.") 
    overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1 

    results <- rbind(results,cbind(
    paste(round(min(overnightRtn,na.rm=T),5),"%",sep=""))) 
} 
+1

使用'try'比'tryCatch'簡單得多。 –

+0

@JoshuaUlrich當我只使用'try'時它停止了,可能我做錯了什麼。 – Mutador

+1

'data1 < - try(getSymbols(...));如果(繼承(data1,「嘗試錯誤」))下一個 –

0

您可以嘗試使用tidyquant包來處理內部錯誤處理。它也不需要for循環,所以它會爲你節省大量的代碼。 tq_get()函數負責獲取股票價格。您可以使用complete_cases參數來調整處理錯誤的方式。

實施例與complete_cases = TRUE:自動刪除 「壞蘋果」

library(tidyquant) 

# get data with complete_cases = TRUE automatically removes bad apples 
c("AAPL", "GOOG", "BAD APPLE", "NFLX") %>% 
    tq_get(get = "stock.prices", complete_cases = TRUE) 

#> Warning in value[[3L]](cond): Error at BAD APPLE during call to get = 
#> 'stock.prices'. Removing BAD APPLE. 
#> # A tibble: 7,680 × 8 
#> symbol  date open high low close volume adjusted 
#>  <chr>  <date> <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl> 
#> 1 AAPL 2007-01-03 86.29 86.58 81.90 83.80 309579900 10.85709 
#> 2 AAPL 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.09807 
#> 3 AAPL 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.01904 
#> 4 AAPL 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.07345 
#> 5 AAPL 2007-01-09 86.45 92.98 85.15 92.57 837324600 11.99333 
#> 6 AAPL 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.56728 
#> 7 AAPL 2007-01-11 95.94 96.78 95.10 95.80 360063200 12.41180 
#> 8 AAPL 2007-01-12 94.59 95.06 93.23 94.62 328172600 12.25892 
#> 9 AAPL 2007-01-16 95.68 97.25 95.45 97.10 311019100 12.58023 
#> 10 AAPL 2007-01-17 97.56 97.60 94.82 94.95 411565000 12.30168 
#> # ... with 7,670 more rows 

實施例與complete_cases = FALSE:返回嵌套數據幀。


library(tidyquant) 

# get data with complete_cases = FALSE returns a nested data frame 
c("AAPL", "GOOG", "BAD APPLE", "NFLX") %>% 
    tq_get(get = "stock.prices", complete_cases = FALSE) 

#> Warning in value[[3L]](cond): Error at BAD APPLE during call to get = 
#> 'stock.prices'. 
#> Warning in value[[3L]](cond): Returning as nested data frame. 
#> # A tibble: 4 × 2 
#>  symbol   stock.prices 
#>  <chr>    <list> 
#> 1  AAPL <tibble [2,560 × 7]> 
#> 2  GOOG <tibble [2,560 × 7]> 
#> 3 BAD APPLE   <lgl [1]> 
#> 4  NFLX <tibble [2,560 × 7]> 

在兩種情況下,用戶得到警告消息。謹慎的用戶會閱讀並嘗試確定問題所在。最重要的是,長時間運行的腳本不會失敗。