(This article was first published on

**Copula**, and kindly contributed to R-bloggers)Also I'm not to much into Technical Indicators and Chart-Analysis during system development it is sometimes handy to visualize your buy and selllimits in a chart.

The quantmod package provides a nice charting environment and you can select from a bunch of predefined indicators.

However what I would like to have is to display my buy and selllimits in my chart.

This can be achieved using addTA but before we can display the TA we need to write some code.

Just as example I'm using some indicators I have added to the yahoo OHLC Data

for(symbol in portfAsymbols){

x=get(symbol)

tmpATR <- ATR(x[,c(2,3,4)],atrvalue,maType=EMA,wilder=TRUE)

tmpADX <- ADX(x[,c(2,3,4)],n=14,maType=EMA,wilder=TRUE)

tmpSAR <- SAR(x[,c(2,3)],accel=c(0.02,0.2))

tmpBB <-BBands(x[,c(2,3,4)],n=20,sd=2,maType="SMA")

x$atrind <- tmpATR$atr

x$adxind <-tmpADX$ADX

x$sar<-tmpSAR

assign(symbol,x)

}

Now I'm defining my buylimit by adding or subtracting the ATR to yesterdays CLOSE.

atrpos=atrvalue + 2

buy = matrix (nrow = n, ncol = 1)

sell = matrix (nrow = n, ncol = 1)

for( i in atrpos:n ) {

buy[i,]=as.numeric(Cl(x[i-1,]))+ as.numeric(round(x$atrind[i-1,],2))

sell[i,]=as.numeric(Cl(x[i-1,]))- as.numeric(round(x$atrind[i-1,],2))

}

And finally we can now display this buy and selllimit on the chart.

addTA (as.xts(sell,as.Date(index(x))),on=1,col=7)

addTA (as.xts(buy,as.Date(index(x))),on=1,col=4)

here is the result

To

**leave a comment**for the author, please follow the link and comment on his blog:**Copula**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...