【问题标题】:Adding Multiple Chart Series in Quantmod R在 Quantmod R 中添加多个图表系列
【发布时间】:2016-07-02 09:18:00
【问题描述】:

我正在尝试在 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()

【问题讨论】:

    标签: r charts finance quantmod quantitative-finance


    【解决方案1】:

    您可以使用chart_Series 代替chartSeries

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

    然后,如果您想在子面板中使用 RSI,只需添加 add_RSI()

    另一种方法是使用xts 的>= 0.10.0 版本(即根本不使用quantmod),您可以从https://github.com/joshuaulrich/xts 获得(0.10.0 还没有在CRAN 上)。 xts 中的新 plot 函数对于一次绘制 xts 对象的多个列非常友好。查看?plot.xts 了解新功能示例。

    编辑#2:

    为了更轻松地查看相对变化,您可以通过多种方式标准化您的价格序列。这是一种典型的方法(使用 0 原点是 Google 图表所做的):

    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)
    

    【讨论】:

    • 有没有办法标准化图表?所以它不是基于绝对价格数据,而是按比例比较图表
    • 在什么意义上缩放?在主图表上?您可以将证券价格除以初始价格水平,这样它们在绘制之前都从 1(比如说)开始?
    • 按百分比获得,类似于可以覆盖不同股票的谷歌图表。我也想添加 rsi 路径,以便查看趋势。
    • 见上面的第二次编辑。这正是我的第一条评论所暗示的,并且似乎也是谷歌所做的。这回答了您的问题 + 添加了一些额外的功能(格式),您可能会发现在使用 chart_Series 功能时很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多