how to draw an histogram

 

if some one can help me out with this

i'm trying to draw an histogram like this

& so far without succes

legend:
green = above 0 & above signal
blue = above 0 but below signal
orange = below 0 but above signal
red = below 0 & below signal

//+------------------------------------------------------------------+
//|                                               Macd_Histogram.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."

#property indicator_separate_window 
#property indicator_buffers 4

#property indicator_color1 Green
#property indicator_color2 Blue
#property indicator_color3 Orange
#property indicator_color4 Red
               

string    indName="Macd_Hitogram";

//---- buffers
double ExtMapBufGreen[];
double ExtMapBufBlue[];
double ExtMapBufOrange[];
double ExtMapBufRed[];

double macd[],signal[],histogram[];

extern double FastEma = 12;
extern double SlowEma = 26;
extern double Signal  = 9;

int FixZeroD=0;
int cnt = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,3);

   SetIndexBuffer(0,ExtMapBufGreen);
   SetIndexBuffer(1,ExtMapBufBlue);
   SetIndexBuffer(2,ExtMapBufOrange);
   SetIndexBuffer(3,ExtMapBufRed);



   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));      
   
        
   //----
   return(0);
  } 

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()                         
  {
  
   int i,Counted_bars;                 

   Counted_bars=IndicatorCounted();  
   i=Bars-Counted_bars-1; 
   if(Counted_bars == 0) 
   double sum=0;

//-------

    

   for(int k = 0; k < i; k++)
   macd[k]      = iMA(NULL,0,FastEma,0,MODE_EMA,PRICE_CLOSE,k)-iMA(NULL,0,SlowEma,0,MODE_EMA,PRICE_CLOSE,k);
   for(k=0; k < i; k++)
   signal[k]    = iMAOnArray(macd,Bars,Signal,0,MODE_SMA,k);
   for(i=0; i < i; i++)
   histogram[k] = macd[k] - signal[k];
//-------
              
   while(i>=0)                      
      {
      ExtMapBufGreen[i]  = ;
      ExtMapBufBlue[i]   = ;
      ExtMapBufOrange[i] = ;
      ExtMapBufRed[i]    = ;
      i--;
      }
     

//--------------------------------------------------------------------
   return;                          
  }

can someone tell me how to go on from here ?

thanks

 

Qjol, drawing of one histogram requires two consecutive index buffers. You need to flip-flop their values to change colors. Good luck.  

#property indicator_separate_window 
#property indicator_buffers 2

#property indicator_color1 Green
#property indicator_color2 Blue
#property indicator_minimum 0
#property indicator_maximum 1
               
double ExtMapBufGreen[];
double ExtMapBufRed[];

int init()
{
   SetIndexBuffer(0,ExtMapBufGreen);
   SetIndexBuffer(1,ExtMapBufRed);
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3,Green);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,3,Red);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)); 
   
   return(0);
} 
int start()                         
{
   int limit,Counted_bars;                 
   Counted_bars=IndicatorCounted();  
   limit=Bars-Counted_bars-1; 

   for(int k = 0; k < limit; k++)
   {
      ExtMapBufGreen[k] = EMPTY_VALUE;
      ExtMapBufRed[k] = EMPTY_VALUE;
      
      if (Close[k] > Open[k]) {
         ExtMapBufGreen[k] = 1.0;
         ExtMapBufRed[k] = 0.0;
      }
      else {
         ExtMapBufGreen[k] = 0.0;
         ExtMapBufRed[k] = 1.0;
      }
   }
   return;                          
}
 

thanks a lot here is the code

legend:
green = above 0 & above signal
blue = above 0 but below signal
orange = below 0 but above signal
red = below 0 & below signal

//+------------------------------------------------------------------+
//|                                               Macd_Histogram.mq4 |
//|                                         Copyright © 2010, NADAV. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, NADAV."

#property indicator_separate_window 
#property indicator_buffers 4

#property indicator_color1 Green
#property indicator_color2 Blue
#property indicator_color3 Orange
#property indicator_color4 Red
               

