API: quantmod

Access financial data through the quantmod API.

Table of Contents


By using SKEMA Quantum Studio framework (Warin 2019), this course will teach you how to use the quantmod package.

Database description

Quantmod is an R package designed to access financial data. It is a useful package to make quantitative analysis on the financial market. The environnement offers tools to explore different trading models and create some.

Quantmod: https://github.com/joshuaulrich/quantmod

Functions

Several functions are embedded in this library to obtain financial data, for example:

Each of these functions are detailed in this course and some examples are provided.

getSymbols()

The function getSymbols() takes as an input the ticker of the stock researched. It then requires the user to specify the timeframe of the data. It will then provide data regarding the price of the stock.

For example, we would like to download stock data regarding “Apple Inc.” (ticker: AAPL) from May, 2nd of 2016 until October, 2nd of 2016. All data will be stored in a variable named “AAPL”.


#Loading the quantmod library
library(quantmod)

# Access financial information on Apple stock
getSymbols("AAPL",src="yahoo", from="2016-05-02", to="2017-10-02") 

[1] "AAPL"

head(AAPL)

           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
2016-05-02     93.97     94.08    92.40      93.64    48160100
2016-05-03     94.20     95.74    93.68      95.18    56831300
2016-05-04     95.20     95.90    93.82      94.19    41025500
2016-05-05     94.00     94.07    92.68      93.24    35890500
2016-05-06     93.37     93.45    91.85      92.72    43699900
2016-05-09     93.00     93.77    92.59      92.79    32936400
           AAPL.Adjusted
2016-05-02      87.77052
2016-05-03      89.21397
2016-05-04      88.28603
2016-05-05      87.92770
2016-05-06      87.43733
2016-05-09      87.50332

You can also download and store your data in a .csv file.


# Download data
write.table(AAPL, file = "AAPL.csv", row.names=FALSE, col.names=TRUE, sep=",")

barChart()

To visualize your data, you need to call the function barChart(). It will showcase the price evolution and highlight posiive and negative trading days. Moreover, you will be able to visualize the volume of traded stocks per day.


# Visualize in a bar chart, with volume traded
barChart(AAPL)

You can also download and store your data in a .csv file.


# Download data
write.table(AAPL, file = "AAPL.csv", row.names=FALSE, col.names=TRUE, sep=",")

getDividends()

Furthermore, to help you analyse your stock, you can also access the dividend of the company with the function getDividends(). It also takes as an input the ticker of the stock researched. It then requires the user to specify the timeframe of the data.


# Get the dividend data
getDividends("AAPL",src="yahoo", from="2016-05-02", to="2017-10-02")

           AAPL.div
2016-05-05     0.57
2016-08-04     0.57
2016-11-03     0.57
2017-02-09     0.57
2017-05-11     0.63
2017-08-10     0.63

tl;dr


#Loading the quantmod library
library(quantmod)

# Access financial information on Apple stock
getSymbols("AAPL",src="yahoo", from="2016-05-02", to="2017-10-02") 


head(AAPL)

# Visualize in a bar chart, with volume traded
barChart(AAPL)

# Download data
write.table(AAPL, file = "AAPL.csv", row.names=FALSE, col.names=TRUE, sep=",")

# Get the dividend data
getDividends("AAPL",src="yahoo", from="2016-05-02", to="2017-10-02")

Code learned this week

Command Detail
getSymbols() stock data
barChart() Financial data visualisation
getDividends() stock dividend data()

References

This course uses the quantmod package documentation


Warin, Thierry. 2019. “SKEMA Quantum Studio: A Technological Framework for Data Science in Higher Education.” https://doi.org/10.6084/m9.figshare.8204195.v2.

Citation

For attribution, please cite this work as

Warin (2020, Jan. 29). Virtual Campus: API: quantmod. Retrieved from https://virtualcampus.skemagloballab.io/posts/api-quantmod/

BibTeX citation

@misc{warin2020api:,
  author = {Warin, Thierry},
  title = {Virtual Campus: API: quantmod},
  url = {https://virtualcampus.skemagloballab.io/posts/api-quantmod/},
  year = {2020}
}