From 8fb26c50e5914d2d17fd4c94cb63d19f04bc35a6 Mon Sep 17 00:00:00 2001 From: Eric Hung Date: Sun, 7 Aug 2016 15:18:56 +0800 Subject: [PATCH] Refactor dropTA to use panel functionality Since S4 method and chartSeries.chob are deprecated, dropTA, listTA and get.chob functions are refactored to use panel functionality of xts::plot.xts. The way TAs are removed is based on which frame they are added to. --- R/TA.R | 8 +++++--- R/chob.R | 2 +- R/dropTA.R | 35 ++++++++++++++++++++++------------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/R/TA.R b/R/TA.R index 00c28461..6ffc451d 100644 --- a/R/TA.R +++ b/R/TA.R @@ -286,9 +286,11 @@ function(type=c('chartSeries','barChart','candleChart')) { }# }}} # listTA {{{ `listTA` <- -function(dev) { - if(missing(dev)) dev <- dev.cur() - sapply(get.chob()[[dev]]@passed.args$TA,function(x) x@call) +function(chob) { + if(missing(chob)) chob <- get.chob() + # return function calls of addTA + chob$Env$call_list[-1] + #sapply(get.chob()[[dev]]@passed.args$TA,function(x) x@call) } # }}} chartNULL <- function(...) return(invisible(NULL)) diff --git a/R/chob.R b/R/chob.R index e2db7e45..e1a126b2 100644 --- a/R/chob.R +++ b/R/chob.R @@ -27,7 +27,7 @@ function(x,pos) `get.chob` <- function() { - x <- .chob$.chob + x <- xts:::.plotxtsEnv$.xts_chob return(x) #x <- get('.chob',as.environment("package:quantmod")) #attr(x,'.Environment') <- NULL diff --git a/R/dropTA.R b/R/dropTA.R index 7c493d71..05e8fa81 100644 --- a/R/dropTA.R +++ b/R/dropTA.R @@ -61,18 +61,15 @@ function(ta,pos,occ=1,dev) { } `dropTA` <- -function(ta,occ=1,dev,all=FALSE) { +function(ta,occ=1,chob,all=FALSE) { if(all) return(do.call('dropTA', list(1:length(listTA())))) if(missing(ta)) stop("no TA indicator specified") - # default to the current device if none specified - if(missing(dev)) dev <- dev.cur() - ta.list <- listTA(dev) - # get the current chob - lchob <- get.chob()[[dev]] + if(missing(chob)) chob <- get.chob() + ta.list <- listTA(chob) sel.ta <- NULL @@ -91,22 +88,34 @@ function(ta,occ=1,dev,all=FALSE) { if(!is.na(which.ta)) { # decrease window count if necessary - if(lchob@passed.args$TA[[which.ta]]@new) - lchob@windows <- lchob@windows - 1 + #if(lchob@passed.args$TA[[which.ta]]@new) + # lchob@windows <- lchob@windows - 1 sel.ta <- c(sel.ta,which.ta) + } else { + stop("nothing to remove") } } if(is.null(sel.ta)) stop("nothing to remove") # remove TA from current list - lchob@passed.args$TA <- lchob@passed.args$TA[-sel.ta] - if(length(lchob@passed.args$TA) < 1) - lchob@passed.args$TA <- list() + ta.list <- ta.list[-sel.ta] + for(li in sel.ta) { + # number of actions of chartSeries object without TA is 9 + frame <- attr(chob$Env$actions[[9 + sel.ta]], "frame") + if(abs(frame)==2) + chob$Env$actions[[9 + sel.ta]] <- NULL + else + chob$remove_frame(frame) + chob$Env$TA[[sel.ta]] <- NULL + ncalls <- length(chob$Env$call_list) + # plot.xts(...) is included in call_list but listTA() is not + chob$Env$call_list[1 + sel.ta] <- NULL + } # redraw chart - do.call("chartSeries.chob",list(lchob)) + chob - write.chob(lchob,lchob@device) + #write.chob(lchob,lchob@device) }