Sun, 14 Dec 2003

Accelerating plotOHLC by a few orders of magnitude

The plotOHLC function in the tseries package is useful to plot timeseries of various financial assets with open/high/low/close data. I had often wondered if it could be made to run a little faster. It turns out that the following patch does
--- plotOHLC.R.orig     2003-12-14 12:02:20.000000000 -0600
+++ plotOHLC.R  2003-12-14 12:03:42.000000000 -0600
@@ -21,14 +21,9 @@
         ylim <- range(x[is.finite(x)])
     plot.new()
     plot.window(xlim, ylim, ...)
-    for (i in 1:NROW(x)) {
-        segments(time.x[i], x[i, "High"], time.x[i], x[i, "Low"],
-            col = col[1], bg = bg)
-        segments(time.x[i] - dt, x[i, "Open"], time.x[i], x[i,
-            "Open"], col = col[1], bg = bg)
-        segments(time.x[i], x[i, "Close"], time.x[i] + dt, x[i,
-            "Close"], col = col[1], bg = bg)
-    }
+    segments(time.x, x[, "High"], time.x, x[, "Low"], col = col[1], bg = bg)
+    segments(time.x - dt, x[, "Open"], time.x, x[, "Open"], col = col[1], bg =$
+    segments(time.x, x[, "Close"], time.x + dt, x[, "Close"], col = col[1], bg$
     if (ann)
         title(main = main, xlab = xlab, ylab = ylab, ...)
     if (axes) {
decrease the time spent on a series of ~500 points by a factor of sixty:
> IBM<-get.hist.quote("IBM", "2001-12-14")
trying URL
http://chart.yahoo.com/table.csv?s=IBM&a=11&b=13&c=2001&d=11&e=12&f=2003&g=d&q=$
Content type application/octet-stream' length unknown
opened URL
.......... .......... ...
downloaded 23Kb

time series starts 2001-12-12
time series ends   2003-12-11
> system.time(plotOHLC(IBM))                    # original
[1] 1.56 0.26 5.11 0.00 0.00
> system.time(fastplotOHLC(IBM))                # patched
[1] 0.02 0.00 0.05 0.00 0.00

/computers/R | permanent link