【问题标题】:Can chartSeries show more than 1 TTR indicator, when chartSeries line is inside an IF-statement?当 chartSeries 线位于 IF 语句内时,chartSeries 能否显示超过 1 个 TTR 指标?
【发布时间】:2016-10-12 23:13:54
【问题描述】:

我正在准备一个菜单,该菜单与生成图形相关,其中源是 xts 对象和 3 个 TTR 指标。似乎捆绑在 chartSeries 线周围的“if - 语句”使得并非所有指标都可见。目前即使改变指标的顺序,结果也只显示图表线最末端的指标。

下面是我在 R-Studio 中运行的代码。

########################################################
# 1. Create a minimal df
########################################################
date <- as.Date(c("2015-10-11", "2016-11-11", "2017-12-11"))
o <- c(1459.60, 1458.47, 1457.71)
c <- c(1458.44, 1457.68, 1458.27)
h <- c(1459.76, 1459.01, 1458.27)
l <- c(1458.08, 1457.52, 1457.70)
v <- c(185, 24080, 9503)
a <- c(185, 24080, 9503)

d3 <- data.frame(date,o,c,h,l,v,a)
colnames(d3)[1:7] <- c('date', 'open', 'close', 'high', 'low', 'volume', 'adjusted')

########################################################
# 2. Produce an xts(x3) based on a df (d3)
########################################################
x3 <- xts(d3[,-1], order.by = d3$date)

########################################################
# 3. Extraction of menu, to select 1 which should
# create a graph with 3 indicators
########################################################
cat("\014")
menu.level.1 <- readline(prompt="Please select just number 1: ")
if (menu.level.1 == 1) {
  print("...create graph")
  chartSeries(x3); addRSI(1); addMACD(2, 1, 1); addBBands(2)
}

【问题讨论】:

    标签: r menu rstudio xts quantmod


    【解决方案1】:

    似乎用这条线替换了折线chartSeries:

    chartSeries(myxts1, TA="addRSI();addMACD();addBBands()")
    

    【讨论】:

      【解决方案2】:

      我最初认为您的示例在 RStudio 之外运行良好,但后来我注意到 addMACD 结果没有被添加。

      library(quantmod)
      data(sample_matrix)
      x <- as.xts(sample_matrix)
      if (TRUE) {
        chartSeries(x)
        addRSI()
        addMACD()
        addBBands()
      }
      

      然后我记得当你在顶层以外的地方使用chartSerieschart_Series 时有时会发生这种情况(例如,在函数、for 循环等中)。解决方案是将add* 调用包装在plot 中。

      if (TRUE) {
        chartSeries(x)
        plot(addRSI())
        plot(addMACD())
        plot(addBBands())
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-14
        • 2021-03-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        • 2019-06-07
        • 2013-07-21
        相关资源
        最近更新 更多