//+------------------------------------------------------------------+
//|                     MA cross.mq4 - I found it at fxfisherman.com |
//|                        Copyright 2012, www.forex-programming.com |
//|                                        www.forex-programming.com |
//+------------------------------------------------------------------+
//#property copyright "Copyright 2012, www.forex-programming.com"
//#property link      "www.forex-programming.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_width1  2
#property indicator_width2  2

extern string s1="-- MA settings --";
extern int    Fast_MA_Period  = 5 ;
extern int    Fast_MA_Shift   = 0 ;
extern int    Fast_MA_Method  = 3 ;
extern int    Fast_MA_Apply   = 0 ;

extern int    Slow_MA_Period  = 11 ;
extern int    Slow_MA_Shift   = 0 ;
extern int    Slow_MA_Method  = 3 ;
extern int    Slow_MA_Apply   = 0 ;

extern string s2="MA Method, 0=SMA,1=EMA,2=SMMA,3=LWMA";
extern string s3="MA Apply, 0=C,1=O,2=H,3=L,4=M,5=T,6=W";

extern string s4="-- Alert settings --";
extern bool   ScreenAlert     =true,
              PushAlert       =false;

double        Fast[], Slow[];
datetime      New_Time=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//   Comment("www.forex-programming.com");
   
   SetIndexBuffer(0, Fast);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "Fast MA");
   SetIndexBuffer(1, Slow);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexLabel(1, "Slow MA");
   
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   Comment("");
   
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   int limit=Bars-counted_bars;
   if (limit<0) limit=0;
   
   // define MAs values
   for (int i=limit; i>=0; i--)
      {
       Fast[i]=iMA(NULL, 0, Fast_MA_Period, Fast_MA_Shift, Fast_MA_Method, Fast_MA_Apply, i);
       Slow[i]=iMA(NULL, 0, Slow_MA_Period, Slow_MA_Shift, Slow_MA_Method, Slow_MA_Apply, i);
      }

   // Alert
      {
       // buy alert
       if ((New_Candle() && Fast[1] > Slow[1] && Fast[2] <= Slow[2]))
         {
          if (ScreenAlert) Alert("BUY - "+Symbol()," | ",TimeToStr(CurTime(),TIME_DATE)," | ",TimeHour(CurTime()),":",TimeMinute(CurTime())," | Period=",Period());
          if (PushAlert) SendNotification("BUY -"+Symbol());
         }
       // sell alert
       if ((New_Candle() && Fast[1] < Slow[1] && Fast[2] >= Slow[2]))
         {
          if (ScreenAlert) Alert("SELL - "+Symbol()," | ",TimeToStr(CurTime(),TIME_DATE)," | ",TimeHour(CurTime()),":",TimeMinute(CurTime())," | Period=",Period());
          if (PushAlert) SendNotification("SELL - "+Symbol());
         }
      }
   
   return(0);
  }

//+------------------------------------------------------------------+
//| Define new candle                                                |
//+------------------------------------------------------------------+
bool New_Candle()
  {
   if (New_Time!=Time[0])
     {
      New_Time=Time[0];
      return(true);
     }
    else
     {
      return(false);
     }
  }

//+------------------------------------------------------------------+
//| Period to String                                                 |
//+------------------------------------------------------------------+
string TFName( int tf )
{
   switch(tf) {
      case PERIOD_M1: return("M1"); break;
		case PERIOD_M5: return("M5"); break;
		case PERIOD_M15: return("M15"); break;
		case PERIOD_M30: return("M30"); break;
		case PERIOD_H1: return("H1"); break;
		case PERIOD_H4: return("H4"); break;
		case PERIOD_D1: return("Daily"); break;
		case PERIOD_W1: return("Weekly"); break;
		case PERIOD_MN1: return("Monthly"); break;
		default: return(TFName(Period()));
	}
	
	return(0);
}