Adding Multiple Chart Series in Quantmod R

时光总嘲笑我的痴心妄想 提交于 2019-12-05 00:55:28

问题


I am trying to plot two charts on one chartSeries in quantmod in R. I am having some difficulty doing this.

library(quantmod)    
tickers <- c('GLD', 'GDX')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data)
chartSeries(Cl(data$GLD), TA="addTA(Cl(data$GDX), on=1)")
addRSI()

回答1:


You could use chart_Series instead of chartSeries.

chart_Series(Cl(data$GLD))
add_TA(Cl(data$GDX), on = 1)

And then if you want RSI below in a sub panel, just add add_RSI().

Another approach is to use version >= 0.10.0 of xts (i.e. don't use quantmod at all), which you can get from https://github.com/joshuaulrich/xts (0.10.0 is not yet on CRAN). The new plot function in xts is very friendly with plotting multiple columns of an xts object all at once. Check out ?plot.xts for examples of new functionality.

Edit #2:

To see relative changes more easily, you can normalise your price series in many ways. This is a typical approach (using a 0 origin is what Google charts does):

normalise_series <- function(xdat) xdat / coredata(xdat)[1]
getSymbols("USO")
window <- "2013/"

# Define colour of default chart line to chart_Series in mytheme object
# which is passed to chart_Series:
mytheme <- chart_theme()
mytheme$col$line.col <- "darkgreen"
chart_Series(normalise_series(Cl(data$GLD)[window]) - 1, theme = mytheme)
add_TA(normalise_series(Cl(data$GDX)[window]) - 1, on = 1, col = "red", lty = 3)
add_TA(normalise_series(Cl(USO)[window]) - 1, on = 1, col = "blue", lty =2)

add_TA(RSI(Cl(data$GLD)), on = NA, col = "darkgreen")
add_TA(RSI(Cl(data$GDX)), on = 2, col = "red", lty = 3)
# Or add RSIs on different subpanels to improve readability of charts:
add_TA(RSI(Cl(USO)), on = NA, col = "blue", lty = 2)



来源:https://stackoverflow.com/questions/38136008/adding-multiple-chart-series-in-quantmod-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!