2015-08-19 108 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

相關問題