chart with individual signals

[This article was first published on Copula, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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){
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

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 their blog: Copula. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)