From bf315e30cfc6aad25beb6ed09211bdcf813fd35c Mon Sep 17 00:00:00 2001 From: Eric Hung Date: Mon, 25 Jul 2016 16:09:59 +0800 Subject: [PATCH] Refactor addMFI to follow skeleton_TA structure Refactor addMFI to use skeleton_TA structure. chartMFI function is given to create Money Flow Indicator based on skeleton_TA structure. --- R/addMFI.R | 94 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 36 deletions(-) diff --git a/R/addMFI.R b/R/addMFI.R index 923877df..af3fb61b 100644 --- a/R/addMFI.R +++ b/R/addMFI.R @@ -7,45 +7,67 @@ `addMFI` <- function (n = 14, ..., on = NA, legend = "auto") { - lchob <- get.current.chob() - x <- as.matrix(lchob@xdata) - volume <- Vo(x) - x <- HLC(x) - x <- MFI(HLC = x, volume = volume, n = n) - yrange <- NULL - chobTA <- new("chobTA") - if (NCOL(x) == 1) { - chobTA@TA.values <- x[lchob@xsubset] - } - else chobTA@TA.values <- x[lchob@xsubset, ] - chobTA@name <- "chartTA" - if (any(is.na(on))) { - chobTA@new <- TRUE - } - else { - chobTA@new <- FALSE - chobTA@on <- on + lenv <- new.env() + lenv$chartMFI <- function(x, n, ..., on, legend) { + xdata <- lchob$Env$xdata + xsubset <- lchob$Env$xsubset + volume <- lchob$Env$vo + xdata <- HLC(xdata) + mfi <- MFI(HLC = xdata, volume = volume, n = n)[xsubset] + spacing <- x$Env$theme$spacing + x.pos <- 1 + spacing * (1:NROW(mfi) - 1) + xlim <- x$Env$xlim + ylim <- c(0,100) + theme <- x$Env$theme + + lines(x.pos, mfi, col = 8, lwd = 1, lend = 2, ...) } - chobTA@call <- match.call() - legend.name <- gsub("^addMFI", "Money Flow Index ", 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) + if(!is.character(legend) || legend == "auto") + legend <- gsub("^addMFI", "Money Flow Index ", deparse(match.call())) + mapply(function(name, value) { + assign(name, value, envir = lenv) + }, names(list(n = n, ..., on = on, legend = legend)), + list(n = n, ..., on = on, legend = legend)) + exp <- parse(text = gsub("list", "chartMFI", as.expression(substitute(list(x = current.chob(), + n = n, ..., on = on, legend = legend)))), srcfile = NULL) + exp <- c(exp, expression( + lc <- xts:::legend.coords("topleft", xlim, c(0,100)), + legend(x = lc$x, y = lc$y, + legend = c(paste(legend, ":"), + paste(format(last(mfi),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], 0, xlim[2], 100, col=theme$fill), + # add grid lines and left-side axis labels + segments(xlim[1], y_grid_lines(c(0,100)), + xlim[2], y_grid_lines(c(0,100)), + col = theme$grid, lwd = x$Env$grid.ticks.lwd, lty = 3), + text(xlim[1], y_grid_lines(c(0,100)), y_grid_lines(c(0,100)), + 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], 0, xlim[2], 100, border=theme$labels)), exp) + + lchob <- current.chob() + x <- lchob$Env$xdata + xsubset <- lchob$Env$xsubset + volume <- lchob$Env$vo + x <- HLC(x) + mfi <- MFI(HLC = x, volume = volume, n = n)[xsubset] + lchob$Env$mfi <- mfi + if(any(is.na(on))) { + lchob$add_frame(ylim=c(0,100),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 }