在Quantmod R中添加多个图表系列 [英] Adding Multiple Chart Series in Quantmod R
问题描述
我试图在R中的quantmod中的一个 chartSeries
上绘制两个图表。我在执行此操作时遇到了一些困难。
库(quantmod)
代码<-c('GLD','GDX')
数据<-new.env()
getSymbols(tickers,src ='yahoo',from ='1980-01-01',env = data)
chartSeries(Cl(data $ GLD), TA = addTA(Cl(data $ GDX),on = 1))
addRSI()
您可以使用 chart_Series
代替 chartSeries
。 p>
chart_Series(Cl(data $ GLD))
add_TA(Cl(data $ GDX),on = 1)
然后,如果要在子面板中的下面使用RSI,只需添加 add_RSI()
。
另一种方法是使用版本<= 0.10.0的 xts
(即完全不使用 quantmod
),您可以从
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()
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)
这篇关于在Quantmod R中添加多个图表系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!