SHARE PRICE PREDICTION USING R

Hey folks!!I will take you guys through the world of finances with this blog where I will show you how to predict the stock shares of a particular organization using R.

Ours is a period of unpredictable economy. An organization earning big bucks today might not be even in the frame of contention in a period of few years. Of late, the stock market has gained utmost attention from all possible sectors because of the high returns it promises. But stock market is no place for a novice. The last decade has seen a number of big stock market players biting the dust due to lack of proper insights into the stock trends. Such is the nature of the stock arena, huge revenue generator one day, major reason for downfall the other day. As such, there is a need for a comprehensive stock value prediction system.

In layman terms, stock market prediction is nothing but trying to determine the future stock prices of a company based on historic and presently available data. For a more formal definition, refer wiki. This calls for a proper statistical analysis framework. As I have made myself pretty clear through the previous posts, when it comes to statistical modeling, I always prefer R due to the abundance of statistical analysis packages it provides. So let’s get started…

Let us look into the stock prizes of Google, Inc (GOOG). GOOG has been traded on the NASDAQ stock exchange since their IPO on August 19th, 2004 at $85 per share. Now, it has seen a rise of about 1,294%. Quite a marvelous accomplishment one would say.

There are quite a few finance sites which would give you the historic stock prices of every company listed on NASDAQ, such as Yahoo and Google finance. Let me introduce some irony here and download the GOOG stock price from the Yahoo finance site. If you will open the link provided, you will see that the stock listing has a number of parameters such as:

  • Date
  • Opening and closing price
  • High and low of the day
  • The volume(number of stocks bought and sold)

Also, the beginning date is August 19, 2004, and the ending date is April 25, 2016 (when I retrieved the data). For the sake of convenience, I would just consider the stock prices from August 19, 2013 to April 25, 2016 on a monthly basis. You can see “Download to Spreadsheet” option at the bottom of the screen. Select that and obtain a CSV file of the stock prizes and store it in your working directory.

Now that we have the stock data, let us begin analyzing it to predict Google’s stock value for the next 2 years.

It is a multistage process which can be shown as:

Step 1: Importing the data

Firstly, we need to import the CSV file into our working environment using the read.csv () function. I will be using the “closing price” values for stock prediction. Refer the following screenshot for the process.

fig1

                                          Fig 1: Reading GOOG stock prize data

Step 2: Creating a time series function

Stock data represents a time series. As we can see from the data, the closing prices are listed in the 5th column, and are ordered from the most recent ones to the previous prices. To create a time series in R, ts() function is used. It can be shown as:

fig2

                                    Fig2: Converting input into a time series

Here,

Rev (goog[,5])  -> reversing the closing price values

Start = c (2013,8) -> starting date for the stock prices

Freq=12 -> 12 months in a year

In addition to the single time series, we can manipulate the data by taking the log of values and placing them both into an accessible data frame.

fig3

                        Fig 3: saving the closing values and the log values in a separate file

Step 3: Plotting the stock values

Now we can load the data frame and plot the GOOG stock prices to get an idea of any trends in the prices over 3 years.

fig4

                                Fig 4: to obtain a plot of the stock prices for 3 years

As you can see on your window, the following plot appears:

first plot

As you can analyze, the period 2014-2014.5 saw a substantial fall of the prices, which again improved post 2015.

Step 4: Decomposition

There is a function, stl() which decomposes the GOOGLE time series into seasonal, trend and remainder components. stl () finds the seasonal component through loess smoothing (or taking the mean if s.window=”periodic”). The seasonal component is removed from the data and the remainder is smoothed to find the trend. The residual component is calculated from seasonal plus trend fit residuals. Use the stl() function to decompose the closing prices in google, and plot the result.

fig5

                                         Fig 5: google time series decomposition

Step 5: Forecasting using stl()

Let us load the forecast function with the library () package. It enables us to forecast different time series models and linear models with the forecast () function. Let us consider the decomposed time series, google.stl, and determine a prediction method as “arima”. Let us predict 2 years into the future (h=24) with a 95% confidence interval (level=95). Then, visualize the results through a plot function.

fig6

                                                         Fig 6: ARIMA forecasting

The resulting plot can be shown as:

fig7

                                                       Fig 7: Forecasted stock prices for Google

With this result, I would definitely buy Google stocks.

So folks this was about share price prediction and forecasting of time series data. Hopefully you gained a few insights into the procedure. Keep checking this space for further posts on analytics.

References:

  • For a tutorial on stock market prediction using a package named “Quantmod”, click here.
  • For another example, click here.
  • I referred this link for understanding ARIMA model.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s