#Getting AMAZON stock dataset and loading the needed packages
#https://www.quantstart.com/articles/ARIMA-GARCH-Trading-Strategy-on-the-SP500-Stock-Market-Index-Using-R/
if(!require(quantmod)) install.packages("quantmod")
if(!require(forecast)) install.packages("forecast")
if(!require(lattice)) install.packages("lattice")
if(!require(tseries)) install.packages("tseries")
if(!require(timeSeries)) install.packages("timeSeries")
if(!require(dplyr)) install.packages("dplyr")
if(!require(fGarch)) install.packages("fGarch")
if(!require(prophet)) install.packages("prophet")
if(!require(rugarch)) install.packages("rugarch")
library(prophet)
library(quantmod)
library(forecast)
library(tseries)
library(timeSeries)
library(dplyr)
library(fGarch)
library(lattice)
library(rugarch)
#getSymbols("^GSPC", src="yahoo", from="2018-01-01")
getSymbols("AAPL", src="yahoo", from="2018-01-01")
[1] "AAPL"
spReturns = diff(log(Cl(AAPL)))
spReturnsN=diff(Cl(AAPL))
spReturns[as.character(head(index(Cl(AAPL)),1))] = 0
windowLength = 500
foreLength = length(spReturns) - windowLength
forecasts <- vector(mode="character", length=foreLength)
for (d in 0:foreLength-1) {
spReturnsOffset = spReturns[(1+d):(windowLength+d)]
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
}
}
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(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=","))
}
}
[1] "2019-12-24,1"
[1] "2019-12-26,1"
[1] "2019-12-27,1"
[1] "2019-12-30,1"
[1] "2019-12-31,1"
[1] "2020-01-02,1"
[1] "2020-01-03,-1"
[1] "2020-01-06,1"
[1] "2020-01-07,1"
[1] "2020-01-08,1"
[1] "2020-01-09,1"
[1] "2020-01-10,1"
[1] "2020-01-13,1"
[1] "2020-01-14,1"
[1] "2020-01-15,1"
[1] "2020-01-16,1"
[1] "2020-01-17,1"
[1] "2020-01-21,1"
[1] "2020-01-22,1"
[1] "2020-01-23,1"
[1] "2020-01-24,1"
[1] "2020-01-27,1"
[1] "2020-01-28,1"
[1] "2020-01-29,1"
[1] "2020-01-30,1"
[1] "2020-01-31,1"
[1] "2020-02-03,1"
[1] "2020-02-04,1"
[1] "2020-02-05,1"
[1] "2020-02-06,-1"
[1] "2020-02-07,1"
[1] "2020-02-10,1"
[1] "2020-02-11,1"
[1] "2020-02-12,1"
[1] "2020-02-13,1"
[1] "2020-02-14,1"
[1] "2020-02-18,1"
[1] "2020-02-19,1"
[1] "2020-02-20,1"
[1] "2020-02-21,1"
[1] "2020-02-24,1"
[1] "2020-02-25,1"
[1] "2020-02-26,1"
[1] "2020-02-27,-1"
[1] "2020-02-28,1"
[1] "2020-03-02,-1"
[1] "2020-03-03,-1"
[1] "2020-03-04,-1"
[1] "2020-03-05,-1"
[1] "2020-03-06,1"
[1] "2020-03-09,1"
[1] "2020-03-10,1"
[1] "2020-03-11,1"
[1] "2020-03-12,1"
[1] "2020-03-13,1"
[1] "2020-03-16,1"
[1] "2020-03-17,1"
[1] "2020-03-18,1"
[1] "2020-03-19,-1"
[1] "2020-03-20,1"
[1] "2020-03-23,-1"
[1] "2020-03-24,-1"
[1] "2020-03-25,1"
[1] "2020-03-26,-1"
[1] "2020-03-27,1"
[1] "2020-03-30,1"
[1] "2020-03-31,-1"
[1] "2020-04-01,1"
[1] "2020-04-02,-1"
[1] "2020-04-03,1"
[1] "2020-04-06,-1"
[1] "2020-04-07,1"
[1] "2020-04-08,-1"
[1] "2020-04-09,-1"
[1] "2020-04-13,1"
[1] "2020-04-14,-1"
[1] "2020-04-15,1"
[1] "2020-04-16,-1"
[1] "2020-04-17,1"
[1] "2020-04-20,1"
[1] "2020-04-21,-1"
[1] "2020-04-22,1"
[1] "2020-04-23,-1"
[1] "2020-04-24,1"
[1] "2020-04-27,-1"
[1] "2020-04-28,1"
[1] "2020-04-29,-1"
[1] "2020-04-30,-1"
[1] "2020-05-01,1"
[1] "2020-05-04,1"
[1] "2020-05-05,1"
[1] "2020-05-06,-1"
[1] "2020-05-07,1"
[1] "2020-05-08,1"
[1] "2020-05-11,-1"
[1] "2020-05-12,1"
[1] "2020-05-13,-1"
[1] "2020-05-14,1"
[1] "2020-05-15,1"
[1] "2020-05-18,1"
[1] "2020-05-19,1"
[1] "2020-05-20,-1"
[1] "2020-05-21,1"
[1] "2020-05-22,-1"
[1] "2020-05-26,1"
write.csv(forecasts, file="forecasts_test.csv", row.names=FALSE)
# Input the Python-refined CSV file
spArimaGarch = as.xts(
read.zoo(
file="forecasts_new.csv", format="%Y-%m-%d", header=F, sep=","
)
)
# Create the ARIMA+GARCH returns
spIntersect = merge( spArimaGarch[,1], spReturns,AAPL$AAPL.Close, spReturnsN, all=F )
spArimaGarchReturns = spIntersect[,1] * spIntersect[,2]
# Create the backtests for ARIMA+GARCH and Buy & Hold
spArimaGarchCurve = log( cumprod( 1 + spArimaGarchReturns ) )
spBuyHoldCurve = log( cumprod( 1 + spIntersect[,2] ) )
spCombinedCurve = merge( spArimaGarchCurve, spBuyHoldCurve, all=F )
# Plot the equity curves
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")
)
)
)
dygraph(spCombinedCurve)%>% dyRangeSelector()
dygraph(spReturns)%>% dyRangeSelector()
spCombinedCurve1 = merge( spArimaGarchCurve, spBuyHoldCurve,spReturns, all=F )
dygraph(spCombinedCurve1)%>% dyRangeSelector()
dygraph(spIntersect[,"AAPL.Close.1"])%>% dyRangeSelector()