[R] plotOHLC unequally spaced time.

Gabor Grothendieck ggrothendieck at myway.com
Wed Oct 6 19:56:02 CEST 2004

Per Wiklund <par <at> wiklund.net> writes:

: Hi I have som financial tick data that i have converted in to 45 min
: bars. These are unequally spaced in time. I wonder if it is possible to
: plot these like bars as the plotOHLC does. As I have understood plotOHLC
: uses class(mts) that must be equally spaced?

plotOHLC is only a couple screenfuls of code so you could adapt it to your
needs.  Another possibility is to fill in the missing spots with NAs and
turn the resulting regular series into an mts.

Here is an example of the latter.

Not sure how you have it represented right now but suppose its in 
matrix mat with the times in tt:

   mat <- cbind(Open = 11:20, High = 21:30, Low = 1:10, Close = 12:21)
   tt <- c(1:2, 4:11)

Then you could:

- convert it to zoo
- create a regular sequence of times and use merge.zoo to merge that 
  with your series thereby filling in the missing times with NAs
- convert result back to ts/mts and then 
- use plotOHLC on the result

   z <- zoo(mat, tt)
   fill <- zoo(,seq(min(tt), max(tt)))
   zf <- merge(fill, z)[,-1]  # fill in missing times with NAs
   z.mt <- ts(unclass(zf), start = index(zf)[1], end = index(zf)[nrow(zf)])

You will likely have to adapt the above to your specific 
data and time representation.

More information about the R-help mailing list