Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor addMFI to follow skeleton_TA structure #103

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 58 additions & 36 deletions R/addMFI.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
[email protected] <- x[lchob@xsubset]
}
else [email protected] <- 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 = [email protected], multi.col = [email protected],
spacing = lchob@spacing, width = lchob@width, bp = lchob@bp,
x.labels = [email protected], time.scale = [email protected],
isLogical = is.logical(x), legend = legend, legend.name = legend.name,
pars = list(gpars))
if (is.null(sys.call(-1))) {
TA <- [email protected]$TA
[email protected]$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
}