"ohlc" <- function(op,hi,lo,cl,f=0.729,nsplit=0,split=0){ # Compute volatility based on daily open, high, low and closing prices. # f is the fraction of the day that trading is closed. # # If nsplit > 0, then split contains the information of (time,ratio) # of the splits. # n=length(op) nm1=n-1 #print(split) s0 = diff(cl)^2 s1 = (op[2:n]-cl[1:nm1])^2/(2*f) + (cl[2:n]-op[2:n])^2/(2*(1-f)) s2 = 0.3607*(hi[2:n]-lo[2:n])^2 s3 = 0.17*(op[2:n]-cl[1:nm1])^2/f + 0.83*(hi[2:n]-lo[2:n])^2/((1-f)*4*log(2)) s5= 0.5*(hi[2:n]-lo[2:n])^2-0.386*(cl[2:n]-op[2:n])^2 s6= 0.12*(op[2:n]-cl[1:nm1])^2/f + 0.88 *s5/(1-f) # take care of stock split if (nsplit > 0){ if(nsplit==1){ spp=matrix(split,1,2) } for (i in 1:nsplit){ idx=spp[i,1] ratio=spp[i,2] s0[idx]=(cl[idx+1]-cl[idx]*ratio)^2 s1[idx]= (op[idx+1]-cl[idx]*ratio)^2/(2*f)+(cl[idx+1]-op[idx+1])^2/(2*(1-f)) s3[idx]=0.17*(op[idx+1]-cl[idx]*ratio)^2/f + 0.83*(hi[idx+1]-lo[idx+1])^2/((1-f)*4*log(2)) s6[idx]= 0.12*(op[idx+1]-cl[idx]*ratio)^2/f + 0.88 *s5[idx]/(1-f) } } ohlc<-list(s0sq=s0,s1sq=s1,s2sq=s2,s3sq=s3,s5sq=s5,s6sq=s6) }