Skip to content

zoo.read intraday data

3 messages · szimine, Gabor Grothendieck, sl z

#
Hi Gabor et al.

the 
 f3 <- function(...) as.POSIXct(paste(...), format = "%Y%m%d %H:%M:%S" ) 

helped me to read intraday data from file
######
<TICKER>,<NAME>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT>
ICE.BRN,ice.brn_m5,5,20100802,10:40:00,79.21000,79.26000,79.16000,79.20000,238,0
ICE.BRN,ice.brn_m5,5,20100802,10:45:00,79.19000,79.26000,79.19000,79.21000,413,0
######

##intraday data 5m  file
fnameId= "./finam_brn_m5.csv"
pDateTimeColumns <- list(4,5) 
b <- read.zoo(fnameId, index=pDateTimeColumns , sep=",", header=TRUE, 
FUN=f3   )
xb <- as.xts(b)
X.TICKER. X.NAME.    X.PER. X.OPEN. X.HIGH. X.LOW.
X.CLOSE. X.VOL. X.OPENINT.
2010-08-02 10:40:00 ICE.BRN   ice.brn_m5 5      79.21   79.26   79.16  79.20    
238   0         
2010-08-02 10:45:00 ICE.BRN   ice.brn_m5 5      79.19   79.26   79.19  79.21    
413   0

problem is that after the conversion to xts  numeric values got converted to
chars
X.TICKER. X.NAME.      X.PER. X.OPEN. X.HIGH. X.LOW. 
X.CLOSE. X.VOL. X.OPENINT.
2010-08-02 10:40:00 "ICE.BRN" "ice.brn_m5" "5"    "79.21" "79.26" "79.16"
"79.20"  " 238" "0"       
2010-08-02 10:45:00 "ICE.BRN" "ice.brn_m5" "5"    "79.19" "79.26" "79.19"
"79.21"  " 413" "0" 


and quantmod charting does  not work.

Q.  how to prevent converting to char with xts ? 

I suspect the problem is that index is constructed from two columns  date 
and time.
R version 2.12.1 (2010-12-16)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] C
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
other attached packages:
[1] quantmod_0.3-15 TTR_0.20-2      Defaults_1.1-1  xts_0.7-6.11   
zoo_1.7-0     


Slava
#
On Tue, Dec 21, 2010 at 11:36 PM, szimine <szimine at gmail.com> wrote:
Read it all in using read.csv and the reread it using read.zoo (note
that read.zoo can read data.frames) excluding the bad columns or else
use the colClasses argument to suppress the unwanted column(s).  Here
are 4 methods.

Lines <-
"<TICKER>,<NAME>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT>
ICE.BRN,ice.brn_m5,5,20100802,10:40:00,79.21000,79.26000,79.16000,79.20000,238,0
ICE.BRN,ice.brn_m5,5,20100802,10:45:00,79.19000,79.26000,79.19000,79.21000,413,0"

library(zoo)

# method 1. read.csv/read.zoo with split= and removing col 2

DF <- read.csv(textConnection(Lines))
z1 <- read.zoo(DF[-2], split = 1, index = list(3, 4), FUN = f3)

# method 2. read.csv/read.zoo removing col 1 and 2
# this one only works if there is one ticker

DF <- read.csv(textConnection(Lines))
z2 <- read.zoo(DF[-(1:2)], index = list(2, 3), FUN = f3)

# method 3.  read.zoo with colClasses as in #1
colClasses <- c("character", "NULL", "numeric", "character",
"character", rep("numeric", 6))
z3 <- read.zoo(textConnection(Lines), header = TRUE, sep = ",",
  split = 1, index = list(3, 4), FUN = f3, colClasses = colClasses)

#4. A method similar to #2 could also be used based on colClasses.
#
Dear Gabor, many thanks for the quick reply.

for the sake of a working example in list archive, code below reads  a
csv with 5 min intraday bars and converts it to a
quantmod::barChart_able  xts object

##csv file 5min bars with the format below
#<TICKER>,<NAME>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT>
#ICE.BRN,ice.brn_m5,5,20100802,10:40:00,79.21000,79.26000,79.16000,79.20000,238,0
# ICE.BRN,ice.brn_m5,5,20100802,10:45:00,79.19000,79.26000,79.19000,79.21000,413,0
##########

##intraday data 5m  file
fnameId= "~/rlab/csvdat/finam_brn_m5.csv"
df <-read.csv(fnameId,sep=',' , header=TRUE)
f3 <- function(...) as.POSIXct(paste(...), format = "%Y%m%d %H:%M:%S")
b <- read.zoo(df[-(1:3)], index=list(1,2),    FUN=f3   )
xb <- as.xts(b)
colnames(xb)=c("Open","High","Low","Close","Volume","OpenInt")
barChart(xb)





On Wed, Dec 22, 2010 at 1:39 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote: