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

\[ROC_t(K)=\frac{P_t-P_{t-K}}{P_{t-K}}\] For continuous type, we have \[ROC_t(K)=log(P_t)-log(P_{t-K})\]

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.012188866
## 2012-12-27 -0.009823658
## 2012-12-28 -0.006647189