如何动态添加指标到quantmod图表

时间:2015-08-13 22:30:32

标签: r quantmod

我想动态地将指标添加到quantmod图表中。我可以成功添加1个指标但是如何添加存储在字符向量中的多个?

#### THIS WORKS
VariableTA = "addBBands()"
df<-data.frame (Open = seq(2,102,1), High = seq(6,106,1), Low = seq(3,103,1), Close = seq(4,104,1))
date = as.POSIXct(seq(as.Date("2015-01-01"), as.Date("2015-06-04"), by="days")[1:101])
dat<-as.xts(df, tzone = Sys.getenv("GMT"), order.by =as.POSIXct(as.character(date)))
chartSeries(dat )
eval(parse(text = VariableTA))

#NOW I HAVE 2 indicator and it does not work
VariableTA = c("addBBands()","addEMA()")
df<-data.frame (Open = seq(2,102,1), High = seq(6,106,1), Low = seq(3,103,1), Close = seq(4,104,1))
date = as.POSIXct(seq(as.Date("2015-01-01"), as.Date("2015-06-04"), by="days")[1:101])
dat<-as.xts(df, tzone = Sys.getenv("GMT"), order.by =as.POSIXct(as.character(date)))
chartSeries(dat )
for(i in 1: length(VariableTA))
{
  eval(parse(text = VariableTA[i]))
}

1 个答案:

答案 0 :(得分:1)

如果您只是通过TA=参数chartSeries指定它们,那将会容易得多。

require(quantmod)
data(sample_matrix)
x <- as.xts(sample_matrix)
VariableTA = "addBBands();addEMA()"
chartSeries(x, TA=VariableTA)

enter image description here