在Quantmod R中添加多个图表系列

时间:2021-07-11 03:14:34

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

我试图在R中的quantmod中的一个chartSeries上绘制两个图表。我在执行此操作时遇到了一些困难。

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 个解决方案

#1


11  

You could use chart_Series instead of chartSeries.

您可以使用chart_Series而不是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().

然后,如果您想在子面板中使用RSI,只需添加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.

另一种方法是使用版本> = 0.10.0的xts(即根本不使用quantmod),你可以从https://github.com/joshuaulrich/xts获得(0.10.0尚未在CRAN上) 。 xts中的新绘图功能非常友好,可以同时绘制xts对象的多个列。查看?plot.xts以获取新功能的示例。

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):

要更轻松地查看相对更改,您可以通过多种方式规范化价格序列。这是一种典型的方法(使用0来源是谷歌图表所做的):

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中添加多个图表系列

#1


11  

You could use chart_Series instead of chartSeries.

您可以使用chart_Series而不是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().

然后,如果您想在子面板中使用RSI,只需添加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.

另一种方法是使用版本> = 0.10.0的xts(即根本不使用quantmod),你可以从https://github.com/joshuaulrich/xts获得(0.10.0尚未在CRAN上) 。 xts中的新绘图功能非常友好,可以同时绘制xts对象的多个列。查看?plot.xts以获取新功能的示例。

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):

要更轻松地查看相对更改,您可以通过多种方式规范化价格序列。这是一种典型的方法(使用0来源是谷歌图表所做的):

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中添加多个图表系列