Original link:tecdat.cn/?p=7207

Original source:Tuo End number according to the tribe public number

 

In this article, I want to show you how to apply a trading strategy for the S&P500 Stock Market index.

By combining the ARIMA and GARCH models, we can outperform the “buy and hold” approach over the long term.

Summary of strategy

The strategy is executed on the basis of a “rolling” forecast:

  1. For each day, the logarithmic returns of the stock index precedekDay is used as a window to fit the best ARIMA and GARCH models.
  2. The portfolio model is used to forecast the next day’s returns.
  3. Short the stock at the last close if the forecast is negative, long if the forecast is positive.
  4. If the forecast is in the same direction as the previous day, nothing changes.

 

Strategy implementation

 

 

The first task is to install and import the necessary libraries in R:

If libraries are already installed, you can simply import them:

> library(quantmod)
> library(lattice)
> library(timeSeries)
> library(rugarch)
Copy the code

When complete, this strategy will be applied to the S&P500.

We can then create a logarithmic difference series of returns for the “closing price” of the S&P 500 and remove the initial NA value:

 

According to the Akaike information criterion, the cyclic process will give us the “best” fit of the ARMA model, which we can then use for input to the GARCH model:

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 ) FALSE, > warning=function( err ) 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) > } > } else { > next > } > }Copy the code

 

If the GARCH model fails to converge, then we simply set the date to produce “long term” predictions.

To prepare the output of the CSV file, I create a string containing comma-separated data with the predicted direction for the next day:

>     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=",")) 
>     }
> }
Copy the code

The penultimate step is to export the CSV file.

Make sure you’re running in the same directory as your batches.csv file:

forecasts = open("forecasts.csv", "r").readlines()
Copy the code

So far, we have stored the corrected indicator file in the forecasts_new.csv file.

Strategy results

Now that we have generated the indicator CSV file, we need to compare its effect to “buy and hold.”

We first read the metrics from the CSV file and store them as spArimaGarch:

We then intersected ARIMA + GARCH’s forecast date with the original EARNINGS set of the S&P500.

Once the benefits of the ARIMA + GARCH strategy are achieved, asset curves can be created for the ARIMA + GARCH model and “buy and hold”. Finally, we combine them into a single data structure:

> spArimaGarchCurve = log( cumprod( 1 + spArimaGarchReturns ) ) > spBuyHoldCurve = log( cumprod( 1 + spIntersect[,2] ) )  > spCombinedCurve = merge( spArimaGarchCurve, spBuyHoldCurve, all=F )Copy the code

Finally, we can plot two yield curves on the same graph:

> xyplot( 
>   spCombinedCurve,
>   superpose=T,
>   col=c("darkred", "darkblue"),
>   lwd=2,
>   key=list( 
>     text=list(
>       c("ARIMA+GARCH", "Buy & Hold")
>     ),
>     lines=list(
>       lwd=2, col=c("darkred", "darkblue")
>     )
>   )
> )
Copy the code

The asset curve is as follows:

ARIMA + GARCH equity Curve vs. S&P500 “buy and hold”

As you can see, the ARIMA + GARCH strategy has significantly outperformed buy and hold over a 65-year period. But you can also see that most of the gains occurred between 1970 and 1980.

So is it really appropriate to apply such models to historical sequences? Another option is to start applying the model to the latest data. In fact, consider the performance of the last ten years, from January 1, 2005:

From 2005 to present, ARIMA + GARCH strategy and S&P500 “buy and hold” equity curve

 

Now that we have completed our discussion of ARIMA and GARCH models, I would like to continue the discussion of time series analysis by considering long state space models and co-integration time series.

These follow-on areas of time series will introduce us to models that can improve our forecasts, which will greatly enhance our trading profitability and/or reduce risk.

 


reference

1. Har-rv-j and recursive neural network (RNN) hybrid model predict and trade high frequency volatility of large stock indexes

2. Har-rv model based on mixed data sampling (MIDAS) regression in R language predicts GDP growth

3. Realization of volatility: ARCH model and HAR-RV model

4. Arma-egarch model of R language and integrated prediction algorithm are used to predict the actual volatility of SPX

5. VaR comparison of GARCH (1,1), MA and historical simulation method

6.R language multivariate COPULA GARCH model time series prediction

7. VAR fitting and prediction based on ARMA-GARCH process for R language

8. Matlab prediction of Arma-GARCH conditional mean and variance models

9. ARIMA + GARCH trading strategy for S&P500 stock index in R language