Search code examples
mysqlribrokers

Saving timestamp on IBrokers package


I am having some issues accessing the timestamp data in the IBrokers package.

Here is an example of the data I get:

                    AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.WAP AAPL.hasGaps AAPL.Count
2015-01-09 17:59:00       112    112.04   111.95        112        6043  112.011            0       2240

So when I run data[,0] I get

2015-01-09 17:59:00

The problem is that later on when I try to save that into a MySQL table I get the following error:

Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) : 
  'dimnames' applied to non-array

It looks like data[,0] does not simply contains the timestamp.

When I do a summary of the variable ts which contains data[,0] I get:

Error in `colnames<-`(`*tmp*`, value = c("ts.1", "ts.0")) : 
  'names' attribute [2] must be the same length as the vector [1]

Any tip on how to access the timestamp or convert the contents of ts to char so I can insert it into the DB will be appreciated.

EDIT:

dput() output

structure(c(112, 112.04, 111.95, 112, 6043, 112.011, 0, 2240), .Dim = c(1L, 
8L), index = structure(1420837140, tzone = "", tclass = c("POSIXct", 
"POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", 
"POSIXt"), .indexTZ = "", tzone = "", .Dimnames = list(NULL, 
    c("AAPL.Open", "AAPL.High", "AAPL.Low", "AAPL.Close", "AAPL.Volume", 
    "AAPL.WAP", "AAPL.hasGaps", "AAPL.Count")), class = c("xts", 
"zoo"), from = "20150112  02:52:24", to = "20150112  02:53:24", src = "IB", updated = structure(33434342.12435, class = c("POSIXct", 
"POSIXt")))

Solution

  • As suggested by @JoshuaUlrich on the comments to my question, the answer was on the zoo package.

    Full documentation on zoo can be found here

    In my particular case by including the zoo library and simply doing:

    time(data[,0])
    

    I solve the Error in dimnames() error.

    Hope it helps someone else.