Skip to content

Instantly share code, notes, and snippets.

@cordmaur

cordmaur/ARIMA.r Secret

Created January 8, 2021 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cordmaur/a46b929268028dd70848030048c2bd7b to your computer and use it in GitHub Desktop.
Save cordmaur/a46b929268028dd70848030048c2bd7b to your computer and use it in GitHub Desktop.
install.packages("quantmod")
install.packages("lattice")
install.packages("timeSeries")
install.packages("rugarch")
library(quantmod)
library(lattice)
library(timeSeries)
library(rugarch)
# Added
library(xts)
getSymbols("^GSPC", from="1950-01-01")
spReturns = diff(log(Cl(GSPC)))
spReturns[as.character(head(index(Cl(GSPC)),1))] = 0
windowLength = 500
foreLength = length(spReturns) - windowLength
forecasts <- vector(mode="character", length=foreLength)
ini = 0
for (d in ini:foreLength) {
# Obtain the S&P500 rolling window for this day
spReturnsOffset = spReturns[(1+d):(windowLength+d)]
# Fit the ARIMA model
final.aic <- Inf
final.order <- c(0,0,0)
for (p in 0:5) for (q in 0:5) {
if ( p == 0 && q == 0) {
next
}
arimaFit = tryCatch( arima(spReturnsOffset, order=c(p, 0, q)),
error=function( err ) {
message(err)
return(FALSE)
},
warning=function( err ) {
# message(err)
return(FALSE)
} )
if( !is.logical( arimaFit ) ) {
current.aic <- AIC(arimaFit)
if (current.aic < final.aic) {
final.aic <- current.aic
final.order <- c(p, 0, q)
# final.arima <- arima(spReturnsOffset, order=final.order)
final.arima <- arimaFit
}
} else {
next
}
}
# test for the case we have not achieved a solution
if (final.order[1]==0 && final.order[3]==0) {
final.order[1] = 1
final.order[3] = 1
}
# Specify and fit the GARCH model
spec = ugarchspec(
variance.model=list(garchOrder=c(1,1)),
mean.model=list(armaOrder=c(final.order[1], final.order[3]), include.mean=T),
distribution.model="sged"
)
fit = tryCatch(
ugarchfit(
spec, spReturnsOffset, solver = 'hybrid'
), error=function(e) e, warning=function(w) w
)
# If the GARCH model does not converge, set the direction to "long" else
# choose the correct forecast direction based on the returns prediction
# Output the results to the screen and the forecasts vector
if(is(fit, "warning")) {
forecasts[d+1] = paste(index(spReturnsOffset[windowLength]), 1, sep=",")
print(paste(index(spReturnsOffset[windowLength]), 1, sep=","))
} else {
fore = ugarchforecast(fit, n.ahead=1)
ind = fore@forecast$seriesFor
forecasts[d+1] = paste(colnames(ind), ifelse(ind[1] < 0, -1, 1), sep=",")
print(paste(colnames(ind), ifelse(ind[1] < 0, -1, 1), sep=","))
}
}
write.csv(forecasts, file="/forecasts_test.csv", row.names=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment