1

I have a xts series "a" containing the 15minute OHLC and volume of SPY on 2015-09-10 between 9:30AM to 13:00PM, values are NA between 13:00PM to 4PM.

I want to plot the whole series with volume bars below the candlesticks.

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()')

generates the following error

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

Any idea how can I fix this error? Or should I use something other than chart_Series for plotting xts with lots of NA values?

Jim Green
  • 261
  • 4
  • 10
  • From help page, there is as Note: *Highly experimental (read: alpha) use with caution.* You should use another function and check whether this already has been reported [here](https://github.com/joshuaulrich/quantmod/issues). –  Sep 15 '15 at 04:14

1 Answers1

2

You can use na.locf this function fills NA values with last values.

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

I realized that it's not the right answer. Then;

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()')

Now na.locf fills every NA values with last observed price -in this case last close price- and volume must be 0 so na.fillis used for filling NA's in volume column.

enter image description here

vck
  • 827
  • 5
  • 10