【发布时间】:2015-05-01 14:10:09
【问题描述】:
我有一个 R 投资组合构建代码,它使用来自雅虎的每日调整收盘价数据。我在 NA 值方面遇到了一些问题,但代码已经运行了一段时间。直到本周末(例如,2015 年 2 月 28 日)。
现在,当我使用 tseries 函数 get.hist.quote() 时,雅虎数据源似乎完全被破坏了。坏我的意思是它不会正确返回 VTV 和许多其他 ETF 的数据。不知道是雅虎时间序列源宕机还是什么。
有一篇帖子 (https://stackoverflow.com/a/3507948/2341077) 建议将 get.hist.quote() 中的 URL 从 chart.yahoo.com 更改为 ichart.yahoo.com 可以解决问题。但这对我没有任何改变。我还确保我安装了最新版本的 tseries。
还有其他人对雅虎的收盘价时间序列有疑问吗?我一直在想是否应该更改我的代码以使用 quantmod 函数 getSymbols,显然,它可以使用 Google Finance 作为数据源。
以下代码用于读取数百个 ETF 品种并返回一个包含 ETF 时间序列数据的矩阵。尝试按日期对齐数据。
即使 Yahoo 似乎在提供数据,仍然存在缺失值,这正是 fillHoles() 函数要解决的问题。
<pre>
#
# Fill "NA" holes in the time series.
#
fillHoles = function(ts.zoo) {
v_approx = na.approx(ts.zoo, maxgap=4, na.rm=FALSE)
v_fill = na.fill(v_approx, fill="extend")
return( v_fill)
}
<i>
#
# The yahoo market data has problems (at least when it's fetched with get.hist.quote()) when the compression
# argument is used to fetch weekly adjusted close data.
#
# Two time series are shown below, for VXF and MINT. The weekly boundaries appear on different dates.
#
# VXF
# 2007-04-04 48.55
# 2007-04-09 48.98
# 2007-04-16 49.52 <==
# 2007-04-23 49.70
# 2007-04-30 50.03
# 2007-05-07 50.04 <==
#
# MINT
# 2007-04-04 8.03
# 2007-04-09 8.03
# 2007-04-17 7.88 <==
# 2007-04-23 8.11
# 2007-04-30 8.92
# 2007-05-08 9.14 <==
#
# If the two time series are merged via a cbind NA values
# end up being inserted where the time series don't line up:'
#
# VXF MINT
# 2007-04-04 48.55 8.03
# 2007-04-09 48.98 8.03
# 2007-04-16 49.52 NA
# 2007-04-23 49.70 8.11
# 2007-04-30 50.03 8.92
# 2007-05-07 50.04 NA
#
# To avoid this problem of data alignment, the function fetches daily adjusted close that can then be converted
# into weekly adjusted close.
#
# Given a vector of symbols, this function will fetch the daily adjusted close price data from
# Yahoo. The data is aligned since not all time series will have exactly the same start and end
# dates (although with daily data, as noted above, this should be less of an issue)
#
</i>
getDailyCloseData = function(symbols, startDate, endDate )
{
closeData.z = c()
firstTime = TRUE
minDate = c()
maxDate = c()
fetchedSyms = c()
startDate.ch = as.character( findMarketDate(as.Date(startDate)))
endDate.ch = as.character( findMarketDate(as.Date(endDate)))
for (i in 1:length(symbols)) {
sym = symbols[i]
print(sym)
symClose.z = NULL
timeOut = 1
tsEndDate.ch = endDate.ch
while ((timeOut < 7) && is.null(symClose.z)) {
try(
(symClose.z = get.hist.quote(instrument=sym, start=startDate.ch, end=tsEndDate.ch, quote="AdjClose",
provider="yahoo", compression="d", retclass="zoo", quiet=T)),
silent = TRUE)
tsEndDate.ch = as.character( findMarketDate( (as.Date(tsEndDate.ch) - 1)))
timeOut = timeOut + 1
}
if (! is.null(symClose.z)) {
fetchedSyms = c(fetchedSyms, sym)
dateIx = index(symClose.z)
if (firstTime) {
closeData.z = symClose.z
firstTime = FALSE
minDate = min(dateIx)
maxDate = max(dateIx)
} else {
minDate = max(minDate, min(dateIx))
maxDate = min(maxDate, max(dateIx))
matIx = index(closeData.z)
repeat {
startIx = which(matIx == minDate)
if (length(startIx) > 0 && startIx > 0) {
break()
} else {
minDate = minDate + 1
}
} # repeat
repeat {
endIx = which(matIx == maxDate)
if (length(endIx) > 0 && endIx > 0) {
break()
} else {
maxDate = maxDate - 1
}
}
matIxAdj = matIx[startIx:endIx]
closeData.z = cbind(closeData.z[matIxAdj,], symClose.z[matIxAdj])
}
} # if (! is.null(symClose.z))
} # for
if (length(closeData.z) > 0) {
dateIx = index(closeData.z)
# fill any NA "holes" created by daily date alignment
closeData.mat = apply(closeData.z, 2, FUN=fillHoles)
rownames(closeData.mat) = as.character(dateIx)
colnames(closeData.mat) = fetchedSyms
}
return( closeData.mat )
} # getDailyCloseData
</pre>
【问题讨论】:
-
任何可重现的例子?
-
一个问题是雅虎数据结果似乎是零星的。
-
我为上下文添加了一个代码示例。然而,雅虎似乎并不一致。我要试试谷歌,使用 quantmod 的 getSymbols()。
标签: r yahoo-finance