Color two buffers alternately

 

Hi,

I have two buffers which I would like to color depending on a condition. In the following simple example the condition is on the close price.

The script seems to work but I have an issue when, between two candles, the price change direction (see image attached).

Note that the problem doesn't exist when the price takes more than two candles to change direction.


I try to explain the issue:

* suppose a trend up-down-up /\/ where the vertices are called a-b-c-d;

* the two buffers will be something like this:

upBuffer = {a, b, c, d}

dnBuffer = { , b, c,  }

MT4 simply connects all the adjacent points that are present in the buffers and if there is an EMPTY_VALUE it leaves a gap.


The question is how can I specify that I want connect a-b and c-d?

If it's not possible, should i connect all the point a-b-c-d, with a color, and then  repaint only the others a-b-c-d?

#property strict
#property indicator_chart_window

#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 2

double upBuffer[];
double dnBuffer[];

int OnInit() {
  SetIndexBuffer(0, upBuffer);
  SetIndexStyle(0, DRAW_LINE);
  SetIndexDrawBegin(0, 0);
  
  SetIndexBuffer(1, dnBuffer);
  SetIndexStyle(1, DRAW_LINE);
  SetIndexDrawBegin(1, 0);
  

  
  return(INIT_SUCCEEDED);
}

int OnCalculate(const int tot_bars,
                const int prev_tot_bars,
                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 limit = tot_bars - prev_tot_bars;

  for (int i = 0; i < limit - 1; i++) {  // from right to left of the chart
    if (close[i] > close [i + 1]) {
      upBuffer[i] = close[i];
      upBuffer[i + 1] = close[i + 1];
    }
    else if (close[i] < close [i + 1]) {
      dnBuffer[i] = close[i];
      dnBuffer[i + 1] = close[i + 1];
    }
  }
  
  return(tot_bars);
}
Files:
 
dcstoyanov: The question is how can I specify that I want connect a-b and c-d?

If it's not possible, should i connect all the point a-b-c-d, with a color, and then  repaint only the others a-b-c-d?

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].

MT4 only draws lines between candles. Therefor, using the above, if only bc is red, it can not be done; bc will be continuous green.(b is the ending green and c is the starting green, thus continuous.)

The alternative is not to connect ab, then there will be a gap ab and a red bc.
          HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 programming forum (2016)

On MT5 you can place end points between lines if you Check Tools → Options (control+O) → Charts → precise time scale
          Is it possible not to snap to the grids? - MQL5 programming forum #5 (2018)