我想使用quantmod 生成这样的情节https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110826/19da3834/attachment.png.
我想,我有点沮丧,这是一项非常简单的任务.我希望能够使用quantmod在图表上绘制一条线.经过几天的尝试,我无处可去.我看不到有关如何使用quantmod :: addLines函数执行此操作的示例(这是我找到的唯一类似问题,但无法找到答案http://r.789695.n4.nabble.com/quantmod- plot-trendline-td894632.html)
我的问题是:我想在指定的日期/时间/条nad y值绘制一条水平线.这条线只允许从某个特定条形开始的n(例如5)条长(另外我还想在指定的y值的行上方添加文本).
我尝试了几件事:
getSymbols("SPY")
lines.SPY <- (Hi(SPY) + Lo(SPY))/2
names(lines.SPY) <- c("lines")
lines.SPY$BuySell <- ifelse(lag(lines.SPY$lines) > lines.SPY$lines, 1, -1)
chartSeries(SPY, subset="2011-08::", theme=chartTheme('white',
up.col='blue', dn.col='red'))
addTA(lines.SPY$lines[lines.SPY$BuySell == -1,], type='p', col='darkred', pch="_", on=1, cex = 2.5)
addTA(lines.SPY$lines[lines.SPY$BuySell == 1,], type='p', col='green4', pch="_", on=1, cex = 2.5)
Run Code Online (Sandbox Code Playgroud)
但这实际上不是线......而且我不知道要添加文字......
然后我试过这个
getSymbols("SPY")
subset = "2011-03::"
dev.new()
chartSeries(SPY, subset=subset, theme="white")
test <- xts(rep(coredata(last(Cl(SPY))), 20), order.by=index(last(SPY, n=20)))
addTA(test, on=1, col="red", legend=NULL, lwd=3)
Run Code Online (Sandbox Code Playgroud)
同样,添加文本是不可能的.这种方法的另一个问题是我无法摆脱顶部的传说.因为我想在一个图表上绘制数十或数百个这样的线条,所以不应该显示图例...
在此先感谢您的想法/代码示例/ ...
最好的问候,萨莫.
(我只是复制 Stergios Marinopoulos 的 R-sig-finance 的答案)使用新的 Chart_Series() 函数以及文本和段。
require(quantmod)
getSymbols("SPY")
chart_Series(SPY, subset="2011-08::", type = "candlesticks" )
text(9, 112.00, "SOME TEXT", adj=0);
segments(9, 111.5, 12, 111.5) ;
Run Code Online (Sandbox Code Playgroud)
我的一些补充评论。添加消息:text(x,y,"message")其中 x 是条形图的编号(1 表示最左边的条形图;您可以使用 0 或负数来绘制左侧),y 是图表中的值。对于 adj,0 表示左对齐,1 表示右对齐,0.5 表示居中。在 0..1 范围之外会相应地移动它(但依赖于此可能是不明智的)。
snippets(x1,y1,x2,y2) 从 (x1,y1) 到 (x2,y2) 绘制一条线,其中 x 是柱形索引,y 是价格。
下面绘制一个等腰三角形,颜色为 20% 不透明红色: Polygon( c(20,30,40), c(5290,5320,5290), col="#ff000033")
即所有R图形功能都可用;但你必须使用chart_Series()。