string    indName="Macd_Hitogram";

//---- buffers
double ExtMapBufGreen[];
double ExtMapBufBlue[];
double ExtMapBufOrange[];
double ExtMapBufRed[];

extern double FastEma = 12;
extern double SlowEma = 26;
extern double Signal  = 9;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3,Green);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,3,Blue);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,3,Orange);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,3,Red);

   SetIndexBuffer(0,ExtMapBufGreen);
   SetIndexBuffer(1,ExtMapBufBlue);
   SetIndexBuffer(2,ExtMapBufOrange);
   SetIndexBuffer(3,ExtMapBufRed);



   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));      
   
        
   //----
   return(0);
  } 

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()                         
  {
  
   int limit,Counted_bars;                 
   Counted_bars=IndicatorCounted();  
   limit=Bars-Counted_bars-1; 

//------- 

   for(int k = 0; k < limit; k++)
      {
      ExtMapBufGreen[k]  = EMPTY_VALUE;
      ExtMapBufBlue[k]   = EMPTY_VALUE;
      ExtMapBufOrange[k] = EMPTY_VALUE;
      ExtMapBufRed[k]    = EMPTY_VALUE;
      
      if (iMACD(NULL, 0, FastEma, SlowEma, Signal,PRICE_CLOSE, MODE_MAIN,k) > iMACD (NULL, 0, FastEma, SlowEma, Signal, PRICE_CLOSE, MODE_SIGNAL,k)
         && iMACD (NULL, 0, FastEma, SlowEma, Signal, PRICE_CLOSE, MODE_SIGNAL,k) > 0)
         {
         ExtMapBufGreen[k]  = 1.0;
         ExtMapBufBlue[k]   = 0.0;
         ExtMapBufOrange[k] = 0.0;
         ExtMapBufRed[k]    = 0.0;
         }
      else if (iMACD(NULL, 0, FastEma, SlowEma, Signal,PRICE_CLOSE, MODE_MAIN,k) < iMACD (NULL, 0, FastEma, SlowEma, Signal, PRICE_CLOSE, MODE_SIGNAL,k)
         && iMACD (NULL, 0, FastEma, SlowEma, Signal, PRICE_CLOSE, MODE_SIGNAL,k) > 0)
         {
         ExtMapBufGreen[k]  = 0.0;
         ExtMapBufBlue[k]   = 1.0;
         ExtMapBufOrange[k] = 0.0;
         ExtMapBufRed[k]    = 0.0;
         }
      else if (iMACD(NULL, 0, FastEma, SlowEma, Signal,PRICE_CLOSE, MODE_MAIN,k) > iMACD (NULL, 0, FastEma, SlowEma, Signal, PRICE_CLOSE, MODE_SIGNAL,k)
         && iMACD (NULL, 0, FastEma, SlowEma, Signal, PRICE_CLOSE, MODE_SIGNAL,k) < 0)
         {
         ExtMapBufGreen[k]  = 0.0;
         ExtMapBufBlue[k]   = 0.0;
         ExtMapBufOrange[k] = 1.0;
         ExtMapBufRed[k]    = 0.0;
         }
      else if (iMACD(NULL, 0, FastEma, SlowEma, Signal,PRICE_CLOSE, MODE_MAIN,k) < iMACD (NULL, 0, FastEma, SlowEma, Signal, PRICE_CLOSE, MODE_SIGNAL,k)
         && iMACD (NULL, 0, FastEma, SlowEma, Signal, PRICE_CLOSE, MODE_SIGNAL,k) < 0)
         {
         ExtMapBufGreen[k]  = 0.0;
         ExtMapBufBlue[k]   = 0.0;
         ExtMapBufOrange[k] = 0.0;
         ExtMapBufRed[k]    = 1.0;
         }
      }

//--------------------------------------------------------------------
   return;                          
  }
Files:
 

Thanks

Helped me 11 years after release  :)

 
thanks. It helped me a lot too
Reason: