Stacked histogram

 

Hello,

I have a very simple histogram indicator displayed in a subwindow. It displays fine unless val2 > val1 then the val2 histogram 'covers' the val1 indicator colour.

Is there a simple fix to this? Is there a way to send val2 colour to the background so that both histogram colours show on this condition?


#property indicator_separate_window
#property indicator_buffers 2
#property indicator_minimum 0

#property indicator_color1 Lime
#property indicator_color2 Red


double val1[];
double val2[];
double ndigit;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   SetIndexBuffer(0,val1);
   SetIndexBuffer(1,val2);
  
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   if(Digits == 4 || Digits == 5)ndigit = 0.0001;       
   else if(Digits == 3 || Digits == 2)ndigit = 0.01;
   
   
   return(0);
  }


int start()
  {

   int i,counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   

//----
   for(i=0; i<limit; i++)
   {
      val1[i]=(Volume[i]+(Close[i]-Open[i])/ndigit)/2;
      val2[i]=(Volume[i]-val1[i]);
   }
   
     

   return(0);
  }
 
I strongely suggest to draw one of them as a line, so the visual impact will be clearer.
 
sd59 #:

I don't really like that format.

I think I can get the format I want by using 4 buffers and then using a reverse indices order if val2 > val1 but this seems a bit over the top for a simple indicator.

Is this a limitation or am I missing a much simpler solution?

You can change order of drawing of the buffers by their indexing in SetIndexBuffer, but some buffers will always overlap the others (depending from their values on corresponding bars), so your solution is to use different width for lines, so that thinner histograms can look through or above thicker ones.

 
You can choose DRAW_LINE and DRAW_HISTOGRAM instead of having them both as histogram.
 
Yashar Seyyedin #:
You can choose DRAW_LINE and DRAW_HISTOGRAM instead of having them both as histogram.

thansk guys I solved it using 3 buffers.


 SetIndexBuffer(0,val1);
 SetIndexBuffer(1,val2);
 SetIndexBuffer(2,temp);

 SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
 SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
 SetIndexStyle(2,DRAW_HISTOGRAM,0,2);
 
 SetIndexEmptyValue(0,0.0);
 SetIndexEmptyValue(1,0.0);
 SetIndexEmptyValue(2,0.0);

 for(i=0; i<limit; i++)
  {
   temp[i] = 0;
   val1[i]=(Volume[i]+(Close[i]-Open[i])/ndigit)/2;
   val2[i]=(Volume[i]-val1[i]);
   if(val2[i] > val1[i]){temp[i] = val1[i];val1[i] = 0;}
  }