1

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.

opt
  • 477
  • 1
  • 10
  • 25
  • what about applying a vector to width? now the 252 is being recycled. – J.R. Sep 17 '14 at 20:44
  • Hi J.R. do you mean that I can pass a vector like [100:1:N] where the width starts at 100 (minimun amount of data that is needed) and increases to N (number of observations) by step 1? The idea seems very good but need to try. In particular, I don't know if rollapply accepts a vector of widths. Any experience with this? But thanks for the suggestion. – opt Sep 17 '14 at 21:02
  • No sry. I don't have any experience with rollaplly(), but the documentation says it accepts a vector or list, thereby my suggestion. – J.R. Sep 17 '14 at 22:23
  • Thanks. I'll try tomorrow and let you know. – opt Sep 17 '14 at 22:28

0 Answers0