Hello.

below is an example which shows a problem when barChart with complex  TA
 argument is called from a function chartingfoo()

while when called from the base environment the same  call to barChart is
fine.

please note  calling  barChart()  from within the  function chartingbar()
 is ok as well.

It appears   that  TA argument from chartSeries  does not see arguments
which are passed within it i.e. in this case  xts object indicator


Rationale:   I wish to make a package  which contains some S4 Objects  and
i would like to put charting functions inside S4 methods.

The current workaround   is to  externalize  charting code in separate R
files and requiring user to  source them at session start.


Any comments will be appreciated.

Regards.

Sviatoslav

##### tryBarchartfunc.R#####

library(quantmod)

## charting 1-dimensional precalculated  indicator serie
`addCalcSerie` <-
function (serie1d , ...,  onPricePane=FALSE, legend = "auto", label="custom
indicator", icol='yellow')
{
    lchob <- quantmod:::get.current.chob()

    x <- serie1d
    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 (onPricePane) on=TRUE else on =NA

    if (any(is.na(on))) {
        chobTA@new <- TRUE
    }
    else {
        chobTA@new <- FALSE
        chobTA@on <- on
    }
    chobTA@call <- match.call()

    legend.name <- label
    gpars <- c(list(...), list(col = icol))[unique(names(c(list(n = 10,
        col = icol), 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 <- quantmod:::chartSeries.chob
        do.call("chartSeries.chob", list(lchob))
        invisible(chobTA)
    }
    else {
        return(chobTA)
    }
}

ddonchianIndic <-function(ohlc, n=5)
{

   index(ohlc) <- as.POSIXct(index(ohlc), format="%Y-%m-%d")

   maxmin <- cbind( runMin(Lo(ohlc), n=n), runMax(Hi(ohlc),n=n) )
   lmaxmin <- lag(maxmin,1)
   colnames(lmaxmin) <- c('min','max')
   return(lmaxmin)
}


chartingfoo <-function( ohcl, indicator, subset="last 60 days" )
{

barChart(ohcl
         ,TA='addCalcSerie(indicator[,1],onPricePane=T,label="donch min",
icol="green");
                   addCalcSerie(indicator[,2],onPricePane=T,label="donch
max", icol="red");'
         ,subset=subset)

  return( invisible() )
}

chartingbar <-function( ohcl,  subset="last 60 days" )
{

barChart(ohcl
         ,TA='addVo()'
         ,subset=subset)

  return( invisible() )
}



############ base environment  #########

loadSymbols('GOOG')

gdonch <- ddonchianIndic(GOOG)

barChart(GOOG
         ,TA='addCalcSerie(gdonch[,1],onPricePane=T,label="donch min",
icol="green");
                   addCalcSerie(gdonch[,2],onPricePane=T,label="donch max",
icol="red");'
         ,subset="last 90 days")


dev.new()
chartingbar(GOOG )

chartingfoo(GOOG, gdonch, subset="last 90 days" )


#######################################
##### tryBarchartfunc.R end#####

	[[alternative HTML version deleted]]

