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

*Related*

To

**leave a comment** for the author, please follow the link and comment on their blog:

** Copula**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as:

Data science,

Big Data, R jobs, visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...