//+------------------------------------------------------------------+
//|                                                    Corre_MAs.mq4 |
//|                                            Pepbullish San Badger |
//|                              http://www.pepbullish.blogspot.com/ |
//+------------------------------------------------------------------+

#property copyright "Pepbullish San Badger"
#property link      "http://www.pepbullish.blogspot.com/"

//-- Indicator Settings --

#property indicator_separate_window
#property indicator_buffers     4

//-- Input Parameters --

extern string  MovingAverage      =  " -- Moving Average Settings -- ";

extern int     MA1Period          =  14; 
extern int     MA2Period          =  21;

extern int     MA1Shift           =  0; 
extern int     MA2Shift           =  0;

extern int     MA1Type            =  0; // 0-Simple  1-Exponencial  2-Smoothed  3-Linear Weighted 
extern int     MA2Type            =  0;

extern int     MA1AppliedPrice    =  0; // 0-Close  1-Open  2-High  3-Low  4-MedianPrice(HL/2)  5-TypicalPrice(HLC/3) 6-WeightedClose(HLCC/4)
extern int     MA2AppliedPrice    =  0;

extern int     SignalPeriod       =  7;
extern int     SignalShift        =  0;
extern int     SignalType         =  0;

extern bool    ShowSignal         =  true;

//-- Indicator Buffers --

double DifMas[];
double BullishBar[];
double BearishBar[];
double Signal[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
  {
  
//-- Drawing Settings and Buffers Mapping --   

   SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(0,DifMas);  
   
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2,Teal);
   SetIndexBuffer(1,BullishBar);   
   
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,2,Crimson);
   SetIndexBuffer(2,BearishBar);
   
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2,DarkOrange);
   SetIndexBuffer(3,Signal);                              
      
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
  
   int    limit;
   int    counted_bars=IndicatorCounted();
   
   if (counted_bars>0) counted_bars--;
   limit=Bars-counted_bars; 
   
   for (int i=0; i<limit; i++)
      {
      
      double Ma1 = iMA(NULL,0,MA1Period,MA1Shift,MA1Type,MA1AppliedPrice,i);
      double Ma2 = iMA(NULL,0,MA2Period,MA2Shift,MA2Type,MA2AppliedPrice,i);
      
      DifMas[i] = Ma1 - Ma2; 
      
      if ( DifMas[i] < DifMas[i-1] ) BullishBar[i] = DifMas[i];
      if ( DifMas[i] > DifMas[i-1] ) BearishBar[i] = DifMas[i];  
      
      }   
      
   if ( ShowSignal )
      {   
      
      for (i=0; i<limit; i++)     
      {
      Signal[i]  = iMAOnArray(DifMas,Bars,SignalPeriod,SignalShift,SignalType,i);   
      }            
      IndicatorShortName("Corre_MAs  ( MA1: "+MA1Period+"  ,  MA2: "+MA2Period+" )   Signal: TRUE   Values:");     
             
      }    
      
      else
      {          
      IndicatorShortName("Corre_MAs  ( MA1: "+MA1Period+"  ,  MA2: "+MA2Period+" )   Signal: FALSE   Values:");
      }
      
   return(0);
  }
//+------------------------------------------------------------------+