2015-08-19 107 views
3

我有一个包含516行和2列的矩阵(名为ichimoku),每个矩阵都包含要绘制的值,目标是重新创建Ichimoku strategy的云。 使用matpot,我可以绘制这两条曲线,但我想要的是遮蔽两条曲线之间的区域。我有两个问题:R - 两条不同颜色交叉线之间的阴影区域

  • 我尝试使用多边形阴影区域,但它不起作用。我怀疑这是因为这两个系列(senkouA和senkouB)在情节上交叉了几次而不是总是大于另一个

  • 我想这个区域在绿色的情况下用绿色表示,如果senkouA> senkouB和红色如果senkouB> senkouA,但从我读的多边形只能是一种颜色。

是否有其他功能多边形这可能会帮助我实现我所期待的,那就是在senkouA和senkouB之间的绿色阴影区时senkouA> senkouB和红色阴影区时senkouB> senkouA ?

的一目均衡表等矩阵如下所示(第一列是senkouA,其他senkouB)

 [,1]  [,2] 
[1,] 23323.62 23320.53 
[2,] 23334.67 23328.71 
[3,] 23334.11 23323.06 
[4,] 23332.94 23323.06 
... 

这里是我的matplot功能(工作):

matplot(ichimoku,lty=1,lwd=1,pch=20,type="l",col=c("red","blue")) 

和我的多边形功能(其中没有):

polygon(c(1:516,516:1),c(senkouA,senkouB),col='green') 
+0

你看看'quantmod'包? –

+0

@帕斯卡不幸的是,我不认为'quantmod'包含了Ichimoku图表。但[这个]博客可能会很有趣。 – RHertel

+0

还有一个[github帖子](https://github.com/IlyaKipnis/IKTrading/issues/3)在Ichimoku指标 – RHertel

回答

3

如果您发现该曲线之间的交叉点,那么你就可以得出交叉口之间的多边形。这里是对之前的post的修改,他们发现曲线之间的交点,以及绘制多边形的函数。

## Some sample data 
set.seed(0) 
dat <- data.frame(x1=3*sin(3*(x=seq(0,10,len=100)))+rnorm(100), 
        x2=2*cos(x)+rnorm(100)) 

## https://stackoverflow.com/questions/20519431/finding-point-of-intersection-in-r 
intersects <- function(x1, x2) { 
    seg1 <- which(!!diff(x1 > x2))       # location of first point in crossing segments 
    above <- x2[seg1] > x1[seg1]        # which curve is above prior to crossing 
    slope1 <- x1[seg1+1] - x1[seg1] 
    slope2 <- x2[seg1+1] - x2[seg1] 
    x <- seg1 + ((x2[seg1] - x1[seg1])/(slope1 - slope2)) 
    y <- x1[seg1] + slope1*(x - seg1) 
    data.frame(x=x, y=y, pindex=seg1, pabove=(1:2)[above+1L]) # pabove is greater curve prior to crossing 
} 

ichimoku <- function(data, addLines=TRUE) { 
    ## Find points of intersections 
    ints <- intersects(data[,1], data[,2]) 
    intervals <- findInterval(1:nrow(data), c(0, ints$x)) 

    ## Make plot 
    matplot(data, type="n", col=2:3, lty=1, lwd=4) 
    legend("topright", c("A", "B"), col=3:2, lty=1, lwd=2) 

    ## Draw the polygons 
    for (i in seq_along(table(intervals))) { 
     xstart <- ifelse(i == 1, 0, ints$x[i-1]) 
     ystart <- ifelse(i == 1, dat[1,ints$pindex[1]], ints$y[i-1]) 
     xend <- ints$x[i] 
     yend <- ints$y[i] 
     x <- seq(nrow(data))[intervals == i] 
     polygon(c(xstart, x, xend, rev(x)), c(ystart, data[x,1], yend, rev(data[x,2])), 
       col=ints$pabove[i]%%2+2) 
    } 

    ## Add lines for curves 
    if (addLines) 
     invisible(lapply(1:2, function(x) lines(seq(nrow(data)), data[,x], col=x%%2+2, lwd=2))) 
} 

## Plot the data 
ichimoku(dat) 

enter image description here

+0

我工作很好,除了一些问题:我在ints df的第一个pindex等于63(然后是98和105),所以第一个ystart不工作,因为有在dat中只有两列,而不是63.我将ints $ pindex [1]替换为2,并且除了最后一个之外,它会抛出多边形。感谢您的发布! – etienne

1

下面是一些代码,适用于您的问题的简单版本时间线,其中线只穿过一次。然而,我没有测试过它的重复过境。

# Make toy data 
ichimoku <- data.frame(senkouA = rep(10, 10), senkouB = c(3, 5, 4, 7, 10, 11, 15, 12, 13, 14)) 

# Make indices for the conditions that define the fill colors. They need to intersect for the polygons to connect. 
index.green = with(ichimoku, as.logical(senkouA >= senkouB)) 
index.red = with(ichimoku, as.logical(senkouA <= senkouB)) 

# Make the line plot 
matplot(ichimoku, lty=1, lwd=1, pch=20, type="l", col=c("red","blue")) 

# Now add polygons with fill color based on those conditions by subsetting the task using the indices. 
with(ichimoku, polygon(x = c(seq(length(senkouA))[index.green], rev(seq(length(senkouA))[index.green])), 
    y = c(senkouB[index.green], senkouA[index.green]), col = "green")) 
with(ichimoku, polygon(x = c(seq(length(senkouA))[index.red], rev(seq(length(senkouA))[index.red])), 
    y = c(senkouB[index.red], senkouA[index.red]), col = "red")) 

这里是我的结果:

enter image description here

+0

感谢显示如何做到这一点,我将继续寻找重复的过境点 – etienne

相关问题