我使用了一个代码(附加链接)将新的交易数据行添加到数据集中:
gspc<-getSymbols(("^GSPC") , src = 'yahoo', from = '2008-01-01', auto.assign = T)
gspc<-cbind(GSPC)
q <- getQuote("^GSPC") # adds the current trade row
qCols <- c("Open","High","Low","Last","Volume","Last")
qx <- xts(q[,qCols], as.Date(q[,"Trade Time"]))
gspc <- rbind(gspc, qx)
我得到了很好的输出:
tail(gspc)
GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted GSPCIsuP GSPCIsUp1day GSPCOpAbOp GSPCMA5Op GSPCMA5High GSPCHiLo1d GSPCMAHiLo5d GSPCSTdVHiLo5d GSPCMaOpCl5d GSPCMA10Op
2015-11-03 2102.63 2116.48 2097.51 2109.79 4272060000 2109.79 1 1 21.869873 2085.644 2090.752 25.439941 11.88403 11.720686 -6.943994 2068.559
2015-11-04 2110.60 2114.59 2096.98 2102.31 4078870000 2102.31 0 1 7.970215 2094.468 2099.974 13.850097 14.33003 10.223758 -8.948047 2076.272
2015-11-05 2101.68 2108.78 2090.41 2099.93 4051890000 2099.93 0 0 -8.920166 2097.134 2104.822 3.989990 10.35400 9.420192 -2.516015 2084.252
2015-11-06 2098.60 2101.91 2083.74 2099.20 4369020000 2099.20 1 0 -3.079834 2098.854 2108.074 7.100097 10.94004 9.022028 -1.954053 2088.293
2015-11-09 2096.56 2096.56 2068.24 2078.58 3882350000 2078.58 0 0 -2.040039 2102.014 2109.592 3.309814 10.73799 9.216563 -4.202002 2090.441
2015-11-10 2077.19 2077.98 2073.35 2077.12 34835429 2077.12 NA NA NA NA NA NA NA NA NA NA
但是当我尝试添加两个计算字段时,第一个工作正常但另一个产生错误
gspc$GSPCHiLo1d<-Lag(Hi(GSPC),k=1)-Lag(Op(GSPC),k=1) # that's fine
gspc$GSPCMAHiLo10d<-SMA(gspc$GSPCHiLo1d,n=10)
#I got an error "Error in runSum(x, n) : Series contains non-leading NAs"
我该如何克服这个问题? 链接到我使用的代码: source code link for adding new row to quantmod data set
答案 0 :(得分:1)
您可以将最后一行替换为:
gspc$GSPCMAHiLo10d<-SMA(gspc$GSPCHiLo1d[-nrow(gspc)]) # the n=10 is optional as it is the default option
tail(gspc)
GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted GSPCHiLo1d GSPCMAHiLo10d
2015-11-03 2102.63 2116.48 2097.51 2109.79 4272060000 2109.79 25.439941 12.483997
2015-11-04 2110.60 2114.59 2096.98 2102.31 4078870000 2102.31 13.850097 13.270007
2015-11-05 2101.68 2108.78 2090.41 2099.93 4051890000 2099.93 3.989990 13.219006
2015-11-06 2098.60 2101.91 2083.74 2099.20 4369020000 2099.20 7.100097 10.597021
2015-11-09 2096.56 2096.56 2068.24 2078.58 3882350000 2078.58 3.309814 8.772998
2015-11-09 2096.56 2096.56 2068.24 2078.58 664847995 2078.58 NA NA
正如您所看到的那样,它会在您右下方的最后一个单元格中放置一个NA
。问题是,您的两个Lag
的公式比gspc
中的一个元素短,因此最后一个元素是NA
,这会导致计算出现问题。运行命令SMA
后,您可以(如果需要)替换NA
值。
您还可以将gspc$GSPCHiLo1d
的最后一个值替换为您想要的值,然后毫无问题地运行SMA
命令。
好的我觉得我发现了一个问题:使用
gspc$GSPCHiLo1d<-Lag(Hi(gspc),k=1)-Lag(Op(gspc),k=1)
而不是
gspc$GSPCHiLo1d<-Lag(Hi(GSPC),k=1)-Lag(Op(GSPC),k=1)
然后
gspc$GSPCMAHiLo10d<-SMA(gspc$GSPCHiLo1d,n=10)
tail(gspc)
GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted GSPCHiLo1d
2015-11-03 2102.63 2116.48 2097.51 2109.79 4272060000 2109.79 25.439941
2015-11-04 2110.60 2114.59 2096.98 2102.31 4078870000 2102.31 13.850097
2015-11-05 2101.68 2108.78 2090.41 2099.93 4051890000 2099.93 3.989990
2015-11-06 2098.60 2101.91 2083.74 2099.20 4369020000 2099.20 7.100097
2015-11-09 2096.56 2096.56 2068.24 2078.58 3882350000 2078.58 3.309814
2015-11-10 2077.19 2079.70 2069.91 2071.69 109144559 2071.69 0.000000
GSPCMAHiLo10d
2015-11-03 12.483997
2015-11-04 13.270007
2015-11-05 13.219006
2015-11-06 10.597021
2015-11-09 8.772998
2015-11-10 8.767016
您想要的评论High
- Low
,但在您的代码中,High
- Open
。将Op
替换为Lo
,您将拥有
tail(gspc)
GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted GSPCHiLo1d
2015-11-03 2102.63 2116.48 2097.51 2109.79 4272060000 2109.79 25.43994
2015-11-04 2110.60 2114.59 2096.98 2102.31 4078870000 2102.31 18.96997
2015-11-05 2101.68 2108.78 2090.41 2099.93 4051890000 2099.93 17.61011
2015-11-06 2098.60 2101.91 2083.74 2099.20 4369020000 2099.20 18.37012
2015-11-09 2096.56 2096.56 2068.24 2078.58 3882350000 2078.58 18.16992
2015-11-10 2077.19 2079.70 2069.91 2071.01 110450050 2071.01 28.32007
GSPCMAHiLo10d
2015-11-03 18.58199
2015-11-04 19.22799
2015-11-05 18.91400
2015-11-06 17.41902
2015-11-09 17.08101
2015-11-10 19.05203
具有所需的28.32
如果那不是您想要的,请告诉我