Using the R language for S & P500 stock index trading strategies ARIMA + GARCH

Original link: http://tecdat.cn/?p=7207

 

In this article, I want to show you how to apply trading strategies S & P500 index of the American stock market.

 By combining ARIMA and GARCH model, in the long run, we can greatly outperform "buy and hold" approach.

Policy Overview

 The strategy "rolling" on the basis of execution:

  1. For each day, the stock index's gains for the first few k days before the k -day window is used as the best fit ARIMA and GARCH. 
  2. Integrated model to predict the next day's gains.
  3. If the forecast is negative, on a short stocks closing, and if the forecast is positive, then do more.
  4. If the prediction same direction as the previous day, then nothing will change.

 

Strategy Implementation

 

 

 The first task is to install and R introducing the necessary library:

If you have installed library, you can simply import them:

> library(quantmod)
> library(lattice)
> library(timeSeries)
> library(rugarch)

Upon completion, the policy will be applied to the S & P500. 

We can then create a Standard & Poor's "closing price" of a number of differences in the yield of 500, and the removal of the initial value of NA:

 

According to Akaike Information Criterion, the cycle will provide us with the "best" fit ARMA model, then we can use it for input GARCH 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 ) 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
>         }
>     }

 

If GARCH model can not converge, then we only need to set the date to have a "long-term" prediction. 

To prepare for the output CSV file, I created a string containing a comma-separated data, and with the next day's forecast direction:

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

The penultimate step is to CSV file output. 

 To ensure that the forecasts.csvoperation of its file in the same directory:

forecasts = open("forecasts.csv", "r").readlines()

So far, we have corrected index file is stored in forecasts_new.csv

Policy Results

Now, we have generated indicators CSV file, we need to effect the "buy and hold" for comparison.

We first read index and store from a CSV file as spArimaGarch:

Then, we will ARIMA + GARCH predicted the date of the original return set S & P500 intersect. 

Once earnings ARIMA + GARCH strategy that can ARIMA + GARCH model and "buy and hold" Creating assets curves. Finally, we will combine them into a data structure:

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

Finally, we can use the following xyplotdraw two equity curves on the same graph command:

> 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")
>     )
>   )
> )

At the balance curve October 6, 2015 is as follows:


 ARIMA + equity curve GARCH policies and S & P500 of "buy and hold"

As you can see, in 65 years time, ARIMA + GARCH strategy significantly outperformed "buy and hold." However, you can also see that most of the gains occurred between 1970 and 1980. 

  Therefore, before the invention of such models will be applied to the historical series really suit you? Another option is to start applying the model to the latest data. In fact, we can consider the performance of the last decade from January 1, 2005 to date:


Since 2005, ARIMA + GARCH strategy and S & P500 of "buy and hold" stock curve

 

Now, we have completed discussions ARIMA and GARCH model series, I want to consider a long stored procedure, state space model and co integration time series time series analysis to continue the discussion.

These follow the field of time series will introduce some of our models that can improve our forecast, it exceeded my prediction here proposed, which will greatly enhance our trading profitability and / or reduce the risk.

 

 If you have any questions, please leave a comment below. 

 

 

  

Big Data tribe  - Chinese professional third-party data service providers to provide customized one-stop data mining and statistical analysis consultancy services

Statistical analysis and data mining consulting services: y0.cn/teradat (Consulting Services, please contact the official website customer service )

Click here to send me a messageQQ:3025393450

 

QQ exchange group: 186 388 004 

[Service] Scene  

Research; the company outsourcing; online and offline one training; data reptile collection; academic research; report writing; market research.

[Tribe] big data to provide customized one-stop data mining and statistical analysis consultancy

Welcome to elective our R language data analysis will be mining will know the course!

 

 

  

Big Data tribe  - Chinese professional third-party data service providers to provide customized one-stop data mining and statistical analysis consultancy services

Statistical analysis and data mining consulting services: y0.cn/teradat (Consulting Services, please contact the official website customer service )

Click here to send me a messageQQ:3025393450

 

QQ exchange group: 186 388 004 

[Service] Scene  

Research; the company outsourcing; online and offline one training; data reptile collection; academic research; report writing; market research.

[Tribe] big data to provide customized one-stop data mining and statistical analysis consultancy

Welcome to elective our R language data analysis will be mining will know the course!

 

Guess you like

Origin www.cnblogs.com/tecdat/p/11587113.html