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 addOBV to follow skeleton_TA structure #104

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
93 changes: 58 additions & 35 deletions R/addOBV.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,66 @@
`addOBV` <-
function (..., on = NA, legend = "auto")
{
lchob <- get.current.chob()
x <- try.xts(lchob@xdata, error=FALSE)
x <- OBV(price = Cl(x), volume = Vo(x))
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$chartOBV <- function(x, ..., on, legend) {
xdata <- try.xts(x$Env$xdata, error=FALSE)
xsubset <- x$Env$xsubset
vo <- x$Env$vo
obv <- OBV(price = Cl(xdata), volume = vo)[xsubset]
spacing <- x$Env$theme$spacing
x.pos <- 1 + spacing * (1:NROW(obv) - 1)
xlim <- x$Env$xlim
ylim <- range(obv, na.rm=TRUE) * 1.05
theme <- x$Env$theme

lines(x.pos, obv, col = 4, lwd = 1, lend = 2, ...)

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

lchob <- current.chob()
x <- try.xts(lchob$Env$xdata, error=FALSE)
xsubset <- lchob$Env$xsubset
vo <- lchob$Env$vo
obv <- OBV(price = Cl(x), volume = vo)[xsubset]
lchob$Env$obv <- obv
if(is.na(on)) {
lchob$add_frame(ylim=range(obv, 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
}