Skip to content

Commit 068ce81

Browse files
committed
Refactor addCLV to follow skeleton_TA
Refactor addCLV to use skeleton_TA structure. chartCLV function is given to create close location value indicator based on skeleton_TA structure.
1 parent 3f56737 commit 068ce81

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

R/addCLV.R

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,66 @@
77
`addCLV` <-
88
function (..., on = NA, legend = "auto")
99
{
10-
lchob <- get.current.chob()
11-
x <- as.matrix(lchob@xdata)
12-
x <- HLC(x)
13-
x <- CLV(HLC = x)
14-
yrange <- NULL
15-
chobTA <- new("chobTA")
16-
if (NCOL(x) == 1) {
17-
chobTA@TA.values <- x[lchob@xsubset]
10+
lenv <- new.env()
11+
lenv$chartCLV <- function(x, ..., on, legend) {
12+
xdata <- x$Env$xdata
13+
xsubset <- x$Env$xsubset
14+
clv <- CLV(HLC=HLC(xdata))[xsubset]
15+
spacing <- x$Env$theme$spacing
16+
x.pos <- 1 + spacing * (1:NROW(clv) - 1)
17+
xlim <- x$Env$xlim
18+
ylim <- range(clv,na.rm=TRUE)
19+
theme <- x$Env$theme
20+
21+
lines(x.pos, clv, type = "h", col = theme$clv$col,
22+
lwd = 1, lend = 2, ...)
1823
}
19-
else chobTA@TA.values <- x[lchob@xsubset, ]
20-
chobTA@name <- "chartTA"
21-
if (any(is.na(on))) {
22-
chobTA@new <- TRUE
24+
if(!is.character(legend) || legend == "auto")
25+
legend <- gsub("^addCLV", "Close Location Value", deparse(match.call()))
26+
mapply(function(name, value) {
27+
assign(name, value, envir = lenv)
28+
}, names(list(..., on = on, legend = legend)),
29+
list(..., on = on, legend = legend))
30+
exp <- parse(text = gsub("list", "chartCLV", as.expression(substitute(list(x = current.chob(),
31+
..., on = on, legend = legend)))), srcfile = NULL)
32+
exp <- c(exp, expression(
33+
lc <- xts:::legend.coords("topleft", xlim, range(clv,na.rm=TRUE)),
34+
legend(x = lc$x, y = lc$y,
35+
legend = c(paste(legend, ":"),
36+
paste(format(last(clv),nsmall = 3L))),
37+
text.col = c(theme$fg, 5),
38+
xjust = lc$xjust,
39+
yjust = lc$yjust,
40+
bty = "n",
41+
y.intersp=0.95)))
42+
exp <- c(expression(
43+
# add inbox color
44+
rect(xlim[1], range(clv, na.rm=TRUE)[1], xlim[2], range(clv, na.rm=TRUE)[2], col=theme$fill),
45+
# add grid lines and left-side axis labels
46+
segments(xlim[1], y_grid_lines(range(clv, na.rm=TRUE)),
47+
xlim[2], y_grid_lines(range(clv, na.rm=TRUE)),
48+
col = theme$grid, lwd = x$Env$grid.ticks.lwd, lty = 3),
49+
text(xlim[1], y_grid_lines(range(clv, na.rm=TRUE)), y_grid_lines(range(clv, na.rm=TRUE)),
50+
col = theme$labels, srt = theme$srt,
51+
offset = 0.5, pos = 2, cex = theme$cex.axis, xpd = TRUE),
52+
# add border of plotting area
53+
rect(xlim[1], range(clv, na.rm=TRUE)[1], xlim[2], range(clv, na.rm=TRUE)[2], border=theme$labels)), exp)
54+
55+
lchob <- current.chob()
56+
if (is.null(lchob$Env$theme$clv$col)) {
57+
lchob$Env$theme$clv$col <- 5
58+
}
59+
xdata <- lchob$Env$xdata
60+
xsubset <- lchob$Env$xsubset
61+
clv <- CLV(HLC=HLC(xdata))[xsubset]
62+
lchob$Env$clv <- clv
63+
if(is.na(on)) {
64+
lchob$add_frame(ylim=range(clv,na.rm=TRUE),asp=1,fixed=TRUE)
65+
lchob$next_frame()
2366
}
2467
else {
25-
chobTA@new <- FALSE
26-
chobTA@on <- on
68+
lchob$set_frame(sign(on)*abs(on))
2769
}
28-
chobTA@call <- match.call()
29-
legend.name <- gsub("^.*[(]", " Close Location Value (",
30-
deparse(match.call()))#, extended = TRUE)
31-
gpars <- c(list(...), list(col=5, type = "h"))[unique(names(c(list(col=5, type = "h"),
32-
list(...))))]
33-
chobTA@params <- list(xrange = lchob@xrange, yrange = yrange,
34-
colors = lchob@colors, color.vol = lchob@color.vol, multi.col = lchob@multi.col,
35-
spacing = lchob@spacing, width = lchob@width, bp = lchob@bp,
36-
x.labels = lchob@x.labels, time.scale = lchob@time.scale,
37-
isLogical = is.logical(x), legend = legend, legend.name = legend.name,
38-
pars = list(gpars))
39-
# if (is.null(sys.call(-1))) {
40-
# TA <- lchob@passed.args$TA
41-
# lchob@passed.args$TA <- c(TA, chobTA)
42-
# lchob@windows <- lchob@windows + ifelse(chobTA@new, 1,
43-
# 0)
44-
# chartSeries.chob <- quantmod:::chartSeries.chob
45-
# do.call("chartSeries.chob", list(lchob))
46-
# invisible(chobTA)
47-
# }
48-
# else {
49-
return(chobTA)
50-
# }
70+
lchob$replot(exp, env=c(lenv,lchob$Env), expr=TRUE)
71+
lchob
5172
}

0 commit comments

Comments
 (0)