5.5 Momentum
An n-day momentum at day t equals Mt(n)=Pt−Pt−n The following function implements the above formula:
function (price,n){
mymom <- rep(0,n)
mom <- nrow(price)
N <- Lag(price,n)
Lprice <-for (i in (n+1):N){
-Lprice[i]
mom[i]<-price[i]
} reclass(mom,price)
mom <-return(mom)
}
We calculate 2-day momentum based on closing price of AAPL.
mymom(Cl(AAPL), n=2)
M <-head (M,n=5)
## [,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