Interactive Data Visualization Using R for Stock Analysis

Written by: Ivan Casarrubias - Sr. Principal Consultant at Paradigm SES

Published on: August 21, 2019

Introduction

R is a very powerful language used for machine learning and business analytics. In R Studio, an analyst can write scripts to model and visualize data. When combined with packages like plot.ly, an analyst can render web pages from the R code by creating an RMD (R Markdown Language) file.

We will be using real-time stock price data for Microsoft and Oracle to measure the market sentiment and forecast price movements, while showcasing interactive data visualizations.

Data Preparation

Let's begin by loading the packages we need to create these visualizations. Blue Data Owl provided us with the web development services to create this web page.

library(tidyverse)
library(knitr)
library(plotly)
Sys.setenv("plotly_username"="BlueDataOwl")
Sys.setenv("plotly_api_key"="..........") 

library(quantmod)
library('ggplot2')
library('forecast')
library('tseries')
library(xts)
library(dygraphs)

Price Comparison

Next we pull in the stock price data and create a dataframe, which will be used to plot the prices for both stocks.

# basic example of ohlc charts
df <- data.frame(Date=index(MSFT),coredata(MSFT))
df <- tail(df, 30)

We will look at the previous 30 days

getSymbols("ORCL",src='yahoo')
## [1] "ORCL"
# basic example of ohlc charts
oracleDF <- data.frame(Date=index(ORCL),coredata(ORCL))
oracleDF <- tail(oracleDF, 30)
microsoft_xts <- xts(df$MSFT.Close,order.by=df$Date,frequency=365)
oracle_xts <- xts(oracleDF$ORCL.Close,order.by=oracleDF$Date,frequency=365)
 
stocks <- cbind(microsoft_xts,oracle_xts)
 
dygraph(stocks,ylab="Closing Price", 
        main="Microsoft and Oracle Closing Stock Prices") %>%
  dySeries("microsoft_xts",label="microsoft") %>%
  dySeries("oracle_xts",label="oracle") %>%
  dyOptions(colors = c("blue","brown")) %>%
  dyRangeSelector()

Using Dygraph, we can create an interactive visualization (instead of a static image) which allows us to plot and compare the stock prices side-by-side

Microsoft and Oracle Closing Stock Prices

Most interactive data visualizations and dashboards on websites require an expensive license and subscription to publish. For example Power BI and Tableau only allow users with a paid subscription to publish or share data visualizations

Candlestick Charts

Next we use the dataframe which contains Microsoft's stock prices to plot a candlestick chart.

p <- df %>%
  plot_ly(x = ~Date, type="candlestick",
          open = ~MSFT.Open, close = ~MSFT.Close,
          high = ~MSFT.High, low = ~MSFT.Low) %>%
  layout(title = "Microsoft last 30 Days Sentiment")

p

A Candlestick chart serves as a cornerstone of technical analysis, because candlestick patterns identify trends and market sentiment. Looking at a candlestick, one can identify an asset’s opening and closing prices, highs and lows, and overall range for a specific time frame.

Oracle <- oracleDF %>%
  plot_ly(x = ~Date, type="candlestick",
          open = ~ORCL.Open, close = ~ORCL.Close,
          high = ~ORCL.High, low = ~ORCL.Low) %>%
  layout(title = "Oracle last 30 Days Sentiment")

Oracle

Because these candle stick visualizations are created using plot.ly, a user can interact with the visualization by hovering over the candle sticks, or dragging the time-bar at the bottom to filter dates (i.e past 30 days vs past week, between date 1 and date 2, etc )

ARIMA

Lastly, we will forcast the price movement for Microsoft using ARIMA. In statistics, econometrics and any time series analysis, an Autoregressive Integrated Moving Average (ARIMA) model is a generalization of an autoregressive moving average (ARMA) model. Both of these models are fitted to time series data either to better understand the data or to predict future points in the series (forecasting).

ARIMA_Microsoft <- ts(df$MSFT.High,start=c(1)) 
Price_Forecast <- auto.arima(ARIMA_Microsoft, seasonal=TRUE) 
Predicted_Price <- forecast(Price_Forecast, h=10) 
plot(Predicted_Price)

In the above visualization, we used ARIMA to forecast. The forecasts are shown as a blue line, with the 80% prediction intervals as a dark shaded area, and the 95% prediction intervals as a light shaded area.

Learn More

Are you interested in learning more on data visualization and analytics? We can help create interactive web pages for your business. Email us at bd@bluedataowl.com

For Business Intelligence dashboards using Power BI, Tableau, Qlikview, Spotfire and more, contact our BI Consultants at bd@paradigmses.com


About the Author

Ivan Casarrubias studied Management Information Systems at Bauer College of Business (University of Houston) and is Sr. Principal Consultant at Paradigm SES. Paradigm SES is a premier Enterprise Performance Management and Business Intelligence firm with Fortune 500 clients from all major industries. Learn More about how Paradigm SES can help your company with our Oracle, Onestream, Hyperion and Azure experience.