[R-SIG-Finance] [R-sig-finance] xts and barChart (quantmod)

kafkaz kafka at centras.lt
Wed Jul 23 21:03:44 CEST 2008


My data is stored in a MySQL database. So, I'm using RMySQL to load
information about price performance. After that, I'm using xts to convert
selected data into XTS object. Example:

rs <- dbSendQuery(con, "select ....");
dji<- fetch(rs, n = -1) 
dji2<-xts(dji,order.by=as.POSIXct(dji[,1]))
colnames(dji2)<-c('Datetime', 'Open', 'High', 'Low', 'Close', 'Volume')
At this point I have a xts object:

class(dji2)
[1] "xts" "zoo"

 >str(dji2)
An ‘xts’ object from 2004-01-07 15:50:00 to 2004-01-09 11:45:00 containing:
  Data: chr [1:50, 1:6] "2004-01-07 15:50:00" "2004-01-07 15:55:00"
"2004-01-07 16:00:00" "2004-01-07 16:05:00" ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:50] "2004-01-07 15:50:00" "2004-01-07 15:55:00" "2004-01-07
16:00:00" "2004-01-07 16:05:00" ...
  ..$ : chr [1:6] "Datetime" "Open" "High" "Low" ...
  Indexed by:  POSIXct[1:50], format: "2004-01-07 15:50:00" "2004-01-07
15:55:00" "2004-01-07 16:00:00" "2004-01-07 16:05:00" ...
  xts Attributes:  
 NULL

dji2[1:4,]
                    Datetime            Open  High  Low   Close Volume
2004-01-07 15:50:00 2004-01-07 15:50:00 10483 10483 10480 10480   0   
2004-01-07 15:55:00 2004-01-07 15:55:00 10479 10479 10465 10465   0   
2004-01-07 16:00:00 2004-01-07 16:00:00 10464 10464 10460 10460   0   
2004-01-07 16:05:00 2004-01-07 16:05:00 10459 10459 10442 10442   0 

If I try to use barChart function (quantmod package), I will get this error:
Error in checkSlotAssignment(object, name, value) : 
  assignment of an object of class "character" is not valid for slot
"yrange" in an object of class "chob"; is(value, "numeric") is not TRUE

The problem lies in the type of columns - open, high, low and close have
type character, but quantmod function expects integer:

if (is.OHLC(x)) {
        chob at yrange <- c(min(Lo(x), na.rm = TRUE), max(Hi(x), 
            na.rm = TRUE))
    }
    else chob at yrange <- range(x[, 1], na.rm = TRUE)

is.character(dji2[,5])
[1] TRUE

Any idea how to fix it?
Thank you.
-- 
View this message in context: http://www.nabble.com/xts-and-barChart-%28quantmod%29-tp18617785p18617785.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list