Problems with Histogram in indicator

 

Hi,

when you program in MQL5 an indicator with an color_histogram and a draw_line, the histogram must be defined after the draw_line.

Is this a feature or a bug :-)


The following very simple code shows the open and close prices as a a histogram and a line, the first sample works

as the histogram is defined after the line.


#property indicator_separate_window

#property indicator_buffers 3
#property indicator_plots   2


// will work as Histogram is defined after Draw_line
#property indicator_label1  "Close"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Blue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1



#property indicator_label2  "Open"
#property indicator_type2   DRAW_COLOR_HISTOGRAM
#property indicator_color2  Blue,Red,Green
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2

double OpenBuffer[];
double CloseBuffer[];
double ColorsBuffer[];

 
int OnInit()
  {
//--- indicator buffers mapping
  SetIndexBuffer(1, OpenBuffer,INDICATOR_DATA);
  SetIndexBuffer(0, CloseBuffer,INDICATOR_DATA);
  SetIndexBuffer(2, ColorsBuffer,INDICATOR_COLOR_INDEX);
   return(0);
  }

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 pos=prev_calculated-1;
   if(pos<0){
       pos=0;
      ArrayInitialize(CloseBuffer,EMPTY_VALUE);
      ArrayInitialize(OpenBuffer,EMPTY_VALUE);
      ArraySetAsSeries(CloseBuffer,false);
      ArraySetAsSeries(OpenBuffer,false);
      ArrayInitialize(ColorsBuffer,EMPTY_VALUE);
     }
   
   for(int i=pos;i<rates_total-1;i++)
     {
     
         CloseBuffer[i]=(double) close[i];
         OpenBuffer[i]=(double) open[i];
         
          ColorsBuffer[i]=1;
          
         if (open[i] < close[i])
            ColorsBuffer[i]=0;
         
         else if (open[i] > close[i])
            ColorsBuffer[i]=1;
        
         else if (open[i] == close[i])
           ColorsBuffer[i]=2;
     }
   return(rates_total);
  }
 
 
  



This will not work: the only change is the order of histogram and draw_line


#property indicator_separate_window

#property indicator_buffers 3
#property indicator_plots   2

 
// replace the property definition above
// will not work as Histogram is before defintion of line

#property indicator_label1  "Open"
#property indicator_type1   DRAW_COLOR_HISTOGRAM
#property indicator_color1  Blue,Red,Green
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2


#property indicator_label2  "Close"
#property indicator_type2   DRAW_LINE
#property indicator_color2  Blue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
 

double OpenBuffer[];
double CloseBuffer[];
double ColorsBuffer[];

 
int OnInit()
  {
//--- indicator buffers mapping
  SetIndexBuffer(1, OpenBuffer,INDICATOR_DATA);
  SetIndexBuffer(0, CloseBuffer,INDICATOR_DATA);
  SetIndexBuffer(2, ColorsBuffer,INDICATOR_COLOR_INDEX);
   return(0);
  }

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 pos=prev_calculated-1;
   if(pos<0){
       pos=0;
      ArrayInitialize(CloseBuffer,EMPTY_VALUE);
      ArrayInitialize(OpenBuffer,EMPTY_VALUE);
      ArraySetAsSeries(CloseBuffer,false);
      ArraySetAsSeries(OpenBuffer,false);
      ArrayInitialize(ColorsBuffer,EMPTY_VALUE);
     }
  
   for(int i=pos;i<rates_total-1;i++)
     {
    
         CloseBuffer[i]=(double) close[i];
         OpenBuffer[i]=(double) open[i];
        
          ColorsBuffer[i]=1;
         
         if (open[i] < close[i])
            ColorsBuffer[i]=0;
        
         else if (open[i] > close[i])
            ColorsBuffer[i]=1;
       
         else if (open[i] == close[i])
           ColorsBuffer[i]=2;
     }
   return(rates_total);
  }
 
 
  

Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants - Documentation on MQL5
 
fari:

Hi,

when you program in MQL5 an indicator with an color_histogram and a draw_line, the histogram must be defined after the draw_line.


This will not work: the only change is the order of histogram and draw_line


All arrays for plots must be assigned to indicators buffers in properly order. Try to do by such way:

int OnInit()
  {
//--- indicator buffers mapping
  SetIndexBuffer(0, OpenBuffer,INDICATOR_DATA);
  SetIndexBuffer(1, ColorsBuffer,INDICATOR_COLOR_INDEX);
  SetIndexBuffer(2, CloseBuffer,INDICATOR_DATA);
   return(0);
  }
Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Indicators Lines
  • www.mql5.com
Standard Constants, Enumerations and Structures / Indicator Constants / Indicators Lines - Documentation on MQL5
 
Also you'd better insert code properly
MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Rosh:

All arrays for plots must be assigned to indicators buffers in properly order. Try to do by such way:



Thank you very much Rosh, this solved my problems.

Reason: