I’m trying to add a variable that keeps track of the all-time high of a time series. The below doesn’t work – R encounters a fatal error during the for loop. I don’t quite understand what’s wrong here.
library(quantmod)
GSPC_df <- getSymbols("^GSPC", src = "yahoo", auto.assign = FALSE,
from = "1927-12-30")
GSPC_df$GSPC.ATH <- GSPC_df$GSPC.Close
for (i in (2:nrow(GSPC_df))){
GSPC_df$GSPC.ATH[i] <- max(GSPC_df$GSPC.Close[i], GSPC_df$GSPC.ATH[i - 1])
}