//+--------------------------------------------------------------------------+
//|                                                3 MA Cross with alert.mq4 |
//|                                                                   mladen |
//+--------------------------------------------------------------------------+
//mod2009fxtsd 2macross shift etc
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 SkyBlue
#property indicator_color2 DarkOrange
#property indicator_color3 DodgerBlue
#property indicator_color4 Red
#property indicator_color5 Lime
#property indicator_color6 Green
#property indicator_color7 Lime
#property indicator_color8 YellowGreen
#property indicator_width1 0
#property indicator_width2 0
#property indicator_width3 1
#property indicator_width4 1

//
//
//
//
//

extern int FasterMA       =    5;
extern int FasterShift    =    0;
extern int FasterMode     =    1;
//extern int MediumMA       =   21;
//extern int MediumShift    =    0;
//extern int MediumMode     =    1;
extern int SlowerMA       =   34;
extern int SlowerShift    =    0;
extern int SlowerMode     =    1;
extern bool showMAs          = true;
extern bool crossesOnCurrent = true;
extern bool alertsOn         = false;
extern bool alertsMessage    = true;
extern bool alertsSound      = false;
extern bool alertsEmail      = false;


//
//
//
//
//

double CrossUp[];
double CrossDown[];
double CrossWUp[];
double CrossWDown[];
double ma1[];
double ma2[];
double ma1s[];
double ma2s[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   if(FasterShift||SlowerShift!=0)  {
   SetIndexBuffer(0, CrossUp);    SetIndexStyle(0, DRAW_ARROW); SetIndexArrow(0, 233);
   SetIndexBuffer(1, CrossDown);  SetIndexStyle(1, DRAW_ARROW); SetIndexArrow(1, 234);
                                    }
   SetIndexBuffer(2, CrossWUp);   SetIndexStyle(2, DRAW_ARROW); SetIndexArrow(2, 241);
   SetIndexBuffer(3, CrossWDown); SetIndexStyle(3, DRAW_ARROW); SetIndexArrow(3, 242);
   SetIndexBuffer(4,ma1);
   SetIndexBuffer(5,ma2);
   SetIndexBuffer(6,ma1s);
   SetIndexBuffer(7,ma2s);
   SetIndexShift(6,FasterShift);
   SetIndexShift(7,SlowerShift);
   if (showMAs)
   {
      SetIndexStyle(4,DRAW_NONE);
      SetIndexStyle(5,DRAW_NONE);
      SetIndexStyle(6,DRAW_LINE);
      SetIndexStyle(7,DRAW_LINE);
      SetIndexEmptyValue(4,0.00);
      SetIndexEmptyValue(5,0.00);
      SetIndexEmptyValue(6,0.00);
      SetIndexEmptyValue(7,0.00);
   }      
   else
   {
      SetIndexStyle(4,DRAW_NONE);
      SetIndexStyle(5,DRAW_NONE);
      SetIndexStyle(6,DRAW_NONE);
      SetIndexStyle(7,DRAW_NONE);
   }      

   SetIndexLabel(0,"maCrossUp");
   SetIndexLabel(1,"maCrossDn");
   SetIndexLabel(2,"BarTimeOfCrossUp");
   SetIndexLabel(3,"BarTimeOfCrossDn");

   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{ 
   int    limit, i; 
   int    counted_bars=IndicatorCounted();
   double fasterMAnow, fasterMAprevious;
   double slowerMAnow, slowerMAprevious;
   double FasterShift1, SlowerShift1;
   

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;

//          if (FasterShift < 0) limit = MathMax(limit,-FasterShift);
//          if (SlowerShift < 0) limit = MathMax(limit,-SlowerShift);


       FasterShift1=0.0;   SlowerShift1=0.0; 
       
   if (FasterShift>SlowerShift)  FasterShift1=FasterShift-SlowerShift; 
   else
   if (FasterShift<SlowerShift)  SlowerShift1=SlowerShift-FasterShift; 
   
   
   double delshift=MathMin(FasterShift,SlowerShift);
   
   SetIndexShift(0,delshift);   SetIndexShift(1,delshift);


      if (FasterShift||FasterShift1< 0) limit = MathMax(limit,-MathMin(FasterShift,FasterShift1));
      if (SlowerShift||SlowerShift1< 0) limit = MathMax(limit,-MathMin(SlowerShift,SlowerShift1));






   //
   //
   //
   //
   //
   //

   
   for(i = limit; i >=0; i--)
   {      
      ma1[i] = iMA(NULL, 0, FasterMA, FasterShift1, FasterMode, PRICE_CLOSE, i);
      ma2[i] = iMA(NULL, 0, SlowerMA, SlowerShift1, SlowerMode, PRICE_CLOSE, i);
      ma1s[i] = iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i);
      ma2s[i] = iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, i);

         if (crossesOnCurrent==false && i==0) continue;
         
      //
      //
      //
      //
      //
         
         fasterMAnow      = ma1[i];
         fasterMAprevious = ma1[i+1];
         slowerMAnow      = ma2[i];
         slowerMAprevious = ma2[i+1];
      
      //
      //
      //
      //
      //
      
      int    crossID=0;
      double curr;
      double prev;
      double point;
      while (true)
         {
            if (fasterMAnow > 0 && slowerMAnow > 0)
               {
                  curr  =  fasterMAnow      - slowerMAnow;
                  prev  =  fasterMAprevious - slowerMAprevious;
                  point = (fasterMAnow      + fasterMAprevious)/2;
                     if (curr*prev<=0) { crossID=2; break; }
               }
               break;
         }
      
         //
         //
         //
         //
         //
         
         CrossUp[i]    = EMPTY_VALUE;
         CrossDown[i]  = EMPTY_VALUE;
         CrossWUp[i]   = EMPTY_VALUE;
         CrossWDown[i] = EMPTY_VALUE;
         if (crossID>0)
         {
            if (alertsOn)
            if ((i==0 && crossesOnCurrent==true) || (i==1 && crossesOnCurrent==false))
            {
                        if(curr>0) doAlert("Fast MA crossed Slow MA UP");
                        else       doAlert("Fast MA crossed Slow MA DOWN"); 
             
            }               

            //
            //
            //
            //
            //
            
            double Range = 0.0;
            for (int counter=i ;counter<=i+9;counter++) Range += MathAbs(High[counter]-Low[counter]);
                                                        Range /= 10;

            //
            //
            //
            //
            //
            



               if (curr>0)    {
                     CrossWUp[i]  = point - Range*0.5;
                     CrossUp[i]   = point - Range*0.85;
                                    }
               else           {
                     CrossWDown[i] = point + Range*0.5;
                     CrossDown[i] = point + Range*0.85;             
                                    }

         }               

   }   
   return(0);
 }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
 
void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"3 MA line crossing"),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}


