Quantmod

Quantitative Financial Modelling & Trading Framework for R

Anne Sophie Gill https://www.skemagloballab.io/gillAnneSophie.html (SKEMA Global Lab in AI)https://skemagloballab.io , Marine Leroi https://www.skemagloballab.io/leroiMarine.html (SKEMA Global Lab in AI)https://skemagloballab.io
02-20-2020

Using the SKEMA Quantum Studio(Warin 2019) framework, we 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.39169
2007-01-04      10.62234
2007-01-05      10.54669
2007-01-08      10.59878
2007-01-09      11.47922
2007-01-10      12.02857

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.18531
2007-01-04      22.14815
2007-01-05      22.02185
2007-01-08      22.23731
2007-01-09      22.25961
2007-01-10      22.03671

# 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

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


We hope this helped you navigate the Quantmod package more easily.


Follow SKEMA Global Lab in Augmented Intelligence

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

Gill & Leroi (2020, Feb. 20). Blog: Quantmod. Retrieved from https://blog.skemagloballab.io/posts/2020-02-20-quantmod/

BibTeX citation

@misc{gill2020quantmod,
  author = {Gill, Anne Sophie and Leroi, Marine},
  title = {Blog: Quantmod},
  url = {https://blog.skemagloballab.io/posts/2020-02-20-quantmod/},
  year = {2020}
}