Dr. Jones Dr. Jones - 3 months ago 31
R Question

Controlling addLines

I have an xts object (NCGSpot) I use for charting and would like to add a vertical line on a given date to the plot. Here is what I do:

chartSeries(NCGSpot, TA="addBBands();addLines()", subset="2015-04-02::2016-08-01",theme="white")


How can I control where the Lines is drawn. I have seen stuff like

addLines(v=anynumber)


But I cannot make much sense out of it and could not find any information on it.
Its there any way I pass on a date to addLines and get the line on that date?

Thx in advance

Answer

If you use chart_Series, (better charting capabilities than chartSeries) you can create vertical lines from scratch using an xts object containing a matrix type of logical, where TRUE applies to the dates where vertical lines are desired. The argument on should be set to 1 or -1 if you want the vertical lines on your main chart. Setting -1 will put the lines behind the candles. Other useful parameters included col and border (should be self explanatory). Here is an example to get you started:

library(quantmod)

getSymbols("AAPL")

xt <- xts(rep(FALSE, NROW(AAPL)), index(AAPL))
dates_for_vertical_marks <- c("2016-02-01", "2016-04-29")
xt[dates_for_vertical_marks, ] <- TRUE

xt2 <- xts(rep(FALSE, NROW(AAPL)), index(AAPL))
dates_for_vertical_marks <- c("2016-07-01")
xt2[dates_for_vertical_marks, ] <- TRUE


chart_Series(AAPL, subset="2016")
add_TA(xt, on =-1, col= "orange", border='blue')
add_TA(xt2, on = 1, col= "darkgreen", border='darkgreen')

vert lines

Comments