R 带XTS、can和x27的图表系列;t同时绘制点和技术指标

R 带XTS、can和x27的图表系列;t同时绘制点和技术指标,r,charts,xts,quantmod,R,Charts,Xts,Quantmod,编辑:我制作了一个较小的示例,以便您可以根据需要复制它 我正在使用OHLC XTS表(欧元/美元) 我在上面画点 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

编辑:我制作了一个较小的示例,以便您可以根据需要复制它

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

我在上面画点

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))

等等,仅使用默认值即可按预期工作


如果我做一个技术,将技术覆盖在图形上,如bbands和SMA,那么这些点可以同时绘制。但是,如果我有一个类似MACD的绘图,它位于一个单独的框中,将图形分隔为同一绘图窗口中的两个图形,我将无法再绘制点。为什么

y轴根据添加的指示器重新编制索引。 我发现最好是自己重新编制索引,然后通过调整点的位置来绘制图表。我将在这里包括通过搜索找到的所有常量

假设您的数据在OHLC中的一个名为“theBars”的变量中,执行此操作
par(usr=c(0,1,最小(theBars),最大(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
这是指在绘制指示器后,最大值略微升高

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

您不需要为没有包含可复制的示例向我们道歉。是的,但我知道,如果我不把它包括在内,我会得到关于它的评论。我编辑了一个例子
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
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
  points.default(x=startHoriz + horizUnit*timeIndex,
                 y= maxRaise * deltaP + max(theBars)-((max(theBars)-as.numeric(dataCol[i]))*(pixelStretch)),
                 cex=dotSize,
                 pch=dotType,
                 col=thecolor)