problem with Colored lines with MT4

 

In order to implement two-colored line in MT4 custom indicators, I try to use two buffers, each with a different color.

the code looks something like this:

// if line should be red
{
        if(buffer1[previous_index]==EMPTY_VALUE) //this is to connect empty space between green and red line
                buffer1[previous_index]=buffer2[previous_index];
        buffer1[index]=value[index];
        buffer2[index]=EMPTY_VALUE
}

// if line should be green
{
        if(buffer2[previous_index]==EMPTY_VALUE)
                buffer2[previous_index]=buffer1[previous_index];
        buffer2[index]=value[index];
        buffer1[index]=EMPTY_VALUE
}

The problem is when color switches too fast(as fast as a different color for each consecutive index) the different color in between cannot be shown and one color dominates all.

Do you have a solution for this? I tried using double times buffers to solve this. Although it worked but I don't like making this so complicated.

 
Yashar Seyyedin:

The problem is when color switches too fast(as fast as a different color for each consecutive index) the different color in between cannot be shown and one color dominates all.

Do you have a solution for this? I tried using double times buffers to solve this. Although it worked but I don't like making this so complicated.

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)

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].
 
Yashar Seyyedin:

In order to implement two-colored line in MT4 custom indicators, I try to use two buffers, each with a different color.

the code looks something like this:

The problem is when color switches too fast(as fast as a different color for each consecutive index) the different color in between cannot be shown and one color dominates all.

Do you have a solution for this? I tried using double times buffers to solve this. Although it worked but I don't like making this so complicated.

The issue starts when there are 3 sequential alternating rise or drops 

like DROP , RISE , DROP 

The indicator then correctly assumes it must connect the 2 drops which will occlude the rise if the drop plot is after the rise plot.

The solution is very ugly , unless i'm missing a DRAW type .

(i think it could be done with section if you smash the buffer following a drawn part with empty value , but i've not tested it.Absent any beautiful solutions the best course would be to have little balls on the data points and convey the indicators information by coloring them)

#property strict
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_plots   5
//--- plot rise
#property indicator_label1  "data"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrSilver
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot fall
#property indicator_label2  "rise1"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrYellowGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
//--- plot data
#property indicator_label3  "fall1"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrCrimson
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2
//--- plot fall
#property indicator_label4  "rise2"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrYellowGreen
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2
//--- plot data
#property indicator_label5  "fall2"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrCrimson
#property indicator_style5  STYLE_SOLID
#property indicator_width5  2
//--- indicator buffers
double         rise1Buffer[],rise2[];
double         fall1Buffer[],fall2[];
double         dataBuffer[],riseIX[],fallIX[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,dataBuffer);
   SetIndexBuffer(1,rise1Buffer);
   SetIndexBuffer(2,fall1Buffer);
   SetIndexBuffer(3,rise2);
   SetIndexBuffer(4,fall2);
   SetIndexBuffer(5,riseIX);SetIndexStyle(5,DRAW_NONE,EMPTY,EMPTY,clrNONE);
   SetIndexBuffer(6,fallIX);SetIndexStyle(6,DRAW_NONE,EMPTY,EMPTY,clrNONE);
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(2,0.0);
   SetIndexEmptyValue(3,0.0);
   SetIndexEmptyValue(4,0.0);
   SetIndexEmptyValue(5,0.0);
   SetIndexEmptyValue(6,0.0);
   reset();
//---
   return(INIT_SUCCEEDED);
  }
  void reset(){
  ArrayFill(rise1Buffer,0,ArraySize(rise1Buffer),0.0);
  ArrayFill(fall1Buffer,0,ArraySize(fall1Buffer),0.0);
  ArrayFill(dataBuffer,0,ArraySize(dataBuffer),0.0);
  ArrayFill(rise2,0,ArraySize(rise2),0.0);
  ArrayFill(fall2,0,ArraySize(fall2),0.0);
  ArrayFill(riseIX,0,ArraySize(riseIX),0.0);
  ArrayFill(fallIX,0,ArraySize(fallIX),0.0);  
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
  int items=MathMin((rates_total-prev_calculated),rates_total-3);
  for(int i=items;i>=0;i--){
  dataBuffer[i]=close[i];
  riseIX[i]=riseIX[i+1];
  fallIX[i]=fallIX[i+1];
  //if data 
    if(dataBuffer[i+1]!=EMPTY_VALUE)
    {
    //rise indicates a rise from the previous to this 
      if(dataBuffer[i]>dataBuffer[i+1]){
      riseIX[i]+=1.0;
      if(riseIX[i]==1.0){
      rise1Buffer[i+1]=dataBuffer[i+1];
      rise1Buffer[i]=dataBuffer[i];
      }else{
      rise2[i+1]=dataBuffer[i+1];
      rise2[i]=dataBuffer[i];
      riseIX[i]=0.0;
      }
      }
    //fall 
      else if(dataBuffer[i]<dataBuffer[i+1]){
      fallIX[i]+=1.0;
      if(fallIX[i]==1.0){
      fall1Buffer[i+1]=dataBuffer[i+1];
      fall1Buffer[i]=dataBuffer[i];
      }else{
      fall2[i+1]=dataBuffer[i+1];
      fall2[i]=dataBuffer[i];
      fallIX[i]=0.0;
      }
      }
    }
  } 
//--- return value of prev_calculated for next call
   return(rates_total);
  }

 
Lorentzos Roussos #:

The issue starts when there are 3 sequential alternating rise or drops 

like DROP , RISE , DROP 

The indicator then correctly assumes it must connect the 2 drops which will occlude the rise if the drop plot is after the rise plot.

The solution is very ugly , unless i'm missing a DRAW type .

(i think it could be done with section if you smash the buffer following a drawn part with empty value , but i've not tested it.Absent any beautiful solutions the best course would be to have little balls on the data points and convey the indicators information by coloring them)

I did the same bro. Seems there is no solution.
 
Yashar Seyyedin #:
I did the same bro. Seems there is no solution.

There is always the canvas , but how would it understand that it must not connect the lines even there.

You could deploy a structure with in and out nodes so an out node of a data point cannot connect to another node .

The lines could only be drawn from in to out of the same type . I think a char would suffice for that . But , custom drawing

Reason: