0

I am relatively new to R and have read as much as I have been able to on the topic but can't seem to find what I am looking for on the other questions.

I am looking to calculate the rate of change (momentum) using TTR and ROC for 12 periods, using monthly data but I would like to ignore the most recent month. In other words I am looking to find the ROC for t-2 till t-12 (January 2016 12 month momentum of a stock excluding January 2016). This is the norm in calculating momentum in portfolio construction literature.

My data is all stocks that have been listed on the South African stock exchange (JSE). The date header is the 1st column (i.e. the date is the variable in the rows) and the stocks are listed in the subsequent columns.

enter image description here

I know my code below is pretty straightforward however I have tried a few things and they have given errors. As I have about 250 stocks (columns) over 20 years, its not advisable to create a new lagged variable for each observation.

x <- Prices.df 
x$DATE <- as.Date(x$DATE, format = "%Y/%m/%d") 
y <- xts(x[,-1], order.by = x$DATE) library(TTR) 
roc <- ROC(y, n = 12, type = "discrete")

Any help would be much appreciated.

Robert
  • 5,038
  • 1
  • 25
  • 43
  • Please provide your data in a reproducible format such as `dput` http://stackoverflow.com/help/mcve – Hack-R Aug 07 '16 at 16:49
  • @Hack-R Sorry I'm not entirely sure what you're asking - I'm a novice, so I've attached what I think you want. `x <- Prices.df` `x$DATE <- as.Date(x$DATE, format = "%Y/%m/%d")` `y <- xts(x[,-1], order.by = x$DATE)` `library(TTR)` `roc <- ROC(y, n = 12, type = "discrete")` – Nicholas Olds Aug 07 '16 at 17:07

1 Answers1

0

Just use lag of roc. The following code works with simulated data (18 periods and 2 assets):

set.seed(123) # to reproduze the same results
x <- data.frame(matrix(rnorm(18*2,100,2),ncol=2)) 
x$DATE <- seq.Date(as.Date("2000/01/01"),length.out = 18,by="1 month") 
x <- x[,c(3,1,2)]
library(TTR) 
library(xts)
y <- xts(x[,-1], order.by = x$DATE) 

roc <- ROC(y, n = 12, type = "discrete")

cbind(y,lag(roc))

                  X1        X2         X1.1         X2.1
1999-12-31  98.87905 101.40271           NA           NA
2000-01-31  99.53965  99.05442           NA           NA
2000-02-29 103.11742  97.86435           NA           NA
2000-03-31 100.14102  99.56405           NA           NA
2000-04-30 100.25858  97.94799           NA           NA
2000-05-31 103.43013  98.54222           NA           NA
2000-06-30 100.92183  98.74992           NA           NA
2000-07-31  97.46988  96.62661           NA           NA
2000-08-31  98.62629 101.67557           NA           NA
2000-09-30  99.10868 100.30675           NA           NA
2000-10-31 102.44816  97.72373           NA           NA
2000-11-30 100.71963 102.50763           NA           NA
2000-12-31 100.80154 100.85293           NA           NA
2001-01-31 100.22137  99.40986  0.019442887 -0.005421782
2001-02-28  98.88832 101.79025  0.006848733  0.003588329
2001-03-31 103.57383 101.75627 -0.041012460  0.040115718
2001-04-30 100.99570 101.64316  0.034279755  0.022018156
2001-05-31  96.06677 101.37728  0.007352244  0.037725848
Robert
  • 5,038
  • 1
  • 25
  • 43