Draw Histogram in Color

 

Hello,


i tried to get a Histogram with 2 colors but i dont find out, how to became the second color.

//+------------------------------------------------------------------+
//|                                                 MACrossHisto.mq5 |
//|                                                           amando |
//|                                                                  |
//+------------------------------------------------------------------+


#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot Diff
#property indicator_label1  "MA Cross Histo"
#property indicator_type1   DRAW_HISTOGRAM

//#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
#property indicator_color1  clrGreen, clrRed
//--- indicator buffers

int Hdl_MA1, Hdl_MA2;
double DiffBuffer[];
double DiffBufferColor[];



input ENUM_APPLIED_PRICE Price_MA1 = PRICE_CLOSE;
input ENUM_APPLIED_PRICE Price_MA2  = PRICE_OPEN;

input ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT;

input int Periode_MA1 = 5;
input int Periode_MA2 = 5;

input ENUM_MA_METHOD Mode_MA1 = MODE_LWMA;
input ENUM_MA_METHOD Mode_MA2 = MODE_LWMA;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,DiffBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,DiffBufferColor,INDICATOR_COLOR_INDEX);

//Specify colors for each index
   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,2);
  // PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,clrGreen);
   //PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,clrRed);

   Hdl_MA1 = iMA(_Symbol,Timeframe,Periode_MA1,0,Mode_MA1,Price_MA1);
   Hdl_MA2 = iMA(_Symbol,Timeframe,Periode_MA2,0,Mode_MA2,Price_MA2);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
  
  
//---
   double MA1[],MA2[];
   int limit=rates_total-prev_calculated,
       count=(limit>1 ? rates_total : 1),
       cop1=0, cop2=0;

   cop1 = CopyBuffer(Hdl_MA1,0,0,count,MA1);    // MA1
   cop2 = CopyBuffer(Hdl_MA2,0,0,count,MA2);    // MA2

   if(cop1!=count || cop2!=count)
      return 0;



   for(int i=prev_calculated; i<=rates_total-1; i++)
     {

      DiffBuffer[i] = MA1[i] - MA2[i];             // Calculation difference


      if(DiffBuffer[i] > 0)
        {
         DiffBufferColor[i] = 0;
        }      //Assign color with index=zero (0)
      else
        {
         DiffBufferColor[i] = 1;
        }       //Assing color with index=one (1)

      //DiffBuffer[i] = MA1[i] - MA2[i];
     }


//--- return value of prev_calculated for next call
   return(rates_total-1);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+


everthing works, beside of the second color, can someone give me a hint?


regards

 

Set the 

#property indicator_type1   DRAW_HISTOGRAM

to

#property indicator_type1   DRAW_COLOR_HISTOGRAM
 
Lorentzos Roussos #:
DRAW_COLOR_HISTOGRAM

f... that was easy, thank you

 
amando #:

f... that was easy, thank you

😊  here is an indicator with all draw types , it may be a helpflu reference

Files:
 

another question to that,

why i get here

DiffBuffer[i] = MA1[i] - MA2[i]; 

sometimes an array out of range?

 
amando #:

another question to that,

why i get here

sometimes an array out of range?

The problem on that Error, if I print the DiffBuffer first the Error moves from pos 13 to pos 18

 
amando #:

another question to that,

why i get here

sometimes an array out of range?

Check the indices with the debugger

or look at ...\MQL5\Indicators\Examples\MACD.mq5

There you find the same:

...
   int start;
   if(prev_calculated==0)
      start=0;
   else
      start=prev_calculated-1;
//--- calculate MACD
   for(int i=start; i<rates_total && !IsStopped(); i++)
      ExtMacdBuffer[i]=ExtFastMaBuffer[i]-ExtSlowMaBuffer[i];