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.
(I'm just copying in the answer from R-sig-finance, by Stergios Marinopoulos) Use the new chart_Series() function, along with text and segments.
require(quantmod)
getSymbols("SPY")
chart_Series(SPY, subset="2011-08::", type = "candlesticks" )
text(9, 112.00, "SOME TEXT", adj=0);
segments(9, 111.5, 12, 111.5) ;
Some additional commentary by me. To add a message: text(x,y,"message")
where x is the number of the bar (1 for the leftmost bar; you can use 0 or negative to draw off the left side), and y is the value in the chart. For adj, 0 means left-align, 1 means right-align, 0.5 means centre it. Outside the 0..1 range shifts it over accordingly (but perhaps unwise to rely on that).
segments(x1,y1,x2,y2) draws a line from (x1,y1) to (x2,y2), where again x is a bar index and y is a price.
The following draws an isosceles triangle, in 20% opaque red: polygon( c(20,30,40), c(5290,5320,5290), col="#ff000033")
I.e. all the R graphics functions are available; but you must use chart_Series().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With