1
library(quantmod)
getSymbols("SPY", from="2013-01-01", to=Sys.Date())
chartSeries(SPY, TA="addSMA(20)")

Is there a way of shifting a moving average to the left and right?

adam.888
  • 7,686
  • 17
  • 70
  • 105
  • try maybe `s <- lag(SMA(SPY,20),10); chart_series(SPY); add_TA(s);` – haki Sep 29 '13 at 05:21
  • Error in `index<-.xts`(`*tmp*`, value = c(1357084800, 1357171200, 1357257600, : unsupported ‘index’ index type of class ‘POSIXct’ Thanks got error above – adam.888 Sep 29 '13 at 09:45

1 Answers1

4

lag is key here

s <- get(getSymbols('SPY'))
sma <- SMA(Cl(s),20)

chart_Series(s ,subset="2013::")
add_TA(sma , on = 1)
add_TA(lag(sma,10) , on = 1 , col ='red')
add_TA(lag(sma,-10) , on = 1 , col = 'blue')

outcome enter image description here

haki
  • 9,389
  • 15
  • 62
  • 110
  • 1
    Error in `index<-.xts`(`*tmp*`, value = c(1357084800, 1357171200, 1357257600, : unsupported ‘index’ index type of class ‘POSIXct’ Thanks,I keep getting these errors and I do not know why – adam.888 Sep 29 '13 at 11:31
  • 1
    post your full code. it looks like your not using a valid xts object or something. – haki Sep 29 '13 at 12:08
  • I just used the code above in your post (including library(quantmod) at the top). I think the POSIXct error means that it is not recognizing the dates but I am not really sure. – adam.888 Sep 29 '13 at 19:08
  • I'm not sure but it might be a timezone issue. checkout this [post](http://stackoverflow.com/questions/15640133/coercing-a-posixct-object-to-date-object/15640388#15640388) - you might find it help full. – haki Sep 30 '13 at 05:22