I am using quantmod in R to get price history for the ‘QQQ’ ETF. I want to calculate the 1:n period rate of change.
I am able to do it using ROC() and can successfully create and name the columns of the xts. I just see it as time intensive – I know there is a better way to do this.
library(quantmod) getSymbols("QQQ") QQQ <- QQQ$QQQ.Close View(QQQ) QQQ <- `colnames<-`(QQQ,"close") QQQ$ROC1 <- ROC(x = QQQ[,1],n = 1) QQQ$ROC2 <- ROC(x = QQQ[,1], n = 2) QQQ$ROC3 <- ROC(x = QQQ[,1], n = 3)
I expect that I will have ‘n’ number of columns 1:n with respective ROC calculations. I understand there will be ‘n’ number of N/A values per column.