Compute Returns of an xts multi-column object

What is the most direct way to calculate object returns (nxm) xts?

When I pass the (nxm) xts mxts mxts to the quantmod dailyReturn function, the return value is a (nx 1) vector representing the return of the first column. I am looking for a way to create an (nxm) xts object containing the corresponding return vector for each mxts column.

I tried to work with some of the applicable functions, such as

 lapply(mxts,dailyReturn) 

but the return values ​​always had the wrong type and lost their mark ( dailyReturn changes the value of the vector colnames to "daily.returns").

Is there a simple, non-hacker way to achieve this? Perhaps I am using the wrong function for this problem?

+4
source share
1 answer

The ROC function in the TTR package will do this, but you can easily do the calculation yourself using lag (this is what ROC does internally).

 R> require(quantmod) # loads TTR R> getSymbols("SPY") R> head(ROC(OHLC(SPY))) SPY.Open SPY.High SPY.Low SPY.Close 2007-01-03 NA NA NA NA 2007-01-04 -0.0071963059 -5.686021e-03 0.0002845153 0.0021198425 2007-01-05 0.0007078143 -4.586355e-03 -0.0016370693 -0.0080082636 2007-01-08 -0.0036151023 7.071886e-05 -0.0009264869 0.0046143553 2007-01-09 0.0034735795 1.342709e-03 0.0010689472 -0.0008502799 2007-01-10 -0.0051793368 -2.118869e-04 -0.0007125045 0.0033261416 
+3
source

Source: https://habr.com/ru/post/1447853/


All Articles