我正在尝试下载纳斯达克100指数组成部分的时间序列。我想使用同一作者的getSymbols
和getYahooData
来进行此操作,以便进一步调查最终的差异。事实证明,只有后一种功能无法导入“ADP”和“NVDA”的数据:
> getYahooData("ADP")
Error in NextMethod(.Generic) :
number of items to replace is not a multiple of replacement length
此外,即使调整后的价格对应所有代号,调整后的音量也不对应,我需要知道原因。由于出现错误的代码可能并不总是运行相同的代码,因此问题更加严重!平均而言,我得到大约6个代码的音量误差。我快速调查显示,即使价格调整落后,数量的差异也只会在最近一段时间内出现。
require(quantmod) # for getSymbols
require(TTR) # for getYahooData
constitutents = c("AAL","AAPL","ADBE","ADI","ADP","ADSK","AKAM","ALXN","AMAT",
"AMGN","AMZN","ATVI","BBBY","BIDU","BIIB","BMRN","CA","CELG",
"CERN","CHKP","CHTR","CMCSA","COST","CSCO","CSX","CTRP",
"CTSH","CTXS","DISCA","DISCK","DISH","DLTR","EA","EBAY",
"ENDP","ESRX","EXPE","FAST","FB","FISV","FOX","FOXA","GILD",
"GOOG","GOOGL","HSIC","INCY","INTC","INTU","ILMN","ISRG","JD",
"KHC","LBTYA","LBTYK","LLTC","LMCA","LRCX","LVNTA","MAR",
"MAT","MDLZ","MNST","MSFT","MU","MXIM","MYL","NCLH","NFLX",
"NTAP","NVDA","NXPI","ORLY","PAYX","PCAR","PCLN","PYPL",
"QCOM","QVCA","REGN","ROST","SBAC","SBUX","SIRI","SNDK",
"SRCL","STX","SWKS","SYMC","TMUS","TSCO","TSLA","TRIP","TXN",
"ULTA","VIAB","VOD","VRSK","VRTX","WBA","WDC","WFM","XLNX",
"YHOO")
errorsList = NULL
for(symbol in constitutents) {
# Download the time series of the ticker indicated by
# the symbol
getSymbols(symbol, from="1900-01-01")
# Adjust the time series for splits and dividends
assign(symbol, adjustOHLC(get(symbol, pos=.GlobalEnv), symbol.name=symbol,
adjust=c("split","dividend")))
# The function getYahooData fails to import the time series for
# the following two tickers. An error message is retrieved.
if (symbol=="ADP")next
if (symbol=="NVDA")next
# Import the same data again using a different function from the same
# authors.
data = getYahooData(symbol)
# Check if the traded volume for the data obtained using two different
# functions correspond each other.
volumeMismatch = min(data[ ,5] - get(symbol, pos=.GlobalEnv)[ ,5])
if (abs(volumeMismatch)>1e-6) {
errorsList = c(errorsList, symbol)
print(paste(symbol, "presents an mismatch for the volume"))
} else {
print(paste("getYahooData and getSymbols corresponds for", symbol))
}
}
> errorsList
[1] "GOOGL" "HSIC" "INCY" "LVNTA" "VRTX" "YHOO"
问题1:为什么getYahooData
无法导入“ADP”和“NVDA”的数据。
问题2:变量代码的音量差异是什么原因?
答案 0 :(得分:0)
使用:
getYahooData("ADP", 19000101, freq = "daily", type = "split",
adjust = TRUE, quiet = T)
或
getYahooData("ADP", d1, d2, freq = "daily", type = "split",
adjust = TRUE, quiet = T)
其中d1
和d2
是您想知道股票价格的指定日期;日期格式应为yyyymmdd
。