[R-SIG-Finance] [R-sig-finance] Extracting OHLC from trade price series

Yuri Volchik yuri.volchik at gmail.com
Fri Feb 29 11:27:26 CET 2008


Hi Jeff,

suppose tick data looks like this:
01/06/2007,00:35:33,2795.,9,
01/06/2007,01:13:14,2799.,1,
01/06/2007,01:13:49,2799.75,5,
01/06/2007,01:19:24,2799.75,4,
01/06/2007,01:20:00,2800.,6,
01/06/2007,01:20:00,2800.,27,
01/06/2007,01:20:20,2800.,1,
1/06/2007,01:20:33,2800.,1,

so formatted data looks:
2007/06/01 00:55:00,#NA,#NA,#NA,#NA,#NA,#NA
2007/06/01 01:00:00,#NA,#NA,#NA,#NA,#NA,#NA
2007/06/01 01:05:00,#NA,#NA,#NA,#NA,#NA,#NA
2007/06/01 01:10:00,#NA,#NA,#NA,#NA,#NA,#NA
2007/06/01 01:15:00,2799,2799.75,6,2,194,71
2007/06/01 01:20:00,2799.75,2799.75,4,1,264,36
2007/06/01 01:25:00,2800,2800,10,4,0,239
2007/06/01 01:30:00,#NA,#NA,#NA,#NA,#NA,#NA
2007/06/01 01:35:00,#NA,#NA,#NA,#NA,#NA,#NA
2007/06/01 01:40:00,#NA,#NA,#NA,#NA,#NA,#NA
2007/06/01 01:45:00,#NA,#NA,#NA,#NA,#NA,#NA
2007/06/01 01:50:00,2794,2794,1,1,10,290
2007/06/01 01:55:00,2799,2800,2,2,116,174

I calculate some other stuff on ticks, not just OHLC.

This is my R routine, very slow and inefficient i'm sure:

number.of.intervals<-24*60/TimeFrame   #Number of time intervals in one day
seconds.in.time.interval=TimeFrame*60
times<-seq(ISOdatetime(2000,1,1,0,TimeFrame,0),by=seconds.in.time.interval,length=number.of.intervals) 
#names of time intervals
times<-format(times,"%H:%M")
d.start<-as.POSIXlt(as.Date(time(dat.)[1]))+TimeFrame*60
d.end<-as.POSIXlt(as.Date(time(dat.)[dim(dat.)[1]]))+3600*24
Index<-seq(from=d.start,to=d.end,by=TimeFrame*60)
dd<-matrix(NA,nrow=length(Index),ncol=1)
dd[Index%in%time(dat.),1]<-dat.[time(dat.)%in%Index,Col]
if (na.max>0)
dd[,1]<-remove.na(dd,max.na=na.max,interp.type=interp.type,column=1)
n.days<-length(Index)/number.of.intervals
dd<-t(dd)
dim(dd)<-c(number.of.intervals,n.days)
ddd<-zoo(t(dd),order.by=seq(from=as.Date(d.start),to=as.Date(d.end-3600*24),by='day'))
colnames(ddd)<-times
days<-unique(as.Date(time(dat.)))
ddd<-ddd[time(ddd)%in%days,]


I'm sure it can be improved. I think the design should incorporate ability
to specify custom column with price data and different aggregation
procedures to be performed on data. Would be willing to help you to develop
such functions.

Best regards,
Yuri



-- 
View this message in context: http://www.nabble.com/Extracting-OHLC-from-trade-price-series-tp15579652p15755859.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list