13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

142

We fit an ARIMA model by looping over the values of p, d and q:

> spfinal.aic <- Inf

> spfinal.order <- c(0,0,0)

> for (p in 1:4) for (d in 0:1) for (q in 1:4) {

> spcurrent.aic <- AIC(arima(sp, order=c(p, d, q)))

> if (spcurrent.aic < spfinal.aic) {

> spfinal.aic <- spcurrent.aic

> spfinal.order <- c(p, d, q)

> spfinal.arima <- arima(sp, order=spfinal.order)

> }

> }

The AIC tells us that the "best" model is the ARIMA(2,0,1) model. Notice once again that

d = 0, as we have already taken first order differences of the series:

> spfinal.order

[1] 2 0 1

We can plot the residuals of the fitted model to see if we have evidence of discrete white

noise, see Figure 11.5.

> acf(resid(spfinal.arima), na.action=na.omit)

Figure 11.5: Correlogram of residuals of ARIMA(2,0,1) model fitted to S&P500 daily log returns

The correlogram looks promising, so the next step is to run the Ljung-Box test and confirm

that we have a good model fit:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!