[API] Quantmod: Application

Quantitative Financial Modelling & Trading Framework for R

Thierry Warin https://warin.ca/aboutme.html (HEC Montréal and CIRANO (Canada))https://www.hec.ca/en/profs/thierry.warin.html
02-20-2020

This course will teach you how to use the Quantmod package.

Quantmod 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.

How to use the Quantmod package


#Loading the quantmod library

library(quantmod)

# Get list of stock exchange symbols

symbols <- stockSymbols()

# Access financial information on Apple and Microsoft stocks

getSymbols("AAPL",src="yahoo")

[1] "AAPL"

head(AAPL)

           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
2007-01-03  12.32714  12.36857 11.70000   11.97143   309579900
2007-01-04  12.00714  12.27857 11.97429   12.23714   211815100
2007-01-05  12.25286  12.31428 12.05714   12.15000   208685400
2007-01-08  12.28000  12.36143 12.18286   12.21000   199276700
2007-01-09  12.35000  13.28286 12.16429   13.22429   837324600
2007-01-10  13.53571  13.97143 13.35000   13.85714   738220000
           AAPL.Adjusted
2007-01-03      10.36364
2007-01-04      10.59366
2007-01-05      10.51822
2007-01-08      10.57016
2007-01-09      11.44823
2007-01-10      11.99609

getSymbols("MSFT",src="yahoo")

[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.12369
2007-01-04      22.08664
2007-01-05      21.96068
2007-01-08      22.17555
2007-01-09      22.19778
2007-01-10      21.97551

# Visualize in a bar chart, with volume traded

barChart(AAPL)


barChart(MSFT)


# Download data

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

write.table(MSFT, file = "MSFT.csv", row.names=FALSE, col.names=TRUE, sep=",")

# Get the dividend data

getDividends("AAPL",src="yahoo", from = "2019-01-01", to = "2020-12-31", split.adjust = FALSE)

           AAPL.div
2019-02-08     0.73
2019-05-10     0.77
2019-08-09     0.77
2019-11-07     0.77
2020-02-07     0.77
2020-05-08     0.82

getDividends("MSFT",src="yahoo", from = "2019-01-01", to = "2020-12-31", split.adjust = FALSE)

           MSFT.div
2019-02-20     0.46
2019-05-15     0.46
2019-08-14     0.46
2019-11-20     0.51
2020-02-19     0.51
2020-05-20     0.51

Citation

For attribution, please cite this work as

Warin (2020, Feb. 20). Thierry Warin: [API] Quantmod: Application. Retrieved from https://warin.ca/posts/api-quantmod-application/

BibTeX citation

@misc{warin2020[api],
  author = {Warin, Thierry},
  title = {Thierry Warin: [API] Quantmod: Application},
  url = {https://warin.ca/posts/api-quantmod-application/},
  year = {2020}
}