[R] A faster plotOHLC() for the tseries package

Dirk Eddelbuettel edd at debian.org
Sun Dec 14 19:28:46 CET 2003


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 = bg)
+    segments(time.x, x[, "Close"], time.x + dt, x[, "Close"], col = col[1], bg = 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=q&y=0&z=IBM&x=.csv'
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


Regards,  Dirk

-- 
Those are my principles, and if you don't like them... well, I have others.
                                                -- Groucho Marx




More information about the R-help mailing list