5.5 Momentum

An \(n\)-day momentum at day \(t\) equals \[M_t(n)=P_t-P_{t-n}\] The following function implements the above formula:

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

We calculate 2-day momentum based on closing price of AAPL.

M <- mymom(Cl(AAPL), n=2)
head (M,n=5)
##                    [,1]
## 2003-01-02  0.000000000
## 2003-01-03  0.000000000
## 2003-01-06  0.003540063
## 2003-01-07 -0.001770279
## 2003-01-08 -0.012389971