//+------------------------------------------------------------------+
//|                                                    Crossover.mq4 |
//|                                                           ������ |
//|                                                                  |
//+------------------------------------------------------------------+ 
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_width1 2
#property indicator_color2 Magenta
#property indicator_width2 2


extern int  Period.MA1=4;
extern int  Period.MA2=16;
extern bool AlertOn=true;

//----
double CrossUp[];
double CrossDown[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  SetIndexStyle(0,DRAW_ARROW);
  SetIndexArrow(0,233);
  SetIndexStyle(1,DRAW_ARROW);
  SetIndexArrow(1,234);

  SetIndexBuffer(0,CrossUp);
  SetIndexBuffer(1,CrossDown);
//----
  return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
  return(0);
  }   
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
  int limit,i,counter;
  double MA1now,MA2now,MA1previous,MA2previous,MA1after,MA2after;
  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++)
    { 
    MA1now=iMA(NULL,0,Period.MA1,0,MODE_SMMA,PRICE_CLOSE,i);
    MA1previous=iMA(NULL,0,Period.MA1,0,MODE_SMMA,PRICE_CLOSE,i+1);
    MA1after=iMA(NULL,0,Period.MA1,0,MODE_SMMA,PRICE_CLOSE,i-1);

    MA2now=iMA(NULL,0,Period.MA2,0,MODE_SMMA,PRICE_OPEN,i);
    MA2previous=iMA(NULL,0,Period.MA2,0,MODE_SMMA,PRICE_OPEN,i+1);
    MA2after=iMA(NULL,0,Period.MA2,0,MODE_SMMA,PRICE_OPEN,i-1);
    
    if(MA1now > MA2now && MA1previous < MA2previous && MA1after > MA2after)
      {
      CrossUp[i]=Low[i]-20*Point;
      if(AlertOn && NewBar())
        {
        Alert(Symbol()," BUY");
        }
      }
    else if (MA1now < MA2now && MA1previous > MA2previous && MA1after < MA2after)
      {
      CrossDown[i]=High[i]+20*Point;
      if(AlertOn && NewBar())
        {
        Alert(Symbol()," SELL");
        }
      }
    }
//----
  return(0);
  }
//+------------------------------------------------------------------+
bool NewBar()
  {
  static datetime lastbar;
  datetime curbar = Time[0];
  if(lastbar!=curbar)
    {
    lastbar=curbar;
    return (true);
    }
  else
    {
    return(false);
    }
  }
//+------------------------------------------------------------------+