Search code examples
rquantmod

Add vertical line in background in quantmod chart


How can I add vertical line to chart in quantmod that appears in the background? Consider this example:

library(quantmod)
symbol <- "AAPL"
cache <- new.env()
getSymbols(symbol, env=cache)
chartSeries(cache$AAPL, subset="last 3 months")
plot(addLines(v=10)) # Adds vertical line at tick 10.

The problem is that adding the vertical line at tick 10 now hides the wicks from the candlestick:

Hidden candlestick wicks

I also tried the function addVLine from qmao. It effectively does this:

c <- quantmod:::get.current.chob()
i <- index(c@xdata[endpoints(c@xdata, "months")])
plot(addTA(xts(rep(TRUE, length(i)), i), on=-1, col="grey"))

The result looks like this:

enter image description here

While I have the lines in the background now, they are super wide and pretty obtrusive. I just want them in the background in the same way the grid lines already exist there. How can I achieve this?

Note: this question resembles an existing one, but here I am asking on how to render the vertical line in the background.


Solution

  • I didn't look at the source code to understand why this works, but it seems to do what you want. Basically, you add the addLines call via the TA argument to chartSeries. This is generally a good thing to do anyway, since it avoids re-drawing the chart for each add* call.

    chartSeries(cache$AAPL, subset="last 3 months", TA="addVo();addLines(v=10,on=-1)")
    

    enter image description here