Skip to content
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
92 changes: 58 additions & 34 deletions R/addTDI.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,67 @@
`addTDI` <-
function (n = 20, multiple = 2, ..., on = NA, legend = "auto")
{
lchob <- get.current.chob()
x <- as.matrix(lchob@xdata)
x <- Cl(x)
x <- TDI(price = x, n = n, multiple = multiple)
yrange <- NULL
chobTA <- new("chobTA")
if (NCOL(x) == 1) {
[email protected] <- x[lchob@xsubset]
lenv <- new.env()
lenv$chartTDI <- function(x, n, multiple, ..., on, legend) {
xdata <- x$Env$xdata
xsubset <- x$Env$xsubset
xdata <- Cl(xdata)
tdi <- TDI(price = xdata, n = n, multiple = multiple)[xsubset]
spacing <- x$Env$theme$spacing
x.pos <- 1 + spacing * (1:NROW(tdi) - 1)
xlim <- x$Env$xlim
ylim <- range(tdi, na.rm=TRUE)*1.05
theme <- x$Env$theme

lines(x.pos, tdi[,1], col = 5, lwd = 1, lend = 2, ...)
lines(x.pos, tdi[,2], col = 6, lwd = 1, lend = 2, ...)

}
else [email protected] <- x[lchob@xsubset, ]
chobTA@name <- "chartTA"
if(!is.character(legend) || legend == "auto")
legend <- gsub("^addTDI", "Trend Detection Index ", deparse(match.call()))
mapply(function(name, value) {
assign(name, value, envir = lenv)
}, names(list(n = n, multiple = multiple, ..., on = on, legend = legend)),
list(n = n, multiple = multiple, ..., on = on, legend = legend))
exp <- parse(text = gsub("list", "chartTDI", as.expression(substitute(list(x = current.chob(),
n = n, multiple = multiple, ..., on = on, legend = legend)))), srcfile = NULL)
exp <- c(exp, expression(
lc <- xts:::legend.coords("topleft", xlim, range(tdi, na.rm=TRUE)*1.05),
legend(x = lc$x, y = lc$y,
legend = c(paste(legend, ":"),
paste("tdi :",format(last(tdi[,1]),nsmall = 3L)),
paste("di :",format(last(tdi[,1]),nsmall = 3L))),
text.col = c(theme$fg, 5, 6),
xjust = lc$xjust,
yjust = lc$yjust,
bty = "n",
y.intersp=0.95)))
exp <- c(expression(
# add inbox color
rect(xlim[1], range(tdi, na.rm=TRUE)[1]*1.05, xlim[2], range(tdi, na.rm=TRUE)[2]*1.05, col=theme$fill),
# add grid lines and left-side axis labels
segments(xlim[1], y_grid_lines(range(tdi, na.rm=TRUE)*1.05),
xlim[2], y_grid_lines(range(tdi, na.rm=TRUE)*1.05),
col = theme$grid, lwd = x$Env$grid.ticks.lwd, lty = 3),
text(xlim[1], y_grid_lines(range(tdi, na.rm=TRUE)*1.05), y_grid_lines(range(tdi, 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], range(tdi, na.rm=TRUE)[1]*1.05, xlim[2], range(tdi, na.rm=TRUE)[2]*1.05, border=theme$labels)), exp)

lchob <- current.chob()
x <- lchob$Env$xdata
xsubset <- lchob$Env$xsubset
x <- Cl(x)
tdi <- TDI(price = x, n = n, multiple = multiple)[xsubset]
lchob$Env$tdi <- tdi
if (any(is.na(on))) {
chobTA@new <- TRUE
}
else {
chobTA@new <- FALSE
chobTA@on <- on
}
chobTA@call <- match.call()
legend.name <- gsub("^addTDI", "Trend Detection Index ", deparse(match.call()))
gpars <- c(list(...), list(col = 5:6))[unique(names(c(list(col = 5:6),
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)
lchob$add_frame(ylim=range(tdi, 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
}