[R-SIG-Finance] quantmod addTA() How to compute his own indicator ?

Jeff Ryan jeff.a.ryan at gmail.com
Mon Jul 7 22:35:31 CEST 2008

Bonjour Pierre,

The site now has some examples with respect to the new addTA and newTA


Apart from that the basic idea is that addTA will take the result of a
function call, and apply it to the active chart, whereas newTA will
let you create an indicator much like the built-in ones.

A quick example _not_ on the site using Guppy Multiple Moving Averages
(soon to be added to TTR or quantmod):

library(TTR) # for the EMA function - make sure to use the TTR version
and not the fTrading version, as TTR prepends NAs


 `GMMA` <- function(x, short=c(3,5,8,10,12,15),
long=c(30,35,40,45,50,60), type='EMA') {
    gmma <- sapply(c(short,long), function(g) do.call(type, list(x,n=g)))
    colnames(gmma) <- c(paste('short lag',short),paste('long lag',long))

  addGMMA <- newTA(GMMA, preFUN=Cl,
legend.name='GMMA',col=c(rep(c('green','red'), each=6)))


  # alternately you can use addTA for one-off chart additions:


In future additions there will be more control over titles, though for
the most part this works well even now.

addTA requires either a vector or matrix with NROW == NROW of the
charted series, OR alternately it can be an
xts object within the range of the original series.  Most graphical
parameters are handled by both functions, as well as the ability to
automatically recycle args and plot multiple lines.  The "on" argument
lets you add an overlay to the main window by specifying "on=1".
Eventually all windows will allow for this.

# add an overlay
addTA(EMA(Cl(YHOO)), on=1, col='yellow')

Hope that helps,

On Mon, Jul 7, 2008 at 2:36 PM, Pierre8r <pierre8r-gmane at yahoo.fr> wrote:
> Hello,
> How to compute his own indicator ?
> I want to use addTA() from quantmod with my own indicator.
> How to compute my own indicator ?
> Please give me some full R code sample.
> The sample code can be some Moving Average sample code or other I don't care.
> For example Close + 1
> I just need something I can start from.
> Thanks,
> Pierre8r
> Here some Java code to compute Moving Average.
> But the sample code can be simpler.
> Java code :
>    public double calculate() {
>        int endBar = qh.size() - 1;
>        int startBar = endBar - length;
>        double sma = 0;
>        for (int bar = startBar; bar <= endBar; bar++) {
>            sma += qh.getPriceBar(bar).getClose();
>        }
>        value = sma / (endBar - startBar + 1);
>        return value;
>    }
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.

Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics

More information about the R-SIG-Finance mailing list