如何使用 quantmod 在新图中叠加多个 TA? [英] How to overlay multiple TA in new plot using quantmod?
问题描述
我们可以使用图表系列函数绘制蜡烛图chartSeries(Cl(PSEC))
我创建了一些自定义值 (I1,I2> 和 I3),我想在烛条图案之外一起绘制(叠加).为此,我使用了 addTA()
We can plot candle stick chart using chart series function chartSeries(Cl(PSEC))
I have created some custom values (I1,I2 and I3) which I want to plot together(overlay) outside the candle stick pattern. I have used addTA()
for this purpose
chartSeries(Cl(PSEC)), TA="addTA(I1,col=2);addTA(I2,col=3);addTA(I3,col=4)")
问题是它分别为 Cl(PSEC),I1,I2 和 I3 绘制了四个图,而不是我想要的两个图 Cl(PSEC) 和 (I1,I2,I3)
The problem is that it plots four plots for Cl(PSEC),I1,I2 and I3 separately instead of two plots which I want Cl(PSEC) and (I1,I2,I3)
已编辑
为了清楚起见,我给出了一个示例代码,其中包含为此目的创建的 I1、I2 和 I3 变量
For clarity I am giving a sample code with I1, I2 and I3 variable created for this purpose
library(quantmod)
PSEC=getSymbols("PSEC",auto.assign=F)
price=Cl(PSEC)
I1=SMA(price,3)
I2=SMA(price,10)
I3=SMA(price,15)
chartSeries(price, TA="addTA(I1,col=2);addTA(I2,col=3);addTA(I3,col=4)")
推荐答案
这里有一个选项可以在很大程度上保留您的原始代码.您可以在第一个之后使用选项 on=2
为每个 TA 获得所需的结果:
Here is an option which preserves largely your original code.
You can obtain the desired result using the option on=2
for each TA after the first:
library(quantmod)
getSymbols("PSEC")
price <- Cl(PSEC)
I1 <- SMA(price,3)
I2 <- SMA(price,10)
I3 <- SMA(price,15)
chartSeries(price, TA=list("addTA(I1, col=2)", "addTA(I2, col=4, on=2)",
"addTA(I3, col=5, on=2)"), subset = "last 6 months")
如果您想在一张图表中叠加价格和 SMA,您可以为每个 TA 使用选项 on=1
.
If you want to overlay the price and the SMAs in one chart, you can use the option on=1
for each TA.
感谢@hvollmeier,他的回答让我意识到我在之前版本的回答中误解了您的问题.
Thanks to @hvollmeier who made me realize with his answer that I had misunderstood your question in the previous version of my answer.
PS:请注意,?addSMA()
中描述了几个选项,包括 with.col
可用于选择时间序列的特定列(Cl是默认列).
PS: Note that several options are described in ?addSMA()
, including with.col
which can be used to select a specific column of the time series (Cl is the default column).
这篇关于如何使用 quantmod 在新图中叠加多个 TA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!