This document attempts to guide the reader through the acquisition and visualisation of financial price data using the “quantmod” package in R. Quantmod (Quantitative Financial Modelling and Trading Framework for R) is a widely-used package which is designed as a “rapid prototyping environment with comprehensive tools for data management and visualisation”.
Quantmod allows data to be attained from a variety of major online sources including Yahoo Finance (https://finance.yahoo.com/), Google Finance (https://www.google.com/finance), OandA (https://www.oanda.com/) and the US Federal Reserve’s “FRED” economic dataset (https://fred.stlouisfed.org/).
This guide will also use the “tidyverse” package.
The package required for this guide can be acquired in RStudio using:
install.packages("quantmod")
This will automatically download and install the package as well as its dependencies from CRAN. This guide was written using quantmod version 0.4-7 on R version 3.3.3. After installation, load packages using:
library(tidyverse)
library(quantmod)
This will also load the “xts”, “zoo” and “TTR” packages which are dependencies of quantmod. Briefly, xts (eXtensible Time Series) and zoo (Z’s Ordered Observations) aim to convert and facilitate the analysis of irregular and differently-formatted time series objects while TTR (Technical Trading Rules) provides a library of common financial analysis transformations.
Financial price data is typically a time series, simply meaning that it is indexed as a sequence ordered by time. Generally, the data contains five observations per time interval for any particular financial equity instrument, namely the open price, high price, low price, close price and volume (number of transactions).
This example uses end-of-day data for Tesla Inc from Yahoo Finance.
Before proceeding, we will need to know the ticker symbol for Tesla. This can easily be found by typing “Tesla” into the search bar at the top of the Yahoo Finance homepage. Doing so provides a list of potential matches, the first one being “TSLA” for the NASDAQ-listed equity.
The quantmod function for downloading the price data for Tesla is:
getSymbols("TSLA")
Producing the following dataframe named “TSLA” with 6 variables and 1704 rows comprising all the trading days since Tesla’s IPO in June 2010.
glimpse(TSLA)
## An 'xts' object on 2010-06-29/2017-04-04 containing:
## Data: num [1:1704, 1:6] 19 25.8 25 23 20 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:6] "TSLA.Open" "TSLA.High" "TSLA.Low" "TSLA.Close" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## List of 2
## $ src : chr "yahoo"
## $ updated: POSIXct[1:1], format: "2017-04-06 01:30:09"
| TSLA.Open | TSLA.High | TSLA.Low | TSLA.Close | TSLA.Volume | TSLA.Adjusted |
|---|---|---|---|---|---|
| 19.00 | 25.00 | 17.54 | 23.89 | 18766300 | 23.89 |
| 25.79 | 30.42 | 23.30 | 23.83 | 17187100 | 23.83 |
| 25.00 | 25.92 | 20.27 | 21.96 | 8218800 | 21.96 |
| 23.00 | 23.10 | 18.71 | 19.20 | 5139800 | 19.20 |
| 20.00 | 20.00 | 15.83 | 16.11 | 6866900 | 16.11 |
| 16.40 | 16.63 | 14.98 | 15.80 | 6921700 | 15.80 |
Tesla has not undergone any share splits or rights issues so the TSLA.Adjusted vector is identical to the TSLA.Close vector. This duplicate column is excluded from a new TESLA dataframe.
TESLA <- subset(TSLA, select = -TSLA.Adjusted)
Producing a line chart on quantmod to quickly visualise price data is straightforward:
chart_Series(TESLA)
However there are several popular alternatives for financial charting. Below is another example in the “candlestick” format which has been limited to Tesla’s year-to-date performance.
Candlestick charts may look similar to box plots but are constructed differently. The bodies are formed using only the open and close prices.
Bodies colored white indicate an “up” day. The base of the rectangle signifies the opening price and the ceiling drawn at the close price.
Inversely, the boxes colored red indicate a “down” day with the base signifying the closing price and the ceiling showing the opening price.
The tails which protrude these bodies, if present, indicate the high and low prices if they differ from either the open or close.
The height of the bars (tails inclusive) display the range of prices which traded during that day. A taller bar therefore corresponds with a greater variance in prices as comparaed with a shorter bar.
Notice also that at this resolution, quantmod labels a weekly grid on the time axis by default.
chart_Series(TESLA['2017::'], name = "YTD Price Movement of Tesla")
Beyond visualisations, the quantmod package also includes functions which simplify the calculation of some common financial metrics. The following illustrates three brief examples.
The highest price recorded in a given time series can be quickly determined with the seriesHI() function. The result indicates that Tesla is currently at its all-time high, something which can be verified by quickly eyeballing the prior charts.
seriesHi(TESLA)
## TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume
## 2017-04-04 296.89 304.81 294.53 303.7 10099400
The market rate of return for a calendar year is an often cited metric for equities. As evidenced, 2013 was a standout year for Tesla sparked by the announcement of its first-ever quarterly profit off the back of the successful Model S.
yearlyReturn(TESLA)
## yearly.returns
## 2010-12-31 0.40157889
## 2011-12-30 0.07247466
## 2012-12-31 0.18592438
## 2013-12-31 3.44139349
## 2014-12-31 0.47849508
## 2015-12-31 0.07913309
## 2016-12-30 -0.10966207
## 2017-04-04 0.42121769
It may be helpful to plot the distribution of daily returns as a histogram. Doing so, the shape and skew of the data is quickly revealed.
TESLA_daily <- dailyReturn(TESLA)
ggplot(TESLA_daily) + geom_histogram(aes(x = daily.returns))
## Index daily.returns
## Min. :2010-06-29 Min. :-0.1932743
## 1st Qu.:2012-03-06 1st Qu.:-0.0139935
## Median :2013-11-13 Median : 0.0008859
## Mean :2013-11-14 Mean : 0.0021795
## 3rd Qu.:2015-07-27 3rd Qu.: 0.0182487
## Max. :2017-04-04 Max. : 0.2573684
= = = = =