I am attempting to calculate the realized volatility of the members of the S&P 500 over a specific interval. I am having trouble looping through the index and storing the values.
The process should be to calculate the volatility of each name and then store it within a data frame. Formatted "Ticker" and "Volatility"
I have been using the below code to calculate vol
library(tseries)
start_date <- as.Date("2019-04-23")
end_date <- as.Date("2020-01-22")
SP_500 <- data.frame(read.csv("Tickers.csv", header = TRUE))
data <- get.hist.quote('TIF',start_date, end_date, quote = c("Close"))
price <- data$Close
ret <- log(lag(price)) - log(price)
ret[is.na(ret)]<-0
vol <- sd(ret) * sqrt(252) * 100
vol
I have tried a million different attempts to looping and storing but all failed.. Thanks for the help in advance!