I have a matrix (named ichimoku) with 516 rows and 2 columns ,each one of them containing values to be plotted, the goal is to recreate the clouds for the Ichimoku strategy. Using matpot, I am able to plot these two curves but what I want is to shade the area between the two curves. I have two problems :
I tried using polygon to shade the area but it does not work. I suspect it is because the two series (senkouA and senkouB) cross several times on the plot instead of having one always greater than the other
I would like the area to be shaded in green if senkouA>senkouB and in red if senkouB>senkouA but from what I read the polygon can only be of one color.
Is there an other function that polygon which might help me achieve what I am looking for, that is a shade area in green between senkouA and senkouB when senkouA>senkouB and a shade area in red when senkouB>senkouA ?
The ichimoku matrix looks like this (the first column is senkouA, the other senkouB)
[,1] [,2]
[1,] 23323.62 23320.53
[2,] 23334.67 23328.71
[3,] 23334.11 23323.06
[4,] 23332.94 23323.06
...
here is my matplot function (which works):
matplot(ichimoku,lty=1,lwd=1,pch=20,type="l",col=c("red","blue"))
and my polygon function (which doesn't):
polygon(c(1:516,516:1),c(senkouA,senkouB),col='green')
If you find the intersections between the curves, then you can draw the polygons between the intersections. Here is a modification of a previous post where they find intersections between curves, and a function to draw the polygons.
Here is some code that works for a simple version of your problem, in which the lines only cross once. I haven't tested it for repeated crossings, however.
Here's my result: