diff --git a/R/addKST.R b/R/addKST.R index 9e668fb6..cd53d564 100644 --- a/R/addKST.R +++ b/R/addKST.R @@ -9,46 +9,74 @@ function (n = c(10, 10, 10, 15), nROC = c(10, 15, 20, 30), nSig = 9, maType, wts = 1:NROW(n), ..., on = NA, legend = "auto") { - lchob <- get.current.chob() - x <- as.matrix(lchob@xdata) + lenv <- new.env() + lenv$chartKST <- function(x, n, nROC, nSig, maType, wts, ..., on, legend) { + xdata <- x$Env$xdata + xsubset <- x$Env$xsubset + xdata <- xdata[xsubset] + xdata <- coredata(Cl(xdata)) + kst <- KST(price = xdata, n = n, nROC = nROC, nSig = nSig, maType = maType, + wts = wts) + spacing <- x$Env$theme$spacing + x.pos <- 1 + spacing * (1:NROW(kst) - 1) + xlim <- x$Env$xlim + ylim <- range(kst, na.rm=TRUE) * 1.05 + theme <- x$Env$theme + + lines(x.pos, kst[,1], col = 6, lwd = 1, lend = 2, ...) + lines(x.pos, kst[,2], col = 7, lwd = 1, lend = 2, ...) + } + if(missing(maType)) maType <- "SMA" + if(!is.character(legend) || legend == "auto") + legend <- gsub("^addKST", "Know Sure Thing", deparse(match.call())) + mapply(function(name, value) { + assign(name, value, envir = lenv) + }, names(list(n = n, nROC = nROC, nSig = nSig, + maType = maType, wts = wts, ..., on = on, legend = legend)), + list(n = n, nROC = nROC, nSig = nSig, + maType = maType, wts = wts, ..., on = on, legend = legend)) + exp <- parse(text = gsub("list", "chartKST", as.expression(substitute(list(x = current.chob(), + n = n, nROC = nROC, nSig = nSig, + maType = maType, wts = wts, ..., on = on, legend = legend)))), srcfile = NULL) + exp <- c(exp, expression( + lc <- xts:::legend.coords("topleft", xlim, range(kst, na.rm=TRUE) * 1.05), + legend(x = lc$x, y = lc$y, + legend = c(legend, + paste("kst :",format(last(kst[,1]),nsmall = 3L)), + paste("signal :",format(last(kst[,2]),nsmall = 3L))), + text.col = c(theme$fg, 6, 7), + xjust = lc$xjust, + yjust = lc$yjust, + bty = "n", + y.intersp=0.95))) + exp <- c(expression( + # add inbox color + rect(xlim[1], range(kst, na.rm=TRUE)[1] * 1.05, xlim[2], range(kst, na.rm=TRUE)[2] * 1.05, col=theme$fill), + # add grid lines and left-side axis labels + segments(xlim[1], y_grid_lines(range(kst, na.rm=TRUE) * 1.05), + xlim[2], y_grid_lines(range(kst, na.rm=TRUE) * 1.05), + col = theme$grid, lwd = x$Env$grid.ticks.lwd, lty = 3), + text(xlim[1], y_grid_lines(range(kst, na.rm=TRUE) * 1.05), y_grid_lines(range(kst, 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(kst, na.rm=TRUE)[1] * 1.05, xlim[2], range(kst, na.rm=TRUE)[2] * 1.05, border=theme$labels)), exp) + + lchob <- current.chob() + x <- lchob$Env$xdata + xsubset <- lchob$Env$xsubset + x <- x[xsubset] x <- coredata(Cl(x)) - x <- KST(price = x, n = n, nROC = nROC, nSig = nSig, maType = maType, + kst <- KST(price = x, n = n, nROC = nROC, nSig = nSig, maType = maType, wts = wts) - 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 + lchob$Env$kst <- kst + if(is.na(on)) { + lchob$add_frame(ylim=range(kst, na.rm=TRUE) * 1.05,asp=1,fixed=TRUE) + lchob$next_frame() } else { - chobTA@new <- FALSE - chobTA@on <- on - } - chobTA@call <- match.call() - legend.name <- gsub("^addKST", "Know Sure Thing ", deparse(match.call())) - gpars <- c(list(...), list(col = 6:7))[unique(names(c(list(col = 6:7), - 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) - chartSeries.chob <- chartSeries.chob - do.call("chartSeries.chob", list(lchob)) - invisible(chobTA) + lchob$set_frame(sign(on)*(abs(on)+1L)) } - else { - return(chobTA) - } -} - + lchob$replot(exp, env=c(lenv,lchob$Env), expr=TRUE) + lchob +} \ No newline at end of file