Finance with quantmod

Shawn Santo

February 05, 2019


1 Introduction

Package quantmod is an R package that provides a framework for quantitative financial modeling and trading. It provides a rapid prototyping environment that makes modeling easier by removing the repetitive workflow issues surrounding data management and visualization.

To get quantmod, run install.packages("quantmod") in your console. Then load the package with

library(quantmod)
library(tidyverse)

Before we get started with quantmod, we will get some practice reading in data sets and working with the apply function.

2 Read data

Data may be

  1. available in base R or through an R package such as the diamonds data set that is available through tidyverse;

  2. read in to R from a file on your computer;

  3. read in to R directly from a website;

  4. scraped from a website.

Today we will get practice with examples that involve 2 and 3. Some of the most popular R functions to accomplish this are

  • read.table()

  • read.csv()

  • load()

2.1 Exercises

Read in the following data sets and save them as a well-named R object. A preview of each data set is given below for you to check your answer. Make sure all variable types are the same, headers are available when applicable, and NA values appear where appropriate.

  1. nevada_casino_sqft.csv (available on Google Classroom)
  1. nevada_casino_sqft.csv (available at: http://www.stat.ufl.edu/~winner/data/nevada_casino_sqft.csv)
  1. qqq.tsv (available on Google Classroom)
  1. spy.txt (available on Google Classroom)

3 Apply

3.1 Matrix examples

The examples below can also be applied to data frames

# create a 3 x 3 matrix
my.matrix <- matrix(data = c(3, 5, 10, -1, 0, 2, 18, 5, -3),
                    nrow = 3, ncol = 3)

# matrix is filled column-wise, view matrix
my.matrix

# by columns
apply(X = my.matrix, MARGIN = 2, FUN = mean)
apply(X = my.matrix, MARGIN = 2, FUN = max)

# by rows
apply(X = my.matrix, MARGIN = 1, FUN = sd)
apply(X = my.matrix, MARGIN = 1, FUN = which.max)
apply(X = my.matrix, MARGIN = 1, FUN = sort)

3.2 Array examples

# create a 2 x 2 x 3 array that contains the numbers 1 - 12
my.array <- array(data = c(1:12), dim = c(2,2,3))

# view the array
my.array

# apply sum over 1 dimension
apply(my.array, 1, sum)
apply(my.array, 2, sum)
apply(my.array, 3, sum)

# apply sum over multiple dimensions
apply(my.array, c(1,2), sum)
apply(my.array, c(1,3), sum)
apply(my.array, c(2,3), sum)
apply(my.array, c(3,1), sum)
apply(my.array, c(3,2), sum)

3.3 Further details

A summary of the a,l,s,t apply functions

Command Description
apply(X, MARGIN, FUN, ...) Obtain a vector/array/list by applying FUN along the specified MARGIN of an array or matrix X
lapply(X, FUN, ...) Obtain a list by applying FUN to the elements of a list X
sapply(X, FUN, ...) Simplified version of lapply. Returns a vector/array instead of list.
tapply(X, INDEX, FUN, ...) Obtain a table by applying FUN to each combination of the factors given in INDEX
  • The above functions are good alternatives to loops

  • They are typically more efficient than loops (often run considerably faster on large data sets)

  • They take practice to get used to, but make analysis easier to debug and less prone to error when used effectively

  • Look at the Help’s examples

4 quantmod

4.1 Introduction

The function getSymbols will allow you to import stock market data into R. All you need is the company’s ticker symbol. It is also possible to import currency exchange rates. The resulting object is an xts object. An eXtensible Time Series (xts) object is similar to a data frame but optimized for time series data. Subsetting can be performed as usual.

Let’s look at Microsoft (MSFT).

getSymbols("MSFT")
[1] "MSFT"
head(MSFT)
           MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume
2007-01-03     29.91     30.25    29.40      29.86    76935100
2007-01-04     29.70     29.97    29.44      29.81    45774500
2007-01-05     29.63     29.75    29.45      29.64    44607200
2007-01-08     29.65     30.10    29.53      29.93    50220200
2007-01-09     30.00     30.18    29.73      29.96    44636600
2007-01-10     29.80     29.89    29.43      29.66    55017400
           MSFT.Adjusted
2007-01-03      22.57483
2007-01-04      22.53703
2007-01-05      22.40850
2007-01-08      22.62775
2007-01-09      22.65043
2007-01-10      22.42362

Function getSymbols automatically creates an R object named after the company’s stock ticker symbol. MSFT contains daily stock price information. One convenient aspect of working with an xts is that we can easily filter the data frame based on a range of dates. For example

MSFT["/2007-01-10"]
MSFT["2019/"]
MSFT["2019-01-01/2019-01-05"]

filters the data to everything before 2007-01-10, everything after 2019, and everything between 2019-01-01 and 2019-01-05, respectively.

quantmod also has functions that allow for easy charting.

chartSeries(x = MSFT)

chartSeries(x = MSFT, subset = "2019", theme=chartTheme('white'))

4.2 Exercises

  1. Import stock data of your choice using getSymbols. You will use this stock data to answer the rest of the questions.

  2. Use the names() function to get the variables’ names

  3. Add a column named XXXX.Change that is the difference between the stock’s close price and open price. XXXX should be replaced with your stock’s ticker symbol.

  4. Use chartSeries to create a chart of your stock’s 2018 price data.

  5. Use ggplot to create a histogram of the XXXX.Change values. Add labels.

  6. Use apply to compute the median for each variable of your stock’s data for 2018. Do the same for 2008.

4.3 Advanced exercises

Continue to work with the stock data you imported.

  1. Create a vector named returns, where returns represents the percent return based on the closing prices. The return, is defined as \[r_i = \frac{p_i - p_{i-1}}{p_{i-1}},\] where \(r_i\) represents the return and \(p_i\) represents the price at time \(i\).

  2. Take the natural log of returns + 1.

  3. Create a histogram of the log returns. What probability distribution does this look like?