Connecting a moving average with different colors

 

Hi,


I've got an indicator that plots a moving average channel, using the high and low values. I've attached a screenshot of it. On the actual chart, the channel is green if both upper and lower lines are going up, red if they are both going down, and grey otherwise.

On that same screenshot, at the bottom I've attached another indicator, to help explain my issue. It is the same kind of indicator, except instead of plotting the actual channel on the chart, it just informs if the channel is rising, falling, or sideways (green, red, or gray). Both indicators show the same information, in different ways.

The issue is, when there are 1 bar color change, the channel on the chart sometimes doesn't plot them in the correct color. I've pointed out an occurrence of that issue with the blue arrow. The channel is supposed to show on one bar that it has turned red, except here it stays gray. When comparing the channel with the lower indicator, both should show the same color for each bar, red green or gray. Except sometimes it doesn't happen that way.

This is because of how the arrays are set. I've got 6 arrays:

double UpperGreen[];
double UpperGray[];
double UpperRed[];
double LowerGreen[];
double LowerGray[];
double LowerRed[];

And on each bar, I assign the high and low value to the corresponding array (color).

In case of a bar where the channel is rising, it gives this:

UpperGreen[i]=upper1;
UpperGreen[i+1]=upper2;
UpperRed[i]=EMPTY_VALUE;
UpperGray[i]=EMPTY_VALUE;
LowerGreen[i]=lower1;
LowerGreen[i+1]=lower2;
LowerRed[i]=EMPTY_VALUE;
LowerGray[i]=EMPTY_VALUE;

The values upper1 and so on are the values of the corresponding channel. I have to assign a value to the preceding bar (in this case, previous bar to assign a value to the green array), or else the lines never connect.


Now to the heart of the issue: When there is a single color for the channel on one particular bar, in the screenshot it's one red between two grays, the red isn't plotted. Or rather, as soon as the next bar (gray) is plotted, the red channel is overwritten and turns to gray instead.

And this is related to the order in which I set my buffers in the OnInit() function. In my case, I have declared them in this order: Green, red, and gray. So that is why in my example, the gray channel takes precedence and colors over the red.

If I change the order in the OnInit(), and say put the red buffers last, then the red channel will be plotted correctly on that particular bar, but I will have issues on other bars and other colors instead.

So in essence, is there a workaround around that type of behavior? 


Thanks,

Files:
Channel.PNG  16 kb
 

Same as any colored line indicator. Look in the CodeBase. If you assign to one color buffer, make the other color buffer(s) EMPTY_VALUE. Then connect to the previous bar.
          HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 programming forum 2016.07.09

For MT4 I use:

  1. One buffer has the value, color set to CLR_NONE so not shown on chart, (but in data window and pop up.)
  2. Two buffers, one color each, with SetIndexLabel(i, NULL) so they don't show in data window.
  3. Then you need to connect the lines on color change. downBuffer[i]=value[i]; if(downBuffer[i+1]==EMPTY_VALUE) downBuffer[i+1]=value[i].
 
William Roeder:

Same as any colored line indicator. Look in the CodeBase. If you assign to one color buffer, make the other color buffer(s) EMPTY_VALUE. Then connect to the previous bar.
          HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 programming forum 2016.07.09

For MT4 I use:

  1. One buffer has the value, color set to CLR_NONE so not shown on chart, (but in data window and pop up.)
  2. Two buffers, one color each, with SetIndexLabel(i, NULL) so they don't show in data window.
  3. Then you need to connect the lines on color change. downBuffer[i]=value[i]; if(downBuffer[i+1]==EMPTY_VALUE) downBuffer[i+1]=value[i].

Hi William, thanks for taking the time to respond.

- I have now added 2 buffers, for the upper and lower values. It helps keep the data window a bit clearer with less values. It also is a bit more efficient, since for previous bar I can now reference that buffer, instead of recalculating the iMA value.

- As for connecting the lines, if you look at my previous code example it's similar to what I was doing. Except, I was hard-calculating the previous bar regardless, so now I've added the condition in case of a color change. I now have (example of bullish channel):

UpperGreen[i]=upper0;         
UpperRed[i]=EMPTY_VALUE;
UpperGray[i]=EMPTY_VALUE;
LowerGreen[i]=lower0;         
LowerRed[i]=EMPTY_VALUE;
LowerGray[i]=EMPTY_VALUE;
if(UpperGreen[i+1]==EMPTY_VALUE)
   {
   UpperGreen[i+1]=upper1;
   LowerGreen[i+1]=lower1;
   }


But, the same issue still resides. And it's down to how MT4 displays lines, so to try and be more explicit I have attached 3 screenshots of the same chart, but with different steps. If you don't mind having a look because it's easier than to try and explain the behavior in text.

My observation (and problem) is that, when you connect with the previous bar when the previous value is EMPTY, it overrides in case of single color change. It's never an issue when you get multiple bars, like G, G, R, R, R. But it is when you get single bar colors like R,R, G, R. The reason is explained on my 3 screenshots.


Thanks,

Files:
Example1-.png  14 kb
Example2-.png  17 kb
Example3-.png  25 kb
 
Barraka: But it is when you get single bar colors like R,R, G, R. The reason is explained on my 3 screenshots.

That is a limitation of MT4. You colored R, R & G, R & G, R. So it drew both lines and the red covered the green (ordering of the buffers).

The only way to fix it would be multiple buffers per color. R1, R1 & G1, R2 & G1, R2. Hardly worth the trouble.

 
William Roeder:

That is a limitation of MT4. You colored R, R & G, R & G, R. So it drew both lines and the red covered the green (ordering of the buffers).

The only way to fix it would be multiple buffers per color. R1, R1 & G1, R2 & G1, R2. Hardly worth the trouble.


Ok, that's what I thought but wanted to be sure I wasn't missing something. Totally agree, I'll stick with what I have, the issue is more of a minor nuisance than anything else.


Cheers,