change the color of a buffer in OnCalculate

 

Hello


is there a way to change the color even the style of a buffer in OnCalculate, after it has been initialized in OnInit?

especially  without  introducing new buffers

 

Hi, 

You cannot change the color without introducing new buffers.

But if you plan to change the color of the entire indicator in OnCalculate event, you may consider reset the values in first buffer to EMPTY_VALUES and reassign the first buffer values to the second one.

A sample workaround for your reference:

extern bool isBlue =True;

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red

double a[];
double b[];
int OnInit()
  {

   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   
   SetIndexBuffer(0, a);
   SetIndexBuffer(1, b);
   
   SetIndexLabel(0, "This is Blue");
   SetIndexLabel(1, "This is Red");
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   if (isBlue){
      for (int i = 0; i < ArraySize(a); i ++){
         b[i] = a[i];
         a[i] = EMPTY_VALUE
         }
      }
   else {
      for (int i = 0; i < ArraySize(a); i ++){
         a[i] = b[i];
         b[i] = EMPTY_VALUE
         }
      }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

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