将垂直线添加到quantmod :: chart_Series

时间:2021-10-13 12:37:28

I want to add vertical lines on several dates on a certain graph. So far I haven't managed to achieve this simple task. This is what I tried:

我想在某个图表上的几个日期添加垂直线条。到目前为止,我还没有完成这项简单的任务。这是我试过的:

> s <- get(getSymbols('nvmi'))["2012::"]
> d1 <- index(s[100])
> d1
[1] "2012-05-24"

> chart_Series(s,TA="addLines(v=d1)")
Error in get.current.chob() : improperly set or missing graphics device

> chart_Series(s)
> abline(v=d1) 
# nothing

> add_TA("addLines(v=d1")
Error in `[.data.frame`(lenv$xdata, Env$xsubset) : 
  undefined columns selected

From what I have already read here, I know that abline is not supposed to work with the new chart_Series function. It doesn't seem to work anyway. The addLines function does not work in any of the forms I tried - plain addLines, plot(addLines(...)), chart_Series(..., TA="addLines(...)") or add_TA("addLines(...)").

根据我在这里已经阅读的内容,我知道abline不适用于新的chart_Series函数。它似乎无论如何都不起作用。 addLines函数不能用于我尝试的任何形式 - plain addLines,plot(addLines(...)),chart_Series(...,TA =“addLines(...)”)或add_TA(“addLines( ...)“)。

I need to use the experimental version of quantmod because it solved other problems I had with the old version. d1 would eventually be a list of dates.

我需要使用quantmod的实验版本,因为它解决了旧版本的其他问题。 d1最终将成为日期列表。

1 个解决方案

#1


13  

You can't mix functions from the old and new versions of quantmod's charting functions. If you want to use addLines, you have to use chartSeries. Even if you use addLines and chartSeries, d1 should be an xts object, not a datetime object. For example:

您不能混合使用新旧版本的quantmod图表功能。如果要使用addLines,则必须使用chartSeries。即使您使用addLines和chartSeries,d1也应该是xts对象,而不是datetime对象。例如:

library(quantmod)
data(sample_matrix)
s <- as.xts(sample_matrix)
chartSeries(s,TA="addLines(v=s[100])")

将垂直线添加到quantmod :: chart_Series

If you want to add a vertical line using chart_Series, create a logical xts object with TRUE values where you want the lines to appear and FALSE otherwise. For example:

如果要使用chart_Series添加垂直线,请创建一个逻辑xts对象,其中TRUE值表示要显示的行,否则为FALSE。例如:

l <- xts(!as.logical(s[,1]),index(s))
l[100] <- TRUE
chart_Series(s,TA="add_TA(l,on=1)")

将垂直线添加到quantmod :: chart_Series

Also note that you can put the vertical line "behind" the chart by using on=-1 in the add_TA call:

另请注意,您可以通过在add_TA调用中使用on = -1将垂直线放在图表的“后面”:

chart_Series(s,TA="add_TA(l,on=-1,col='grey',border='grey')")

#1


13  

You can't mix functions from the old and new versions of quantmod's charting functions. If you want to use addLines, you have to use chartSeries. Even if you use addLines and chartSeries, d1 should be an xts object, not a datetime object. For example:

您不能混合使用新旧版本的quantmod图表功能。如果要使用addLines,则必须使用chartSeries。即使您使用addLines和chartSeries,d1也应该是xts对象,而不是datetime对象。例如:

library(quantmod)
data(sample_matrix)
s <- as.xts(sample_matrix)
chartSeries(s,TA="addLines(v=s[100])")

将垂直线添加到quantmod :: chart_Series

If you want to add a vertical line using chart_Series, create a logical xts object with TRUE values where you want the lines to appear and FALSE otherwise. For example:

如果要使用chart_Series添加垂直线,请创建一个逻辑xts对象,其中TRUE值表示要显示的行,否则为FALSE。例如:

l <- xts(!as.logical(s[,1]),index(s))
l[100] <- TRUE
chart_Series(s,TA="add_TA(l,on=1)")

将垂直线添加到quantmod :: chart_Series

Also note that you can put the vertical line "behind" the chart by using on=-1 in the add_TA call:

另请注意,您可以通过在add_TA调用中使用on = -1将垂直线放在图表的“后面”:

chart_Series(s,TA="add_TA(l,on=-1,col='grey',border='grey')")