-4

I am trying to save an object to a text file with the command write.table(ESH2, "c:/ESH2.txt", sep=","). The problem is that the saved time series doesn't contain date and time values that I downloaded. I used the package twsInstrument and the command getBAT(ESH2)

The data i have when loading it into R with the command load(file = "C:/ESH2.Rdata")

               ES.Bid.Price ES.Ask.Price ES.Trade.Price ES.Mid.Price ES.Volume
1323700200        1237.25        1237.50        1237.50     1237.375      6954
1324057980        1210.25        1210.50        1210.25     1210.375      3792
1324058040        1210.50        1211.00        1211.00     1210.750      3305
.........
.........
.........
1324058100           NA           NA             NA           NA         823

attr(,".indexCLASS")
[1] POSIXct POSIXt 
attr(,".indexTZ")
[1] 
attr(,"from")
[1] 20111211  23:59:59
attr(,"to")
[1] 20111216  23:59:59
attr(,"src")
[1] IB
attr(,"updated")
[1] "2011-12-16 18:54:55 CET"

The first column should show Date_Time and not 1323700200.

I am searching for an easy way to download the data once per week and merge the data.

p.s Yes i can read through the tutorials/books to accomplish this and yes i will do it but the problem is my lack of time. I want to start collecting the data this week cause interactive brokers is limiting data requests 1min data = 5DAYS maximum. I am thankful for any help and suggestions.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Joe Joeansjen
  • 75
  • 1
  • 2
  • 6
  • Could please post a reproducible example? http://stackoverflow.com/q/5963269/687713 – Luciano Selzer Dec 16 '11 at 18:54
  • You need to provide more detail (e.g. a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)). – Joshua Ulrich Dec 16 '11 at 18:54
  • This is still hard to reproduce, because even after installing `twsInstrument` and all of its dependencies I find I still need an Interactive Brokers account to use `currency("USD"); getBAT("ESH2")`. However, I think now knowing that your object is an `xts` object will help ... – Ben Bolker Dec 17 '11 at 19:30
  • Having written (and deleted) my own answer I now see that @DWin's is correct. Did you try it ??? – Ben Bolker Dec 17 '11 at 19:37

2 Answers2

5

You probably have an xts or zoo object and need to use the write.zoo function. If I'm right about the structure of the "ESH2" object, then the data that you are calling the "first column" is actually the rownames which in zoo/xts parlance is the "index" while the data can be accessed with coredata and is a matrix object.

From the examples on the read/write.zoo page:

Lines <- "CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 300 0
CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 40 51 73.25 100 0
CVX 20070201 9 40 52 73.25 100 0
CVX 20070201 9 40 53 73.25 300 0"

z <- read.zoo(textConnection(Lines), 
    colClasses = c("NULL", "NULL", "numeric", "numeric", "numeric", "numeric",
        "numeric", "NULL"), 
    col.names = c("Symbol", "Date", "Hour", "Minute", "Second", "Price", 
        "Volume", "junk"),
    index = 1:3,  # do not count columns that are "NULL" in colClasses
    FUN = function(h, m, s) times(paste(h, m, s, sep = ":")),
    FUN2 = function(tt) trunc(tt, "00:00:05"),
    aggregate = mean)
# The only material I added.
write.zoo(z)
"Index" "Price" "Volume"
09:30:50 73.25 32660
09:40:50 73.25 166.666666666667
IRTFM
  • 258,963
  • 21
  • 364
  • 487
1

If your only goal is to save the object so that you can access it from R again in the future then using

save(ESH2, file = "C:/ESH2.Rdata") # or whatever you want to call the saved file

will do that for you. You can get the object back again later by using load:

load(file = "C:/ESH2.Rdata")

But as mentioned in the comments if you want help getting the write.table or write.csv solution to work then you'll need to provide more information.

Dason
  • 60,663
  • 9
  • 131
  • 148
  • 2
    You should add this information to your question (by editing it), as well as showing the result of `str(ESH2)`. – Ben Bolker Dec 16 '11 at 20:10