options("getSymbols.yahoo.warning"=FALSE)
suppressWarnings(suppressMessages(library(quantmod)))
amazon = getSymbols("AMZN", auto.assign=F, from = "2018-01-01", to = "2018-04-01")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
#plot(as.ts(amazon$AMZN.Open))
# functions to explore unprocessed xts from quantmod
chartSeries(amazon, type = "line")

# acf and pacf to get an idea about autocorrelation
library(forecast)
ggtsdisplay(amazon$AMZN.Open)

# Arima model
amazonarima = auto.arima(amazon$AMZN.Open, stepwise = T, approximation = F, trace = T); amazonarima
##
## ARIMA(2,1,2) with drift : 599.3494
## ARIMA(0,1,0) with drift : 595.3083
## ARIMA(1,1,0) with drift : 597.481
## ARIMA(0,1,1) with drift : 597.445
## ARIMA(0,1,0) : 593.982
## ARIMA(1,1,1) with drift : 599.3131
##
## Best model: ARIMA(0,1,0)
## Series: amazon$AMZN.Open
## ARIMA(0,1,0)
##
## sigma^2 estimated as 1127: log likelihood=-295.96
## AIC=593.91 AICc=593.98 BIC=596.01
plot(forecast(amazonarima, h = 20)) # next 20 days

# Ets model - exponential smoothing
amazonets <- ets(amazon$AMZN.Open)
# Forecast ets
plot(forecast(amazonets, h = 20)) #next 20 days

no clear trend, no seasonality. Therefore price not predictable.