Skip to content

IBrokers- how to keep target file from being re-written?

2 messages · Vince Fulco, Jeff Ryan

#
For now, using reqHistoricalData() vs. the new wrapper reqHistory(),
setting endDateTime array and then looping thru writing to same file.
Func overwrites and attempt to add 'a'ppend qualifiers doesn't work.

dfile='/home/foo/'

now<- Sys.Date()-1

tp<- seq(now, length=6, by=paste(-5, 'days'))

# reformat dates to conform with IB needs YYYYMMDD

now2<- as.Date(tp, format="%Y%m%d")

reqHistoricalData(tws,
		    contract,
		    endDateTime=now2,
		    barSize = "5 mins",
		    duration = "5 D",
		    useRTH = "0",
		    whatToShow = "TRADES",
		    time.format = "1",
		    verbose = TRUE,
		    tickerId = "1",
		    #eventHistoricalData,
                    # this line fails
                    #file=paste(dfile,'EStest.dat',sep=''),open='a')
		    file=paste(dfile,'EStest.dat',sep=''))

* Would prefer to work from one file but could write out to numerous
and then merge.

Snippet from reqHistoricalData() suggests write.table would need to be
extended with 'append=TRUE' but sure there is something more
obvious...

#
if (!missing(file)) {
            cm[, 1] <- dts
            write.table(cm, file = file, quote = FALSE, row.names = FALSE,
                col.names = FALSE, sep = ",")
            invisible(return())
#


TIA, Vince
#
Hi Vince,

I should probably let a ... arg pass to write.table, but I'll have to
check that doesn't cause issues.

If you pass in an *open* file handle, you will likely get the output you want.

something like:

fh <- file("EStest.dat", open="a")
reqHistoricalData(tws, twsSTK("AAPL"), file=fh)


Alternatively, with reqHistory you can just let the function merge in
memory, and then use write.zoo to save to a csv file.

write.zoo(reqHistory(tws, twsSTK("AAPL")), file="AAPL.csv")
system("head AAPL.csv")
system("wc -l AAPL.csv")

HTH
Jeff
On Tue, Aug 11, 2009 at 11:25 AM, Vince Fulco<vfulco1 at gmail.com> wrote: