I have daily data for S&P500 and store the close-to-close return in my_data$Return. My goal is to refit a GARCH(1,1) each day in the period (so starting from the startDate which is 01/01/2004) and then calculate a 30 days forecast. In other words, I need to simulate what would have been the GARCH(1, 1) 30 days forecast at each point in time in the period I am using. In order to do that, I have the function myFit that calibrates the model and does the forecast. This function is then called by apply.fromstart so that I can apply that every day from startDate onwards.
If I launch this, R complains because ugarchfit requires at least 100 data points to run.
library(quantmod)
library(PerformanceAnalytics)
library(rugarch)
symbolLst <- c("^GSPC","^VIX")
startDate = as.Date("2004-01-01")
myForHorizon = 30
myData <- new.env()
getSymbols(symbolLst, env = myData, src = "yahoo", from = startDate)
args = eapply(myData,
function(x){OHLC(x)})
my_data = do.call(cbind,
args,
envir = myData)
my_data$Return <- ClCl(myData$GSPC)
my_data$Return[is.na(my_data$Return)] <- 0
spec = ugarchspec(
variance.model=list(garchOrder=c(1,1)))
myFit <- function(myVector)
{
myVec <- myVector
fit = ugarchfit(spec=spec, data=myVec)
forecast = ugarchforecast(fit, n.ahead=myForHorizon)
myForecast = sigma(forecast)[myForHorizon,] * sqrt(252) * 100
return(myForecast)
}
forecastVec <- apply.fromstart(my_data$Return, FUN=myFit)
I see that the function requires at least 100 data but how can I change the function to avoid the error? I have also tried to replace the last line with:
forecastVec <- rollapply(my_data$Return, width = 252, FUN=myFit)
but R shows an error since the solver fails to converge. Plus, rollapply doesn't let me work with an expanding window, which is what I would like to do instead. Infact, I am applying on a 1 year rolling window here.
Any idea about how to get the forecasts every day in the period?
Thanks a lot for the help.