Charting with quantmod package
In this post series, I will show some of the quantmod package graphic capabilities by charting share financial data.
library(quantmod)
At the purpose, I herein download 2014 year first three months Yahoo share prices.
ticker <- "YHOO"
stock <- new.env()
startDate = as.Date("2014-01-01")
endDate = as.Date("2014-03-31")
getSymbols(ticker, source="yahoo", env = stock, from = startDate, to=endDate)
## 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] "YHOO"
share <- stock[[ticker]]
Starting from a HLOCV object, using quantmod package is straightforward to graph a nice chart. However some options should be evaluated:
- type of required graph (auto, candlesticks, matchsticks, bars, line)
- theme to be used (white, white.mono, black, black.mono, beige, wsj)
Defaults are candlestick graph type and black theme.
chartSeries(share, type='auto', theme = chartTheme("black"))
chartSeries(share, type='candlesticks', theme = chartTheme("black"))
chartSeries(share, type='matchsticks', theme = chartTheme("black"))
chartSeries(share, type='bars', theme = chartTheme("black"))
chartSeries(share, type='line', theme = chartTheme("black"))
chartSeries(share, type='candlesticks', theme = chartTheme("white"))
chartSeries(share, type='candlesticks', theme = chartTheme("white.mono"))
chartSeries(share, type='candlesticks', theme = chartTheme("black.mono"))
chartSeries(share, type='candlesticks', theme = chartTheme("beige"))
chartSeries(share, type='candlesticks', theme = chartTheme("wsj"))
The chart look&feel is determined by the chart.theme object.
ctheme <- get(".chart.theme", as.environment("package:quantmod"))
class(ctheme)
## [1] "chart.theme"
ctheme
## List of 6
## $ white :List of 24
## ..$ fg.col : chr "#000000"
## ..$ bg.col : chr "#F0F0F0"
## ..$ grid.col : chr "#CCCCCC"
## ..$ border : chr "#444444"
## ..$ minor.tick : chr "#888888"
## ..$ major.tick : chr "#000000"
## ..$ up.col : chr "#00CC00"
## ..$ dn.col : chr "#FF7700"
## ..$ dn.up.col : chr "#888888"
## ..$ up.up.col : chr "#FFFFFF"
## ..$ dn.dn.col : chr "#FF0000"
## ..$ up.dn.col : chr "#000000"
## ..$ up.border : chr "#444444"
## ..$ dn.border : chr "#444444"
## ..$ dn.up.border: chr "#444444"
## ..$ up.up.border: chr "#444444"
## ..$ dn.dn.border: chr "#444444"
## ..$ up.dn.border: chr "#444444"
## ..$ main.col : chr "#555555"
## ..$ sub.col : chr "#555555"
## ..$ area : chr "#FFFFFF"
## ..$ fill : chr "#F7F7F7"
## ..$ Expiry : chr "#C9C9C9"
## ..$ theme.name : chr "white"
## $ white.mono:List of 26
## ..$ fg.col : chr "#666666"
## ..$ bg.col : chr "#FFFFFF"
## ..$ grid.col : chr "#CCCCCC"
## ..$ border : chr "#666666"
## ..$ minor.tick : chr "#CCCCCC"
## ..$ major.tick : chr "#888888"
## ..$ up.col : chr "#000000"
## ..$ dn.col : chr "#000000"
## ..$ dn.up.col : chr "#888888"
## ..$ up.up.col : chr "#FFFFFF"
## ..$ dn.dn.col : chr "#4D4D4D"
## ..$ up.dn.col : chr "#000000"
## ..$ up.border : chr "#666666"
## ..$ dn.border : chr "#666666"
## ..$ dn.up.border: chr "#666666"
## ..$ up.up.border: chr "#666666"
## ..$ dn.dn.border: chr "#666666"
## ..$ up.dn.border: chr "#666666"
## ..$ main.col : chr "#555555"
## ..$ sub.col : chr "#555555"
## ..$ fill : chr "#F7F7F7"
## ..$ Expiry : chr "#C9C9C9"
## ..$ BBands.col : chr "#666666"
## ..$ BBands.fill : chr "#F7F7F7"
## ..$ BBands :List of 2
## .. ..$ col : chr "#666666"
## .. ..$ fill: chr "#F7F7F7"
## ..$ theme.name : chr "white.mono"
## $ black :List of 27
## ..$ fg.col : chr "#666666"
## ..$ bg.col : chr "#222222"
## ..$ grid.col : chr "#303030"
## ..$ border : chr "#666666"
## ..$ minor.tick : chr "#303030"
## ..$ major.tick : chr "#AAAAAA"
## ..$ up.col : chr "#00FF00"
## ..$ dn.col : chr "#FF9900"
## ..$ dn.up.col : chr "#888888"
## ..$ up.up.col : chr "#FFFFFF"
## ..$ dn.dn.col : chr "#FF0000"
## ..$ up.dn.col : chr "#000000"
## ..$ up.border : chr "#666666"
## ..$ dn.border : chr "#666666"
## ..$ dn.up.border: chr "#666666"
## ..$ up.up.border: chr "#666666"
## ..$ dn.dn.border: chr "#666666"
## ..$ up.dn.border: chr "#666666"
## ..$ main.col : chr "#999999"
## ..$ sub.col : chr "#999999"
## ..$ area : chr "#252525"
## ..$ fill : chr "#282828"
## ..$ Expiry : chr "#383838"
## ..$ BBands.col : chr "red"
## ..$ BBands.fill : chr "#282828"
## ..$ BBands :List of 2
## .. ..$ col : chr "red"
## .. ..$ fill: chr "#282828"
## ..$ theme.name : chr "black"
## $ black.mono:List of 26
## ..$ fg.col : chr "#666666"
## ..$ bg.col : chr "#222222"
## ..$ grid.col : chr "#303030"
## ..$ border : chr "#666666"
## ..$ minor.tick : chr "#303030"
## ..$ major.tick : chr "#AAAAAA"
## ..$ up.col : chr "#FFFFFF"
## ..$ dn.col : chr "#FFFFFF"
## ..$ dn.up.col : chr "#888888"
## ..$ up.up.col : chr "#FFFFFF"
## ..$ dn.dn.col : chr "#4D4D4D"
## ..$ up.dn.col : chr "#000000"
## ..$ up.border : chr "#666666"
## ..$ dn.border : chr "#666666"
## ..$ dn.up.border: chr "#666666"
## ..$ up.up.border: chr "#666666"
## ..$ dn.dn.border: chr "#666666"
## ..$ up.dn.border: chr "#666666"
## ..$ main.col : chr "#999999"
## ..$ sub.col : chr "#999999"
## ..$ fill : chr "#777777"
## ..$ Expiry : chr "#383838"
## ..$ BBands :List of 2
## .. ..$ col : chr "#DDDDDD"
## .. ..$ fill: chr "#777777"
## ..$ BBands.col : chr "#DDDDDD"
## ..$ BBands.fill : chr "#777777"
## ..$ theme.name : chr "black.mono"
## $ beige :List of 26
## ..$ fg.col : chr "#888888"
## ..$ bg.col : chr "#F5F5D0"
## ..$ grid.col : chr "#CCCCCC"
## ..$ border : chr "#666666"
## ..$ minor.tick : chr "#CCCCCC"
## ..$ major.tick : chr "#AAAAAA"
## ..$ up.col : chr "#00FF00"
## ..$ dn.col : chr "#AA0000"
## ..$ dn.up.col : chr "#888888"
## ..$ up.up.col : chr "#FFFFFF"
## ..$ dn.dn.col : chr "#FF0000"
## ..$ up.dn.col : chr "#000000"
## ..$ up.border : chr "#666666"
## ..$ dn.border : chr "#666666"
## ..$ dn.up.border: chr "#666666"
## ..$ up.up.border: chr "#666666"
## ..$ dn.dn.border: chr "#666666"
## ..$ up.dn.border: chr "#666666"
## ..$ main.col : chr "#555555"
## ..$ sub.col : chr "#555555"
## ..$ fill : chr "#F5F5F5"
## ..$ Expiry : chr "#C9C9C9"
## ..$ BBands.col : chr "orange"
## ..$ BBands.fill : chr "#F5F5DF"
## ..$ BBands :List of 2
## .. ..$ col : chr "orange"
## .. ..$ fill: chr "#F5F5DF"
## ..$ theme.name : chr "beige"
## $ wsj :List of 24
## ..$ fg.col : chr "#000000"
## ..$ bg.col : chr "#F0F0F0"
## ..$ grid.col : chr "#ffffff"
## ..$ border : chr "#444444"
## ..$ minor.tick : chr "#888888"
## ..$ major.tick : chr "#000000"
## ..$ up.col : chr "#FFFFFF"
## ..$ dn.col : chr "#666666"
## ..$ dn.up.col : chr "#888888"
## ..$ up.up.col : chr "#FFFFFF"
## ..$ dn.dn.col : chr "#FF0000"
## ..$ up.dn.col : chr "#000000"
## ..$ up.border : chr "#444444"
## ..$ dn.border : chr "#666666"
## ..$ dn.up.border: chr "#444444"
## ..$ up.up.border: chr "#444444"
## ..$ dn.dn.border: chr "#444444"
## ..$ up.dn.border: chr "#444444"
## ..$ main.col : chr "#555555"
## ..$ sub.col : chr "#555555"
## ..$ area : chr "#d3d0af"
## ..$ fill : chr "#F7F7F7"
## ..$ Expiry : chr "#C9C9C9"
## ..$ theme.name : chr "wsj"
## - attr(*, "class")= chr "chart.theme"
As you can see, the chart.theme object is a list of all availables chart themes and collects for each one of those the corresponding parameters set. It is interesting to see how to define a custom theme. Herein I define a new theme named mytheme, where, starting from the white theme, the area and up/down candlestick colors are updated.
mytheme <- ctheme[["white"]]
mytheme$area <- "#DCDCDC"
mytheme$up.col <- "#228B22"
mytheme$dn.col <- "#FF0000"
ctheme$mytheme <- mytheme
unlockBinding(".chart.theme", env = as.environment("package:quantmod"))
try(assign(".chart.theme", ctheme, envir = as.environment("package:quantmod")))
lockBinding(".chart.theme", env = as.environment("package:quantmod"))
ctheme <- get(".chart.theme", as.environment("package:quantmod"))
chartSeries(share, type='candlesticks', theme = chartTheme("mytheme"))
In next post, I will show how to enrich the basic chart with additional quantitative indicators.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.