quantmod chart_Series绘制具有NA值的系列

时间:2015-09-15 04:08:24

标签: r plot xts quantmod

我有一个xts系列“a”,其中包含15分钟的OHLC和2015年9月10日上午9:30到下午13:00的SPY量,值在下午13:00到下午4点之间为NA。

我想用烛台下方的音量条绘制整个系列。

require(quantmod)
a<-structure(c(194.48, 195.14, 194.84, 194.56, 194.57, 195.82, 195.89, 
195.56, 195.06, 195.8, 195.79, 196.02, 195.58, 195.71, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 195.37, 195.42, 194.87, 
194.95, 195.89, 196.32, 195.96, 195.57, 195.82, 195.95, 196.24, 
196.16, 195.95, 196.1499, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 194.42, 194.64, 194.27, 194.25, 194.5595, 195.73, 
195.44, 194.86, 195.05, 195.405, 195.6, 195.465, 195.48, 195.699, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 195.141, 
194.84, 194.56, 194.56, 195.81, 195.9, 195.57, 195.05, 195.8, 
195.79, 196.01, 195.58, 195.72, 195.785, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 8328828, 5188794, 5859222, 4854178, 
6374039, 5039393, 4419063, 5703169, 3504604, 3207277, 2999488, 
3774300, 2385201, 2212837, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA), .Dim = c(27L, 5L), .Dimnames = list(NULL, c("Open", 
"High", "Low", "Close", "Volume")), index = structure(c(1441892700, 
1441893600, 1441894500, 1441895400, 1441896300, 1441897200, 1441898100, 
1441899000, 1441899900, 1441900800, 1441901700, 1441902600, 1441903500, 
1441904400, 1441905300, 1441906200, 1441907100, 1441908000, 1441908900, 
1441909800, 1441910700, 1441911600, 1441912500, 1441913400, 1441914300, 
1441915200, 1441915500), tzone = "", tclass = c("POSIXct", "POSIXt"
)), tclass = c("POSIXct", "POSIXt"), tzone = "", .indexCLASS = c("POSIXct", 
"POSIXt"), .indexTZ = "", class = c("xts", "zoo"))

chart_Series(a,TA='add_Vo()')

生成以下错误

Error in plot.window(c(1, 27), c(NA_real_, NA_real_)) : 
  need finite 'ylim' values

知道如何修复此错误?或者我应该使用chart_Series之外的其他内容来绘制xts有很多NA值的内容吗?

1 个答案:

答案 0 :(得分:2)

您可以使用na.locf此功能将NA值填入最后一个值。

chart_Series(na.locf(a),TA='add_Vo()')

我意识到这不是正确的答案。然后;

a_tmp<-cbind(matrix(na.locf(as.numeric(t(a[,1:4]))),ncol = 4,byrow = T),na.fill(a[,5],0))
colnames(a_tmp)<-colnames(a)
chart_Series(a_tmp,TA='add_Vo()')

现在na.locf使用上次观察到的价格填充每个NA值 - 在这种情况下,最后收盘价格 - 且音量必须为0,因此na.fill用于填充NA'在卷栏中。

enter image description here