I would like to produce a plot like this https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110826/19da3834/attachment.png using quantmod.
I am a bit frustrated with, I guess, a very simple task. I would like to be able to draw a line on the chart using quantmod. After several days of trying I am getting nowhere. I can not see an example on how to do that with quantmod::addLines function (this is the only similar question I found but could not find an answer on it http://r.789695.n4.nabble.com/quantmod-plot-trendline-td894632.html)
My problem is this: I would like to plot a horizontal line at specified date/time/bar nad y value. This line is only allowed to be n (for example 5) bars long starting at some specific bar (additionally I would also like to add the text just above the line of the y value specified).
I have tried several things:
getSymbols("SPY")
lines.SPY <- (Hi(SPY) + Lo(SPY))/2
names(lines.SPY) <- c("lines")
lines.SPY$BuySell <- ifelse(lag(lines.SPY$lines) > lines.SPY$lines, 1, -1)
chartSeries(SPY, subset="2011-08::", theme=chartTheme('white',
up.col='blue', dn.col='red'))
addTA(lines.SPY$lines[lines.SPY$BuySell == -1,], type='p', col='darkred', pch="_", on=1, cex = 2.5)
addTA(lines.SPY$lines[lines.SPY$BuySell == 1,], type='p', col='green4', pch="_", on=1, cex = 2.5)
But this are not actually lines... And I do not know ow to add text...
Then I have tried this
getSymbols("SPY")
subset = "2011-03::"
dev.new()
chartSeries(SPY, subset=subset, theme="white")
test <- xts(rep(coredata(last(Cl(SPY))), 20), order.by=index(last(SPY, n=20)))
addTA(test, on=1, col="red", legend=NULL, lwd=3)
Again, adding text is not possible. The other problem with this approach is that I can not get rid off the legend at the top. Since I want to draw tens or hundreds of those lines on one chart legend should not be displayed...
Thanks in advance for your ideas/code examples/...
Best regards, Samo.