Search code examples
rquantmod

Extending a line 20 days into future on chartSeries plot


I have added a line to a chart using quantmod. But how can I make it extend out into the future for the next 20 days?

library(quantmod)
getSymbols("SPY", from="2013-01-01", to="2013-09-28")
chartSeries(SPY, TA="addLines(h=c(max(SPY[,c(1:4)])))")

Also how can I add its value onto the secondary y axis?

I also tried:

 chart_Series(SPY, subset="2013::", type = "candlesticks" ) 
 segments(1, 150, 800, 150)

But I could not change the x axis forward.


Solution

  • Put the value you want to extend into another object, then merge the object you want to chart with the index of the other object. This will create rows full of NA for the future days.

    futureLine <- xts(,end(SPY)+1:20)  # create empty object with days we want
    futureLine$max <- max(Hi(SPY))     # fill in data
    chart_Series(merge(SPY,index(futureLine)), TA="add_TA(futureLine, on=1)")
    

    The regular chartSeries doesn't like this approach, as it seems to remove all the rows that only contain NA.