5.7 Rate of Change (ROC)

Rate of Change (ROC) with K-day momentum at day t equals to return of K days.

For discrete type, we have

ROCt(K)=PtPtKPtK For continuous type, we have ROCt(K)=log(Pt)log(PtK)

myROC <- function (price,n){
  roc <- rep(0,n)
  N <- nrow(price)
  Lprice <- Lag(price,n)
  for (i in (n+1):N){
    roc[i]<-(price[i]-Lprice[i])/Lprice[i]
  }
  roc <- reclass(roc,price)
  return(roc)
}

Now we test our code:

roc <- myROC(Cl(AAPL),n=2)
tail(roc,n=3)
##                    [,1]
## 2012-12-26 -0.012188826
## 2012-12-27 -0.009823658
## 2012-12-28 -0.006647189