From 05aa4d0c636268d58f750e1b48177e42f7595ac5 Mon Sep 17 00:00:00 2001 From: Eric Hung Date: Thu, 28 Jul 2016 16:47:40 +0800 Subject: [PATCH] Refactor addVolatility to follow skeleton_TA structure chartVolatility function is given to create volatility series based on skeleton_TA structure. --- R/addVolatility.R | 91 +++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/R/addVolatility.R b/R/addVolatility.R index 2fe1f718..75b158bb 100644 --- a/R/addVolatility.R +++ b/R/addVolatility.R @@ -7,43 +7,66 @@ `addVolatility` <- function (n = 10, calc = "close", N = 260, ..., on = NA, legend = "auto") { - lchob <- get.current.chob() - x <- as.matrix(lchob@xdata) - x <- OHLC(x) - x <- volatility(OHLC = x, n = n, calc = calc, N = N) - yrange <- NULL - chobTA <- new("chobTA") - if (NCOL(x) == 1) { - chobTA@TA.values <- x[lchob@xsubset] + lenv <- new.env() + lenv$chartVolatility <- function(x, n, calc, N, ..., on, legend) { + xdata <- x$Env$xdata + xsubset <- x$Env$xsubset + xdata <- OHLC(xdata) + vol <- volatility(OHLC = xdata, n = n, calc = calc, N = N)[xsubset] + spacing <- x$Env$theme$spacing + x.pos <- 1 + spacing * (1:NROW(vol) - 1) + xlim <- x$Env$xlim + ylim <- c(min(vol, na.rm=TRUE) * 0.95, max(vol, na.rm=TRUE) * 1.05) + theme <- x$Env$theme + + lines(x.pos, vol, col = 8, lwd = 1, lend = 2, ...) + } - else chobTA@TA.values <- x[lchob@xsubset, ] - chobTA@name <- "chartTA" + if(!is.character(legend) || legend == "auto") + legend <- gsub("^add", "", deparse(match.call())) + mapply(function(name, value) { + assign(name, value, envir = lenv) + }, names(list(n = n, calc = calc, N = N, ..., on = on, legend = legend)), + list(n = n, calc = calc, N = N, ..., on = on, legend = legend)) + exp <- parse(text = gsub("list", "chartVolatility", as.expression(substitute(list(x = current.chob(), + n = n, calc = calc, N = N, ..., on = on, legend = legend)))), srcfile = NULL) + exp <- c(exp, expression( + lc <- xts:::legend.coords("topleft", xlim, c(min(vol, na.rm=TRUE) * 0.95,max(vol, na.rm=TRUE) * 1.05)), + legend(x = lc$x, y = lc$y, + legend = c(paste(legend, ":"), + format(last(vol),nsmall = 3L)), + text.col = c(theme$fg, 8), + xjust = lc$xjust, + yjust = lc$yjust, + bty = "n", + y.intersp=0.95))) + exp <- c(expression( + # add inbox color + rect(xlim[1], min(vol, na.rm=TRUE) * 0.95, xlim[2], max(vol, na.rm=TRUE) * 1.05, col=theme$fill), + # add grid lines and left-side axis labels + segments(xlim[1], y_grid_lines(c(min(vol, na.rm=TRUE) * 0.95,max(vol, na.rm=TRUE) * 1.05)), + xlim[2], y_grid_lines(c(min(vol, na.rm=TRUE) * 0.95,max(vol, na.rm=TRUE) * 1.05)), + col = theme$grid, lwd = x$Env$grid.ticks.lwd, lty = 3), + text(xlim[1], y_grid_lines(c(min(vol, na.rm=TRUE) * 0.95,max(vol, na.rm=TRUE) * 1.05)), y_grid_lines(c(min(vol, na.rm=TRUE) * 0.95,max(vol, na.rm=TRUE) * 1.05)), + col = theme$labels, srt = theme$srt, + offset = 0.5, pos = 2, cex = theme$cex.axis, xpd = TRUE), + # add border of plotting area + rect(xlim[1], min(vol, na.rm=TRUE) * 0.95, xlim[2], max(vol, na.rm=TRUE) * 1.05, border=theme$labels)), exp) + + lchob <- current.chob() + x <- lchob$Env$xdata + xsubset <- lchob$Env$xsubset + x <- OHLC(x) + vol <- volatility(OHLC = x, n = n, calc = calc, N = N)[xsubset] + lchob$Env$vol <- vol if (any(is.na(on))) { - chobTA@new <- TRUE - } - else { - chobTA@new <- FALSE - chobTA@on <- on - } - chobTA@call <- match.call() - legend.name <- gsub("^add", "", deparse(match.call())) - gpars <- c(list(...), list(col = 8))[unique(names(c(list(col = 8), - list(...))))] - chobTA@params <- list(xrange = lchob@xrange, yrange = yrange, - colors = lchob@colors, color.vol = lchob@color.vol, multi.col = lchob@multi.col, - spacing = lchob@spacing, width = lchob@width, bp = lchob@bp, - x.labels = lchob@x.labels, time.scale = lchob@time.scale, - isLogical = is.logical(x), legend = legend, legend.name = legend.name, - pars = list(gpars)) - if (is.null(sys.call(-1))) { - TA <- lchob@passed.args$TA - lchob@passed.args$TA <- c(TA, chobTA) - lchob@windows <- lchob@windows + ifelse(chobTA@new, 1, - 0) - do.call("chartSeries.chob", list(lchob)) - invisible(chobTA) + lchob$add_frame(ylim=c(min(vol, na.rm=TRUE) * 0.95, + max(vol, na.rm=TRUE) * 1.05), asp=1, fixed=TRUE) + lchob$next_frame() } else { - return(chobTA) + lchob$set_frame(sign(on)*(abs(on)+1L)) } + lchob$replot(exp, env=c(lenv, lchob$Env), expr=TRUE) + lchob }