//+------------------------------------------------------------------+
//|                                         Helper MA Cross With MACD.mq4 |
//|                         |By: TradeForex
//+------------------------------------------------------------------+

/*
  +------------------------------------------------------------------+
  
  +------------------------------------------------------------------+
*/   

#property indicator_minimum 0.0
#property indicator_maximum 1.5

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 3
#property indicator_color2 Red
#property indicator_width2 3

//----
IndicatorShortName ("Helper");
IndicatorDigits (0);
double CrossUp[];
double CrossDown[];
extern int TimeFrame = 0;
extern int Fast_MA_Period = 14;
extern int Fast_MA_Mode = 1;
extern int Fast_Applied_Price = 0;
extern int Slow_MA_Period = 20;
extern int Slow_MA_Mode = 0;
extern int Slow_Applied_Price = 0;
extern int MaValueGap = 0;
extern  string MACD_Parameters = " ----MACD Variables ---  ";
extern int MACD_TimeFrame = 0;
extern int Fast_EMA = 12;
extern int Slow_EMA  = 26;
extern int MACD_SMA = 9;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators



   SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY, 4);


   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY, 4);
   SetIndexArrow(1, 117);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//DRAW_HISTOGRAM
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
   double fasterEMAnow, slowerEMAnow, fasterEMAprevious, slowerEMAprevious;
    double MacdCurrent, MacdPrevious, SignalCurrent, SignalPrevious;

         double Range, AvgRange;
   int 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--;

   limit=Bars-counted_bars;
   
   for(i = 0; i <= limit; i++) {
   
      counter=i;
      for (counter=i ;counter<=i+9;counter++)
      
       
      fasterEMAnow = iMA(NULL, TimeFrame, Fast_MA_Period, 0, Fast_MA_Mode, Fast_Applied_Price, i);

      slowerEMAnow = iMA(NULL, TimeFrame, Slow_MA_Period, 0, Slow_MA_Mode, Slow_Applied_Price, i);

    MacdCurrent=iMACD(NULL,MACD_TimeFrame,Fast_EMA,Slow_EMA,MACD_SMA,PRICE_CLOSE,MODE_MAIN,i+0);
    MacdPrevious=iMACD(NULL,MACD_TimeFrame,Fast_EMA,Slow_EMA,MACD_SMA,PRICE_CLOSE,MODE_MAIN,i+1);
    SignalPrevious=iMACD(NULL,MACD_TimeFrame,Fast_EMA,Slow_EMA,MACD_SMA,PRICE_CLOSE,MODE_SIGNAL,i+1);


      if ((fasterEMAnow > slowerEMAnow)
        && (MacdCurrent > MacdPrevious) && (MacdCurrent > SignalPrevious)
      && MathAbs(fasterEMAnow - slowerEMAnow)/Point > MaValueGap
      ) 
      {
         CrossUp[i] = 1;
      }
     else if ((fasterEMAnow < slowerEMAnow) 
        && (MacdCurrent < MacdPrevious) && (MacdCurrent < SignalPrevious)
       && MathAbs(fasterEMAnow - slowerEMAnow)/Point > MaValueGap
      ) {
         CrossDown[i] = 1;
      }    
 
   }
   return(0);
}

