//+------------------------------------------------------------------+
//|                                             monster-breakout.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_maximum 3.5
#property indicator_minimum -0.5
#property indicator_buffers 8
#property indicator_color1  clrDarkGreen
#property indicator_color2  clrMaroon
#property indicator_color3  clrLime
#property indicator_color4  clrRed
#property indicator_color5  clrDeepSkyBlue
#property indicator_color6  clrYellow
#property indicator_color7  clrDarkGreen
#property indicator_color8  clrMaroon

extern ENUM_TIMEFRAMES   TimeFrame               = PERIOD_CURRENT;
input int                period1                 = 12;
input int                ma1                     = 12;
input int                period2                 = 50;
input int                ma2                     = 12;
input int                period3                 = 200;
input int                ma3                     = 12;
input int                period4                 = 800;
input int                ma4                     = 12;

double hi, li, ci, value_of_monster;
string indicatorFileName;

double B0[], B1[], B2[], B3[], B4[], B5[], B6[], B7[];
double Bdata1[],  Bdata2[],  Bdata3[],  Bdata4[];
double Bhighest1[], Bhighest2[], Bhighest3[], Bhighest4[];
double Blowest1[], Blowest2[], Blowest3[], Blowest4[];
double B1hi[], B1cl[], B1lo[], B2hi[], B2cl[], B2lo[], B3hi[], B3cl[], B3lo[], B4hi[], B4cl[], B4lo[];;

int OnInit(){
   IndicatorBuffers(32);
   SetIndexBuffer(0,B0); SetIndexStyle(0,DRAW_ARROW);  SetIndexArrow(0,167);
   SetIndexBuffer(1,B1); SetIndexStyle(1,DRAW_ARROW);  SetIndexArrow(1,167);
   SetIndexBuffer(2,B2); SetIndexStyle(2,DRAW_ARROW);  SetIndexArrow(2,167);
   SetIndexBuffer(3,B3); SetIndexStyle(3,DRAW_ARROW);  SetIndexArrow(3,167);
   SetIndexBuffer(4,B4); SetIndexStyle(4,DRAW_ARROW);  SetIndexArrow(4,167);
   SetIndexBuffer(5,B5); SetIndexStyle(5,DRAW_ARROW);  SetIndexArrow(5,167);
   SetIndexBuffer(6,B6); SetIndexStyle(6,DRAW_ARROW);  SetIndexArrow(6,167);
   SetIndexBuffer(7,B7); SetIndexStyle(7,DRAW_ARROW);  SetIndexArrow(7,167);
   SetIndexBuffer(8,Bdata1);  SetIndexBuffer(12,Bhighest1); SetIndexBuffer(16,Blowest1); SetIndexBuffer(20,B1hi); SetIndexBuffer(24,B1cl); SetIndexBuffer(28,B1lo);
   SetIndexBuffer(9,Bdata2);  SetIndexBuffer(13,Bhighest2); SetIndexBuffer(17,Blowest2); SetIndexBuffer(21,B2hi); SetIndexBuffer(25,B2cl); SetIndexBuffer(29,B2lo);
   SetIndexBuffer(10,Bdata3); SetIndexBuffer(14,Bhighest3); SetIndexBuffer(18,Blowest3); SetIndexBuffer(22,B3hi); SetIndexBuffer(26,B3cl); SetIndexBuffer(30,B3lo);
   SetIndexBuffer(11,Bdata4); SetIndexBuffer(15,Bhighest4); SetIndexBuffer(19,Blowest4); SetIndexBuffer(23,B4hi); SetIndexBuffer(27,B4cl); SetIndexBuffer(31,B4lo);
   TimeFrame         = fmax(TimeFrame,Period());
   IndicatorShortName("m4" + " " + TimeFrame);
   indicatorFileName = WindowExpertName();
   return(0);
  }
  
