使用 quantmod 循环
Looping with quantmod
我是 R、循环和 quantmod 的新手。我试图说服 quantmod 跳过它无法处理的任何代码并继续处理下一个代码,而不是停止。我以为我在这里找到了答案 how do I loop through all the stocks with quantmod and ttr? 但我无法让 Rime 的解决方案起作用:
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
...
下面是我的原始代码,其中只有 returns 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"
在 R 中循环时有一些解决错误的方法,一种方法是使用 tryCatch
函数,juba showed 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="")))
}
您可以尝试 tidyquant
包,它负责内部错误处理。它还不需要 for 循环,因此可以为您节省大量代码。 tq_get()
函数负责获取股票价格。您可以使用 complete_cases
参数来调整错误的处理方式。
complete_cases = TRUE
示例:自动删除 "bad apples"
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
的示例:Returns 嵌套数据框。
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]>
在这两种情况下,用户都会收到警告消息。谨慎的用户会阅读它们并尝试确定问题所在。最重要的是,长 运行 脚本不会失败。
我是 R、循环和 quantmod 的新手。我试图说服 quantmod 跳过它无法处理的任何代码并继续处理下一个代码,而不是停止。我以为我在这里找到了答案 how do I loop through all the stocks with quantmod and ttr? 但我无法让 Rime 的解决方案起作用:
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
...
下面是我的原始代码,其中只有 returns 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"
在 R 中循环时有一些解决错误的方法,一种方法是使用 tryCatch
函数,juba showed 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="")))
}
您可以尝试 tidyquant
包,它负责内部错误处理。它还不需要 for 循环,因此可以为您节省大量代码。 tq_get()
函数负责获取股票价格。您可以使用 complete_cases
参数来调整错误的处理方式。
complete_cases = TRUE
示例:自动删除 "bad apples"
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
的示例:Returns 嵌套数据框。
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]>
在这两种情况下,用户都会收到警告消息。谨慎的用户会阅读它们并尝试确定问题所在。最重要的是,长 运行 脚本不会失败。