//+------------------------------------------------------------------+
//|                                                Master_Candle.mq4 |
//|                                                         Zen_Leow |
//|               Modified by EasyRyder to utlize Average_Price_Bars | 
//+------------------------------------------------------------------+
#property copyright "Zen_Leow"
#property link      ""

#property indicator_chart_window

extern int MinEngulfCandles = 1;
extern color TopLineColor = SpringGreen;
extern color BottomLineColor = Red;
extern int LineWidth = 1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
   int    obj_total=ObjectsTotal();
   string name, topLine, bottomLine;
   topLine = Symbol()+"_"+Period()+"_MasterTop_";
   bottomLine = Symbol()+"_"+Period()+"_MasterBottom_";
   for(int i=obj_total-1; i>=0; i--)
   {
      name=ObjectName(i);
      if (StringFind(name,topLine,0) != -1 || StringFind(name,bottomLine,0) != -1)
      {
         ObjectDelete(name);
      }
   }
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i;                           // Bar index       
   int Counted_bars;                // Number of counted bars
   //--------------------------------------------------------------------   
   Counted_bars=IndicatorCounted(); // Number of counted bars   
   i=Bars-Counted_bars-1;           // Index of the first uncounted   
   
   // always recount the latest possible location for a master candle to be formed
   if (i == 0)
   {
      i = MinEngulfCandles+1; 
   }
   while(i>MinEngulfCandles)                      // Loop for uncounted bars     
   {      
      if (isMasterCandle(i))
      {
         DrawLines(i);
      }
      i--;
   }
//----
   return(0);
}

bool isMasterCandle(int index)
{
   double CandleOpen = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", 2, index);
   double CandleClose = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", 3, index);
   int regLow = 1;
   int regHigh = 0;
   if (CandleClose > CandleOpen) {regHigh = 1; regLow = 0;}
   double CandleTop = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", regHigh, index);
   double CandleBottom = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", regLow, index);
   
   for (int h = index-1; h >= index - MinEngulfCandles; h--)
   {
      CandleOpen = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", 2, h);
      CandleClose = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", 3, h);
      regLow = 1;
      regHigh = 0;
      if (CandleClose > CandleOpen) {regHigh = 1; regLow = 0;}
      double high = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", regHigh, h);
      double low = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", regLow, h);
      if (high > CandleTop || low < CandleBottom)
      {
         return (false);
      }
   }
   
   return (true);
}

void DrawLines(int index)
{
   double CandleOpen = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", 2, index);
   double CandleClose = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", 3, index);
   int regLow = 1;
   int regHigh = 0;
   if (CandleClose > CandleOpen) {regHigh = 1; regLow = 0;}
   double high = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", regHigh, index);
   double low = iCustom(Symbol(), 0, "Synergy_APB", "", "", "", regLow, index);
   string TopName = Symbol()+"_"+Period()+"_MasterTop_" + Time[index];
   ObjectCreate(TopName, OBJ_TREND, 0, Time[index], high, Time[index - MinEngulfCandles], high);
   ObjectSet(TopName, OBJPROP_RAY, false);
   ObjectSet(TopName, OBJPROP_WIDTH, LineWidth);
   ObjectSet(TopName, OBJPROP_COLOR, TopLineColor);
   
   string BottomName = Symbol()+"_"+Period()+"_MasterBottom_" + Time[index];
   ObjectCreate(BottomName, OBJ_TREND, 0, Time[index], low, Time[index - MinEngulfCandles], low);
   ObjectSet(BottomName, OBJPROP_RAY, false);
   ObjectSet(BottomName, OBJPROP_WIDTH, LineWidth);
   ObjectSet(BottomName, OBJPROP_COLOR, BottomLineColor);
}
//+------------------------------------------------------------------+