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.
## [,1]
## 2003-01-02 0.0000000000
## 2003-01-03 0.0000000000
## 2003-01-06 0.0008846439
## 2003-01-07 -0.0004420741
## 2003-01-08 -0.0030974926