[R] zoo.read intraday data

sl z szimine at gmail.com
Wed Dec 22 22:45:12 CET 2010


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:
> On Tue, Dec 21, 2010 at 11:36 PM, szimine <szimine at gmail.com> wrote:
>>
>> 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)
>>
>>
>>> head(b,2) ##
>>                    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
>>
>>> head(xb,2)
>>                    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"
>>
>
>
> 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.
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>



More information about the R-help mailing list