Search code examples
rloopsmatrixindexingquantmod

Add column with index starting at 100 to stock prices/returns matrix


I've recently begun to use R for doing calculations with financial data, so please bear with me. I'll try to be as specific as possible.

What I'm trying to do: Using R with the quantmod package, I load financial data into a matrix and then add a column with the daily returns, as follows:

> getSymbols("^GDAXI",from="1900-01-01")
> "GDAXI"
> GDAXI$Returns<-dailyReturn(Cl(GDAXI))
> head(GDAXI)
               GDAXI.Open GDAXI.High GDAXI.Low GDAXI.Close GDAXI.Volume GDAXI.Adjusted  Returns
1990-11-26     1466.3     1466.3     1443.2    1443.2      0            1443.2          0.000000000
1990-11-27     1438.3     1438.3     1415.3    1415.3      0            1415.3         -0.019332040
1990-11-28     1410.0     1431.9     1402.8    1420.6      0            1420.6          0.003744789
1990-11-29     1420.4     1424.6     1415.8    1418.9      0            1418.9         -0.001196677
1990-11-30     1421.5     1443.9     1421.5    1441.2      0            1441.2          0.015716400
1990-12-03     1470.1     1476.6     1458.7    1462.6      0            1462.6          0.014848737

However, next I would like to add another column with an index starting at 100 for later comparisons with another time series. I have not been able to do so.

First, I tried

GDAXI$Index[1]=100
for(i in 2:nrow(GDAXI)){ GDAXI$Index[i]<-GDAXI$Index[i-1]*(1+GDAXI$Returns[i]) }

But I get this error message, which I don't understand:

Error in NextMethod(.Generic) : replacement has length zero

Then, I tried

> GDAXI$Index<-ifelse(index(GDAXI$Returns)==index(first(GDAXI)),100,lag(GDAXI$Index,1)*(1+GDAXI$Returns))

and get another error message:

Error in hasTsp(x) : attempt to set an attribute on NULL

All I want is a column with the following values (added manually as illustration):

1990-11-26  100.00
1990-11-27   98.066796
1990-11-28   98.4340255
1990-11-29   98.3162417
1990-11-30   99.8614191
1990-12-03  101.344235

Please help! Having previously coded in VBA I'm probably approaching this the wrong way. But searching the web and stackoverflow hasn't led me to the solution, yet. Thank you very much!


Solution

  • You can just divide the close price column by its first value.

    library(quantmod)
    ind <- function(x) {
      coredata(x) <- t(
        t(coredata(x)) / 
        apply(coredata(x),2,function(u){ c(u[!is.na(u)&u!=0],NA)[1] })
       )
      x
    }
    getSymbols("^GDAXI",from="1900-01-01")
    GDAXI$Index <- 100 * ind( Cl(GDAXI) )
    

    The errors in your code mainly come from the fact that xts objects are not data.frames. Each value is associated with a date, and you cannot combine elements for different dates: when you write GDAXI$Index[i-1]*(1+GDAXI$Returns[i]), the dates do not match.

    lag, as in your second attempt, changes those dates, so that the computations are meaningful. But your code is recursive: you use the value of GDAXI$Index to define itself (recursion only works for functions).