我是R的完整初学者。我想使用getSymbols在一段时间内下载有关S& P500当前公司的历史数据。显然,有些公司在特定时期内并不存在,R停止为下一个代码下载数据。有没有办法让getSymbols在他们的数据不存在的情况下简单地省略代码?在那个时期获得标准普尔500指数清单要容易得多,但遗憾的是它不是免费的。
答案 0 :(得分:1)
您可以在try
中使用sapply
,如下所示:
library(quantmod)
WoW <- new.env()
##
sapply(SiP, function(x){
try(
getSymbols(
x,
from=as.Date("2001-01-01"),
to=as.Date("2007-01-01"),
env=WoW),
silent=TRUE)
})
错误将打印到控制台(如果需要,您可以缓解此错误),但不产生错误的代码仍会产生数据:
R> ls(WoW)
[1] "AA" "AEE" "AEP" "AES" "AP" "ARG" "ATI" "AVY" "BLL" "CF" "CMS" "CNP" "CTL" "D" "DOW" "DTE" "DUK" "ECL" "ED" "EIX"
[21] "EMN" "ETR" "EXC" "FCX" "FE" "FMC" "FTR" "GAS" "IFF" "IP" "LVLT" "MON" "MOS" "MWV" "NEE" "NEM" "NI" "NRG" "NU" "NUE"
[41] "OI" "PCG" "PEG" "PNW" "POM" "PPG" "PPL" "SCG" "SO" "SRE" "T" "TE" "TEG" "VZ" "WEC" "WIN" "XEL"
##
R> length(ls(WoW))
[1] 57
R> length(SiP)
[1] 59
所以看起来有2个股票存在问题,因为sapply(...)
成功返回了其他股票的数据。
从这里开始,可以通过您的首选方法在WoW
内访问对象,例如
R> with(WoW, chartSeries(ARG))
数据:
SiP=c('AES','GAS','AEE','AEP','CNP', 'CMS','ED','D',
'DTE','DUK','EIX', 'ETR','EXC','FE','TEG',
'NEE','NI', 'NU','NRG','PCG','POM','PNW','PPL',
'PEG','SCG','SRE','SO','TE','WEC', 'XEL','T',
'CTL','FTR','LVLT','VZ', 'WIN','AP','ARG',
'AA','ATI','AVY', 'BLL','CF','DOW','D',
'EMN','ECL', 'FMC','FCX','IP','IFF','LYB',
'MWV', 'MON','MOS','NEM','NUE','OI','PPG')
答案 1 :(得分:0)
问题是代码清单stockSymbols()
生成的标点符号,尽管来自雅虎,但使用getSymbols()
产生404,因为雅虎不会在网址getSymbols()
中使用这些标点符号试图刮擦。
实施例:
stockSymbols()
检索符号&#34; AA-P&#34;,您尝试将其传递到getSymbols()
,然后您获得404&#39; d,因为Yahoo!使用&#34; AA&#34;在URL中,而不是&#34; AA-P&#34;,对于该股票,尽管已经给出了#34; AA-P&#34;来自任何资源stockSymbols()
从中检索它。
我已经制作了一些代码来清理stockSymbols()
生成的代码列表,以便getSymbols()
不会生成错误。这将删除包含标点符号的首选项和符号,因此结果来自普通股发行。
library(quantmod)
symbols = stockSymbols()
symbols = symbols[,1]
for (i in seq_along(symbols)) {
hyph = gregexpr(pattern = "-", symbols[i])
per = gregexpr(pattern = "[.]", symbols[i])
if (hyph[[1]][1] > 0 ) {
symbols[i] = substr(symbols[i], 1, hyph[[1]][1] - 1)
} else if (per[[1]][1] > 0 ) {
symbols[i] = substr(symbols[i], 1, per[[1]][1] - 1)
}
}
symbols = unique(symbols)
for (i in seq_along(symbols)){
tryit <- try(getSymbols(symbols[i],from="2016-01-01", src='yahoo'))
if(inherits(tryit, "try-error")){
i <- i+1
}
else {
stock = getSymbols(symbols[i], from="2016-01-01", src = "yahoo", auto.assign = FALSE)
stocks[[i]] = as.data.frame(stock)
}
}
答案 2 :(得分:0)
您可以尝试在内部处理错误处理的tidyquant
包。它也不需要for循环或tryCatch
语句,因此它可以为您节省大量代码。 tq_get()
函数负责获取股票价格。您可以使用complete_cases
参数来调整错误的处理方式。
complete_cases = TRUE
示例:自动删除&#34;坏苹果&#34;
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]>
在这两种情况下,用户都会收到警告消息。谨慎的用户将阅读它们并尝试确定问题所在。最重要的是,长时间运行的脚本不会失败。