Question

Goal: I want to add multiple vertical lines to my chart. In this example, I want to add vertical lines for the following dates: 2012-01-09,2012-01-24, and 2012-01-31.

Issue: However, my codes adds 4 lines instead of 3 AND it adds them at the wrong dates.

Can someone tell me what I am doing wrong? Thank you!

library(quantmod)
getSymbols("SPY", from="2012-01-01", to="2013-06-15")

#add multiple vertical lines
chartSeries(SPY,TA="addLines(v=SPY[c(5,15,20)])")

enter image description here

Was it helpful?

Solution

I have a hackish way to do it in my qmao package (https://r-forge.r-project.org/R/?group_id=1113), which is based on this post to the r-sig-finance mailing list.

If you don't want to bother installing the whole package, here is the code for the function you'd need: https://r-forge.r-project.org/scm/viewvc.php/pkg/qmao/R/addVLine.R?view=markup&root=twsinstrument

chartSeries(SPY)
addVLine(index(SPY[c(5,15,20)]))

which produces:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top