This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
This is an example of using the quantmod package to make plots of stock market data from yahoo finance.
date()
[1] "Sat Jan 14 11:04:51 2017"
# install.packages("quantmod", dependencies = TRUE)
Start the quantmod package.
library(quantmod)
Loading required package: xts
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
Lets look at Goldman Sachs stock price.
getSymbols("GS", src="yahoo",from="2000-01-01")
As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
auto.assign=TRUE by default.
This behavior will be phased out in 0.5-0 when the call will
default to use auto.assign=FALSE. getOption("getSymbols.env") and
getOptions("getSymbols.auto.assign") are now checked for alternate defaults
This message is shown once per session and may be disabled by setting
options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
[1] "GS"
tail(GS)
GS.Open GS.High GS.Low GS.Close GS.Volume GS.Adjusted
2017-01-06 242.29 246.20 241.37 244.90 3585800 244.90
2017-01-09 243.25 244.69 241.47 242.89 3018600 242.89
2017-01-10 240.87 243.44 239.05 242.57 3404300 242.57
2017-01-11 242.77 245.84 242.00 245.76 3508400 245.76
2017-01-12 245.06 245.47 241.57 243.84 4016700 243.84
2017-01-13 245.43 247.77 242.91 244.30 4167900 244.30
chartSeries(GS)
Here are some plots.
barChart(GS,theme='white.mono',bar.type='hlc')
candleChart(GS,multi.col=TRUE,theme='white')
candleChart(GS,subset='2007-12::2008')
lineChart(GS,line.type='h',TA=NULL)
chartSeries(GS,theme="white",TA="addVo();addBBands();addCCI()")
chartSeries(GS,TA="addVo();addBBands();addCCI();addDEMA();addZLEMA()")
Here are some more plots.
chartSeries(GS,theme="white") #draw the chart
chartSeries(GS)
addBBands() #add Bollinger Bands
addCCI() #add Commodity Channel Index
d = Sys.Date() - 366*2
d
[1] "2015-01-13"
Here is the portfolio we will look at.
portfolio = c("GOOG","DOW","F","MSFT","DTO")
#portfolio = c("GOOG","DOW","F","GM","MSFT")
Get the stock symbolds from yahoo.finance.
getSymbols(portfolio, src="yahoo", from=d)
[1] "GOOG" "DOW" "F" "MSFT" "DTO"
tail(GOOG)
GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted
2017-01-06 795.26 807.900 792.204 806.15 1620500 806.15
2017-01-09 806.40 809.966 802.830 806.65 1272400 806.65
2017-01-10 807.86 809.130 803.510 804.79 1174300 804.79
2017-01-11 805.00 808.150 801.370 807.91 1057900 807.91
2017-01-12 807.14 807.390 799.170 806.36 1351000 806.36
2017-01-13 807.48 811.224 806.690 807.88 1090100 807.88
chartSeries(GOOG)
addBBands() #add Bollinger Bands
tail(DOW)
DOW.Open DOW.High DOW.Low DOW.Close DOW.Volume DOW.Adjusted
2017-01-06 57.80 57.89 57.11 57.44 4974800 57.44
2017-01-09 57.40 59.08 57.39 57.79 11091000 57.79
2017-01-10 57.71 57.83 57.16 57.67 5892200 57.67
2017-01-11 57.90 58.30 57.43 58.26 5781500 58.26
2017-01-12 58.27 58.61 58.06 58.29 6042200 58.29
2017-01-13 58.27 58.33 57.73 57.80 5757700 57.80
chartSeries(DOW)
addBBands() #add Bollinger Bands
tail(F)
F.Open F.High F.Low F.Close F.Volume F.Adjusted
2017-01-06 12.80 12.84 12.64 12.76 40247100 12.76
2017-01-09 12.79 12.86 12.63 12.63 39183400 12.63
2017-01-10 12.70 13.02 12.66 12.85 58480500 12.85
2017-01-11 12.71 12.72 12.51 12.67 49482100 12.67
2017-01-12 12.66 12.68 12.35 12.59 36468900 12.59
2017-01-13 12.62 12.67 12.50 12.63 29040300 12.63
chartSeries(F)
addBBands() #add Bollinger Bands
tail(MSFT)
MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
2017-01-06 62.30 63.15 62.04 62.84 19897900 62.84
2017-01-09 62.76 63.08 62.54 62.64 20256600 62.64
2017-01-10 62.73 63.07 62.28 62.62 18547000 62.62
2017-01-11 62.61 63.23 62.43 63.19 21462800 63.19
2017-01-12 63.06 63.40 61.95 62.61 20898900 62.61
2017-01-13 62.62 62.87 62.35 62.70 19350400 62.70
chartSeries(MSFT)
addBBands() #add Bollinger Bands
tail(DTO)
DTO.Open DTO.High DTO.Low DTO.Close DTO.Volume DTO.Adjusted
2017-01-06 104.99 106.20 103.40 105.06 4300 105.06
2017-01-09 107.28 111.00 107.28 111.00 5100 111.00
2017-01-10 109.88 114.18 109.88 113.93 11000 113.93
2017-01-11 113.06 114.20 108.20 110.89 8300 110.89
2017-01-12 106.93 108.50 106.00 108.50 5000 108.50
2017-01-13 109.70 110.22 109.23 109.65 1300 109.65
chartSeries(DTO)
addBBands() #add Bollinger Bands
d = Sys.Date() - 30
d
[1] "2016-12-15"
getSymbols(portfolio, from=d)
[1] "GOOG" "DOW" "F" "MSFT" "DTO"
tail(GOOG)
GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted
2017-01-06 795.26 807.900 792.204 806.15 1620500 806.15
2017-01-09 806.40 809.966 802.830 806.65 1272400 806.65
2017-01-10 807.86 809.130 803.510 804.79 1174300 804.79
2017-01-11 805.00 808.150 801.370 807.91 1057900 807.91
2017-01-12 807.14 807.390 799.170 806.36 1351000 806.36
2017-01-13 807.48 811.224 806.690 807.88 1090100 807.88
chartSeries(GOOG,theme="white")
tail(DOW)
DOW.Open DOW.High DOW.Low DOW.Close DOW.Volume DOW.Adjusted
2017-01-06 57.80 57.89 57.11 57.44 4974800 57.44
2017-01-09 57.40 59.08 57.39 57.79 11091000 57.79
2017-01-10 57.71 57.83 57.16 57.67 5892200 57.67
2017-01-11 57.90 58.30 57.43 58.26 5781500 58.26
2017-01-12 58.27 58.61 58.06 58.29 6042200 58.29
2017-01-13 58.27 58.33 57.73 57.80 5757700 57.80
chartSeries(DOW,theme="white")
tail(F)
F.Open F.High F.Low F.Close F.Volume F.Adjusted
2017-01-06 12.80 12.84 12.64 12.76 40247100 12.76
2017-01-09 12.79 12.86 12.63 12.63 39183400 12.63
2017-01-10 12.70 13.02 12.66 12.85 58480500 12.85
2017-01-11 12.71 12.72 12.51 12.67 49482100 12.67
2017-01-12 12.66 12.68 12.35 12.59 36468900 12.59
2017-01-13 12.62 12.67 12.50 12.63 29040300 12.63
chartSeries(F,theme="white")
tail(MSFT)
MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
2017-01-06 62.30 63.15 62.04 62.84 19897900 62.84
2017-01-09 62.76 63.08 62.54 62.64 20256600 62.64
2017-01-10 62.73 63.07 62.28 62.62 18547000 62.62
2017-01-11 62.61 63.23 62.43 63.19 21462800 63.19
2017-01-12 63.06 63.40 61.95 62.61 20898900 62.61
2017-01-13 62.62 62.87 62.35 62.70 19350400 62.70
chartSeries(MSFT,theme="white")
tail(DTO)
DTO.Open DTO.High DTO.Low DTO.Close DTO.Volume DTO.Adjusted
2017-01-06 104.99 106.20 103.40 105.06 4300 105.06
2017-01-09 107.28 111.00 107.28 111.00 5100 111.00
2017-01-10 109.88 114.18 109.88 113.93 11000 113.93
2017-01-11 113.06 114.20 108.20 110.89 8300 110.89
2017-01-12 106.93 108.50 106.00 108.50 5000 108.50
2017-01-13 109.70 110.22 109.23 109.65 1300 109.65
chartSeries(DTO,theme="white")