Forecasting the stock market

In this recipe, we will develop a step-by-step 2-year forecast of the Fiat-Chrysler Automotive stock price.

This task will be accomplished by applying the Arima modeling technique to FCA stock time series.

Arima (Autoregressive integrated moving average) models basically involve the estimation of an autoregressive model and a moving average, employed to estimate both the stochastic part and the underlying trend.

Getting ready

This recipe is mainly based on the tseries package and forecast package, the first for Arima model fitting and the second for prediction of future values. We will also need the quantmod package in order to download stock data from Yahoo Finance.

We therefore need to install and load these three packages:

install.packages(c("tseries","forecast","quantmod"))
library(tseries)
library(forecast)
library(quantmod)

How to do it...

  1. Download data:
    sp500 <- new.env()
    stocks <- getSymbols(c("FCA"), env = sp500,
      
    from = as.Date("2015-01-01"), 
      to = as.Date("2015-10-31"))
    FCA <- sp500$FCA
    FCA <- get("FCA",envir = sp500)
    FCA <- with(sp500, FCA)
    FCA  <- as.data.frame(FCA)
    FCA  <- FCA$FCA.Adjusted
    
  2. Compute percentage log differences:
    time_series <- 100 * diff(log(FCA))
    
  3. Derive a train dataset:
    time_series_train <- time_series[1:(0.9 * length(time_series))] # Train dataset
    
  4. Train an Arima model on the train dataset:
    arima_fit      <- arima(time_series_train, order = c(2, 0, 2))
    arima_forecast <- forecast(arima_fit)
    
  5. Plot a forecast:
    plot(arima_forecast, main = "ARMA forecasts FCA returns")
    

    Let's take a look at the following graph:

    How to do it...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset