【问题标题】:chartSeries with XTS, can't plot points and technical indicators simultaneously带有 XTS 的 chartSeries,不能同时绘制点和技术指标
【发布时间】:2014-07-09 21:53:16
【问题描述】:

编辑:我安装了一个较小的示例,因此您可以根据需要复制它。

我正在使用一个 OHLC XTS 表(欧元/美元)

> theBars
                     Open  High   Low Close
2014-06-17 01:42:26 13835 13836 13835 13836
2014-06-17 01:42:59 13836 13838 13835 13837
2014-06-17 01:43:21 13837 13838 13837 13837
2014-06-17 01:43:51 13837 13837 13837 13837
2014-06-17 01:44:23 13837 13837 13837 13837
2014-06-17 01:44:51 13837 13838 13837 13838
2014-06-17 01:45:28 13837 13840 13837 13840
2014-06-17 01:45:59 13840 13842 13840 13842
2014-06-17 01:46:22 13842 13843 13842 13843
2014-06-17 01:46:58 13843 13844 13843 13844
2014-06-17 01:47:29 13843 13844 13843 13843
2014-06-17 01:47:58 13843 13843 13841 13843
2014-06-17 01:48:22 13843 13843 13842 13843
2014-06-17 01:48:59 13843 13843 13842 13842
2014-06-17 01:49:05 13842 13842 13841 13841
2014-06-17 01:49:54 13841 13841 13840 13841
2014-06-17 01:50:18 13841 13841 13841 13841
2014-06-17 01:50:44 13840 13840 13839 13840
2014-06-17 01:52:55 13839 13839 13838 13839
2014-06-17 01:53:42 13838 13839 13838 13838
2014-06-17 01:54:22 13837 13838 13837 13838
2014-06-17 01:54:58 13837 13838 13836 13837
2014-06-17 01:55:29 13836 13836 13834 13835
2014-06-17 01:55:59 13835 13837 13835 13837
2014-06-17 01:56:28 13837 13839 13837 13838
2014-06-17 01:56:59 13838 13838 13837 13837
2014-06-17 01:57:29 13837 13838 13837 13838
2014-06-17 01:57:59 13838 13838 13838 13838
2014-06-17 01:58:29 13838 13838 13836 13837
2014-06-17 01:58:58 13837 13837 13836 13836
2014-06-17 01:59:29 13836 13841 13836 13840
2014-06-17 01:59:59 13840 13840 13835 13837
2014-06-17 02:00:29 13837 13837 13836 13836
2014-06-17 02:00:58 13836 13836 13835 13836
2014-06-17 02:01:29 13835 13837 13835 13837
2014-06-17 02:01:58 13837 13837 13836 13836

我正在用它绘制点

points.default(x=timeIndex*tMult+1, #aligns with tMult = 3 when candles are candles, 1 when they are matchsticks
                     y=as.numeric(dataCol[i]), #the price, its around 13818
                     cex=dotSize,
                     pch=dotType,
                     col=thecolor)

按预期工作。 在这个例子中,这里是值

      x       y     cex     pch     col 
   "19" "13841"     "2"     "2" "green"
        x         y       cex       pch       col 
     "19"   "13841"       "3"       "2" "#7070FF" 
        x         y       cex       pch       col 
     "19"   "13841"       "4"       "2" "#7070FF"

(每个点的中心是相同的,因为它是同时以一个价格进行多笔交易) 我还用

绘制了一些技术指标
plot(addMACD(fast,slow,signal,maType,histogram))

 plot(addSMA(n,overlay = overlay))

等等。仅使用默认值也可以按预期工作。

如果我做一个技术,将技术叠加到图表上,如 bbands 和 SMA,则可以同时绘制这些点。但是,如果我有一个像 MACD 这样的绘图,它位于一个单独的框中,将图形分成同一个绘图窗口中的两个图形,我无法再绘制这些点。为什么?!

【问题讨论】:

  • 您无需为未包含可重现的示例而向我们道歉。这只是意味着更少的人可能会尝试帮助您。
  • 是的,但我知道如果我不包括它,我会得到关于它的信息
  • 编辑以包含一个示例

标签: r charts xts quantmod


【解决方案1】:

y 轴根据添加的指标重新索引。 我发现最好自己重新索引它,然后通过调整点的位置来绘制图表。我将在此处包含通过该搜索找到的所有常量。

假设您的数据在 OHLC 中的一个名为“theBars”的变量中,请执行此操作par(usr=c(0,1,min(theBars),max(theBars)),xpd=TRUE)

我在 1920x1200 分辨率的显示器上完成了所有这些操作,因此所有的像素测量都是以此为依据的。这与使用par("fin") 的任何分辨率兼容 此外,这在几分之一秒内执行。这是为了清晰而不是速度而写的。

跨 [0,1] 的水平对齐,带有不同数量的蜡烛:

if(numBars < 10) stop("cannot plot less than 10 candles")
    horizNumerator <- if(numBars == 10) 1547 else
                      if(numBars == 11) 1559 else
                      if(numBars == 12) 1568 else
                      if(numBars == 13) 1574 else
                      if(numBars == 14) 1581 else
                      if(numBars == 15) 1588 else
                      if(numBars == 16) 1592 else
                      if(numBars == 17) 1597 else
                      if(numBars == 18) 1601 else
                      if(numBars == 19) 1603 else
                      if(numBars == 20) 1606 else
                      if(numBars <= 30) 1626 + (numBars-30)/(10/20) else
                      if(numBars <= 40) 1636 + (numBars-40)/(10/10) else
                      if(numBars <= 50) 1641 + (numBars-50)/(10/7) else
                      if(numBars <= 60) 1646 + (numBars-60)/(10/5) else
                      if(numBars <= 100) 1651 + (numBars-100)/(40/5) else
                      if(numBars <= 140) 1655 + (numBars-140)/(40/4) else
                      if(numBars <= 312) 1658 + (numBars-312)/(172/3) else
                      if(numBars > 312) 1662



   horizUnit <- (horizNumerator/1802)/(length(theBars[,1])-1)*ifelse(plotNum<2,1,1693/1655)*par("fin")[1]/20
    startHoriz <- ifelse(plotNum<2,55/1802,35/1802)*par("fin")[1]/20

垂直对齐,plotNum 是已经绘制的指标数量,deltaP &lt;- max(theBars)-min(theBars)

这涉及挤压绘图区域的指标

pixelStretch <- (switch(as.character(plotNum),"0"=975,"1"=665,"2"=551,"3"=465, "4"=400,"5"=352,"6"= 313))/(983) 
deltaP <- max(theBars)-min(theBars)

这涉及在绘制指标后最大值移动略高

maxRaise <- if(plotNum == 0) 0 else
                if(plotNum == 1) 15 else
                if(plotNum == 2) 31 else
                if(plotNum >  2) 36
    maxRaise <- maxRaise*par("fin")[2]/12.0625/983

然后像这样在数据列dataCol 中绘制点

  points.default(x=startHoriz + horizUnit*timeIndex,
                 y= maxRaise * deltaP + max(theBars)-((max(theBars)-as.numeric(dataCol[i]))*(pixelStretch)),
                 cex=dotSize,
                 pch=dotType,
                 col=thecolor)

【讨论】:

    猜你喜欢
    • 2022-06-19
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多