//+------------------------------------------------------------------+
//|                                             CandleAggregator.mq4 |
//|                                                   David Louisson |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "David Louisson"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 LimeGreen
#property indicator_color3 Red
#property indicator_color4 LimeGreen
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 5
#property indicator_width4 5

extern int AggrFactor    = 2;         // how many candles to aggregate into a single candle
extern int TimeShift     = 0;         // which component candle to begin the aggregation process
extern bool AggrBackward = false;     // forward: disp agg over left candle in group; backward: disp agg over right candle
extern bool InterCandles = false;     // true: show all intervening candles
extern bool SwitchOff    = false;     // true: allows you to toggle indicator off without having to delete it from the chart

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int CountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
   TimeShift = MathMod(TimeShift,AggrFactor);
  
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, Red);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, LimeGreen);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, 5, Red);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, 5, LimeGreen);
   SetIndexBuffer(3, ExtMapBuffer4);
//----
   SetIndexDrawBegin(0,10);
   SetIndexDrawBegin(1,10);
   SetIndexDrawBegin(2,10);
   SetIndexDrawBegin(3,10);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);

   ArrayInitialize(ExtMapBuffer1,EMPTY_VALUE);
   ArrayInitialize(ExtMapBuffer2,EMPTY_VALUE);
   ArrayInitialize(ExtMapBuffer3,EMPTY_VALUE);
   ArrayInitialize(ExtMapBuffer4,EMPTY_VALUE);

//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i, p1, p2;
   double aggOpen, aggHigh, aggLow, aggClose;
   
   if(Bars<=10) return(0);
   CountedBars=IndicatorCounted();
//---- check for possible errors
   if (CountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (CountedBars>0) CountedBars--;
   int pos=Bars-CountedBars-1;
   while(pos>=0)
     {
      p2 = pos+AggrFactor-1;                                // leftmost candle in group to be aggregated
      if (TimeShift > 0 && pos == 0)  p2 = TimeShift-1;     // adjust for timeshift (current candle only)
      aggOpen  = Open[p2];                                  
      aggClose = Close[pos]+Point/5;                        // add 0.2 so that horiz line gets drawn for doji (force open != close)
      aggHigh  = 0;                                         // initialize with low value
      aggLow   = 9999;                                      // initialize with high value
      for (i=pos;i<=p2;i++)                                 // find highest and lowest price across candles to be aggregated
      {
        aggHigh=MathMax(High[i],aggHigh);
        aggLow=MathMin(Low[i],aggLow);
      }
      if ((MathMod(pos,AggrFactor) == TimeShift || pos == 0 || InterCandles == true) && SwitchOff == false)
      {
        p1 = p2;                                            // p2 = leftmost candle in group
        if (AggrBackward == true) p1 = pos;                 // pos = rightmost candle in group
        if (aggOpen<aggClose) 
          {
           ExtMapBuffer1[p1]=aggLow;
           ExtMapBuffer2[p1]=aggHigh;
          } 
        else
          {
           ExtMapBuffer1[p1]=aggHigh;
           ExtMapBuffer2[p1]=aggLow;
          } 
        ExtMapBuffer3[p1]=aggOpen;
        ExtMapBuffer4[p1]=aggClose;
      }
 	   pos--;
 	   
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+