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 addVolatility to follow skeleton_TA structure #107

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
91 changes: 57 additions & 34 deletions R/addVolatility.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}