Hi,

Here is my first attempt, modeled after add_SMA:
I get the error message

Error in xy.coords(x, y) : 'x' and 'y' lengths differ

Any ideas? Also, what is "on=" argument do?
I notice that the underlying function stoch() is called multiple times.
Unless there is a caching mechanism, it appears that there is unnecessary
computation. Am I mistaken? Thanks.

     Gordon


#-----------------------------------------------------------------------------
add_stoch = function (nFastK = 10, nFastD=3, nSlowD=3, maType, bounded=T,
smooth=1, on = 1, ...)
{
    lenv <- new.env()
    lenv$add_stoch <- function(x, n, ...) {
        xdata <- x$Env$xdata
        xsubset <- x$Env$xsubset
        sto <- stoch(Cl(xdata),
nFastK=nFastK,nFastD=nFastD,nSlowD=nSlowD,maType=SMA, bounded=bounded,
smooth=smooth)[xsubset]
        lines(1:NROW(xdata[xsubset]), sto, ...)
    }
    mapply(function(name, value) {
        assign(name, value, envir = lenv)
    }, names(list(nFastK=nFastK,nFastD=nFastD,nSlowD=nSlowD,maType=SMA,
bounded=bounded, smooth=smooth, ...)),
list(nFastK=nFastK,nFastD=nFastD,nSlowD=nSlowD,maType=SMA, bounded=bounded,
smooth=smooth, ...))
    exp <- parse(text = gsub("list", "add_stoch",
as.expression(substitute(list(x = current.chob(),
        nFastK=nFastK,nFastD=nFastD,nSlowD=nSlowD,maType=SMA,
bounded=bounded, smooth=smooth, ...)))), srcfile = NULL)
    plot_object <- current.chob()
    lenv$xdata <-
stoch(Cl(plot_object$Env$xdata),nFastK=nFastK,nFastD=nFastD,nSlowD=nSlowD,maType=SMA,
bounded=bounded, smooth=smooth)[,2]
    print(ls(lenv))
    print(ls(plot_object$Env))
    print(exp)
    plot_object$set_frame(sign(on) * abs(on) + 1L)
    plot_object$add(exp, env = c(lenv, plot_object$Env), expr = TRUE)
    print(names(plot_object))
    plot_object
}

        [[alternative HTML version deleted]]

_______________________________________________
R-SIG-Finance@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should 
go.

Reply via email to