
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 SeaGreen
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
extern string ModeValues = "<<Values for Ma_Mode>>";
extern string ModeValueDef = "0=Simple,1=Exponential,2=Smoothed,3=LinearWeighted";
extern string PriceValues = "<<Values for Ma_AppliedPrice>>";
extern string AppliedPriceValueDef = "0=Closed,1=Open,2=High,3=Low,4=Median,5=Typical,6=WeightedClose";

extern int FasterMa = 5;
extern int FasterMa_Mode = 0;
extern int FasterMa_AppliedPrice = 0;
extern int SlowerMa = 8;
extern int SlowerMa_Mode = 0;
extern int SlowerMa_AppliedPrice = 0;
extern bool SoundON=true;
double alertTag;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY,3);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY,3);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
   double FasterManow, SlowerManow, FasterMaprevious, SlowerMaprevious, FasterMaafter, SlowerMaafter;
   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;
      Range=0;
      AvgRange=0;
      for (counter=i ;counter<=i+9;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;

      FasterManow = iMA(NULL, 0, FasterMa, 0, FasterMa_Mode, FasterMa_AppliedPrice, i);
      FasterMaprevious = iMA(NULL, 0, FasterMa, 0, FasterMa_Mode, FasterMa_AppliedPrice, i+1);
      FasterMaafter = iMA(NULL, 0, FasterMa, 0, FasterMa_Mode, FasterMa_AppliedPrice, i-1);

      SlowerManow = iMA(NULL, 0, SlowerMa, 0, SlowerMa_Mode, SlowerMa_AppliedPrice, i);
      SlowerMaprevious = iMA(NULL, 0, SlowerMa, 0, SlowerMa_Mode, SlowerMa_AppliedPrice, i+1);
      SlowerMaafter = iMA(NULL, 0, SlowerMa, 0, SlowerMa_Mode, SlowerMa_AppliedPrice, i-1);
      
      if ((FasterManow > SlowerManow) && (FasterMaprevious < SlowerMaprevious) && (FasterMaafter > SlowerMaafter)) {
         CrossUp[i] = Low[i] - Range*0.5;
      }
      else if ((FasterManow < SlowerManow) && (FasterMaprevious > SlowerMaprevious) && (FasterMaafter < SlowerMaafter)) {
          CrossDown[i] = High[i] + Range*0.5;
      }
        if (SoundON==true && i==1 && CrossUp[i] > CrossDown[i] && alertTag!=Time[0]){
         Alert(FasterMa+"-"+SlowerMa+" MA Cross going Down on ",Symbol()," ",Period());
        alertTag = Time[0];
      }
        if (SoundON==true && i==1 && CrossUp[i] < CrossDown[i] && alertTag!=Time[0]){
       Alert(FasterMa+"-"+SlowerMa+" MA Cross going Up on ",Symbol()," ",Period());
        alertTag = Time[0];
        } 
  }
   return(0);
}