int start(){ int i, limit=Bars-IndicatorCounted()-1;

 if(TimeFrame == Period()){
   for(i=limit;i>=0;i--){ B1hi[i]=iMA(NULL,0,ma1,0,3,PRICE_HIGH,i); B1cl[i]=iMA(NULL,0,ma1,0,3,PRICE_CLOSE,i); B1lo[i]=iMA(NULL,0,ma1,0,3,PRICE_LOW,i);
                          B2hi[i]=iMA(NULL,0,ma2,0,3,PRICE_HIGH,i); B2cl[i]=iMA(NULL,0,ma2,0,3,PRICE_CLOSE,i); B2lo[i]=iMA(NULL,0,ma2,0,3,PRICE_LOW,i);
                          B3hi[i]=iMA(NULL,0,ma3,0,3,PRICE_HIGH,i); B3cl[i]=iMA(NULL,0,ma3,0,3,PRICE_CLOSE,i); B3lo[i]=iMA(NULL,0,ma3,0,3,PRICE_LOW,i);
                          B4hi[i]=iMA(NULL,0,ma4,0,3,PRICE_HIGH,i); B4cl[i]=iMA(NULL,0,ma4,0,3,PRICE_CLOSE,i); B4lo[i]=iMA(NULL,0,ma4,0,3,PRICE_LOW,i); } 

   for(i=limit;i>=0;i--){ Bdata1[i]=(B1hi[i]+B1lo[i])/2;
                          Bhighest1[i]=B1hi[ArrayMaximum(Bdata1,period1,i)]; hi=Bhighest1[i+1];
                          Blowest1[i] =B1lo[ArrayMinimum(Bdata1,period1,i)]; li=Blowest1[i+1];
                          ci=B1cl[i]; value_of_monster=ci>hi? 1 : ci<li ? -1 :0;
                          if (value_of_monster==0)     { B0[i]=EMPTY_VALUE; B1[i]=EMPTY_VALUE; }
                          else if (value_of_monster<0) { B1[i]=3;           B0[i]=EMPTY_VALUE; }
                          else if (value_of_monster>0) { B0[i]=3;           B1[i]=EMPTY_VALUE; }
                          Bdata2[i]=(B2hi[i]+B1lo[i])/2;
                          Bhighest2[i]=B2hi[ArrayMaximum(Bdata2,period2,i)]; hi=Bhighest2[i+1];
                          Blowest2[i] =B1lo[ArrayMinimum(Bdata2,period2,i)]; li=Blowest2[i+1];
                          ci=B2cl[i]; value_of_monster=ci>hi? 1 : ci<li ? -1 :0;
                          if (value_of_monster==0)     { B2[i]=EMPTY_VALUE; B3[i]=EMPTY_VALUE; }
                          else if (value_of_monster<0) { B3[i]=2;           B2[i]=EMPTY_VALUE; }
                          else if (value_of_monster>0) { B2[i]=2;           B3[i]=EMPTY_VALUE; }
                          Bdata3[i]=(B3hi[i]+B3lo[i])/2;
                          Bhighest3[i]=B3hi[ArrayMaximum(Bdata3,period3,i)]; hi=Bhighest3[i+1];
                          Blowest3[i] =B3lo[ArrayMinimum(Bdata3,period3,i)]; li=Blowest3[i+1];
                          ci=B3cl[i]; value_of_monster=ci>hi? 1 : ci<li ? -1 :0;
                          if (value_of_monster==0)     { B4[i]=EMPTY_VALUE; B5[i]=EMPTY_VALUE; }
                          else if (value_of_monster<0) { B5[i]=1;           B4[i]=EMPTY_VALUE; }
                          else if (value_of_monster>0) { B4[i]=1;           B5[i]=EMPTY_VALUE; }
                          Bdata4[i]=(B4hi[i]+B4lo[i])/2;
                          Bhighest4[i]=B4hi[ArrayMaximum(Bdata4,period4,i)]; hi=Bhighest4[i+1];
                          Blowest4[i] =B4lo[ArrayMinimum(Bdata4,period4,i)]; li=Blowest4[i+1];
                          ci=B4cl[i]; value_of_monster=ci>hi? 1 : ci<li ? -1 :0;
                          if (value_of_monster==0)     { B6[i]=EMPTY_VALUE; B7[i]=EMPTY_VALUE; }
                          else if (value_of_monster<0) { B7[i]=0;           B6[i]=EMPTY_VALUE; }
                          else if (value_of_monster>0) { B6[i]=0;           B7[i]=EMPTY_VALUE; }  }
   return(0);
  }

   for(i=limit; i>=0; i--){ int y = iBarShift(NULL,TimeFrame,Time[i]); 
       B0[i]= iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period1,ma1,period2,ma2,period3,ma3,period4,ma4,0,y);
       B1[i]= iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period1,ma1,period2,ma2,period3,ma3,period4,ma4,1,y);
       B2[i]= iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period1,ma1,period2,ma2,period3,ma3,period4,ma4,2,y);
       B3[i]= iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period1,ma1,period2,ma2,period3,ma3,period4,ma4,3,y);
       B4[i]= iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period1,ma1,period2,ma2,period3,ma3,period4,ma4,4,y);
       B5[i]= iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period1,ma1,period2,ma2,period3,ma3,period4,ma4,5,y);
       B6[i]= iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period1,ma1,period2,ma2,period3,ma3,period4,ma4,6,y);
       B7[i]= iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,period1,ma1,period2,ma2,period3,ma3,period4,ma4,7,y);
   }
   return(0);
  }

