#property copyright "Copyright © 2014, ForexBaron.net"
#property link      "http://ForexBaron.net"

#property indicator_chart_window

extern int SignalCandle        = 1;
extern string mah4hi = "*****  ma settings:";
extern int MaTimeFrame        = 240;
extern int FastMaPeriod       = 21;
extern int FastMaShift        = 0;
extern int FastMaMethod       = MODE_EMA;
extern int FastMaAppliedPrice = PRICE_CLOSE;
extern int SlowMaPeriod       = 144;
extern int SlowMaShift        = 0;
extern int SlowMaMethod       = MODE_EMA;
extern int SlowMaAppliedPrice = PRICE_CLOSE;
extern string fxfishhi = "***** FxFish settings:";
extern int  FxFishPeriod       = 10;
extern int  FxFishPrice        = 0;
extern bool FxFishMode_Fast    = TRUE;
extern string alhi = "***** ALERT settings:";
extern bool PopupAlerts            = TRUE;
extern bool EmailAlerts            = FALSE;
extern bool PushNotificationAlerts = FALSE;
extern bool SoundAlerts            = FALSE;
extern string SoundFileLong        = "alert.wav";
extern string SoundFileShort       = "alert2.wav";
extern bool  ShowScreenComment     = TRUE;
extern int  lastalert = 3;
     
int init() {
 return(0);
}

int deinit() {
 if (ShowScreenComment) Comment("");
 return(0);
}

int start() {

 double fxfishlong  = iCustom(Symbol(),Period(),"FX Fish",FxFishPeriod,FxFishPrice,FxFishMode_Fast,false,0,SignalCandle);
 double fxfishshort = iCustom(Symbol(),Period(),"FX Fish",FxFishPeriod,FxFishPrice,FxFishMode_Fast,false,1,SignalCandle);
 double fxfishlong1  = iCustom(Symbol(),Period(),"FX Fish",FxFishPeriod,FxFishPrice,FxFishMode_Fast,false,0,SignalCandle+1);
 double fxfishshort1 = iCustom(Symbol(),Period(),"FX Fish",FxFishPeriod,FxFishPrice,FxFishMode_Fast,false,1,SignalCandle+1);

 double fastma = iMA(Symbol(),MaTimeFrame,FastMaPeriod,FastMaShift,FastMaMethod,FastMaAppliedPrice,SignalCandle);
 double slowma = iMA(Symbol(),MaTimeFrame,SlowMaPeriod,SlowMaShift,SlowMaMethod,SlowMaAppliedPrice,SignalCandle);
 double fastma1 = iMA(Symbol(),MaTimeFrame,FastMaPeriod,FastMaShift,FastMaMethod,FastMaAppliedPrice,SignalCandle+1);
 double slowma1 = iMA(Symbol(),MaTimeFrame,SlowMaPeriod,SlowMaShift,SlowMaMethod,SlowMaAppliedPrice,SignalCandle+1);

 string TrendDir = "none";
 if (fxfishlong!=0.0 && fxfishshort==0.0 && fastma>slowma) TrendDir = "LONG";
 if (fxfishlong==0.0 && fxfishshort!=0.0 && fastma<slowma) TrendDir = "SHORT";
 if (fxfishlong!=0.0 && fxfishshort==0.0 && fxfishlong1==0.0 && fxfishshort1!=0.0 && fastma>slowma && fastma1<=slowma1) {
  TrendDir = "LONG CHANGE";
  if (lastalert!=2) { doAlerts("LONG TREND",SoundFileLong); lastalert=2; }
 }
 if (fxfishlong==0.0 && fxfishshort!=0.0 && fxfishlong1!=0.0 && fxfishshort1==0.0 && fastma<slowma && fastma1>=slowma1) {
  TrendDir = "SHORT CHANGE";
   if (lastalert!=1) { doAlerts("SHORT TREND",SoundFileShort); lastalert=1; }
 }

 if (ShowScreenComment) Comment("signal/trend: "+TrendDir+", MA(tf:"+TFtoStr(MaTimeFrame)+") fast: "+DoubleToStr(fastma,Digits)+", slow: "+DoubleToStr(slowma,Digits));
 return(0);
}

void doAlerts(string msg,string SoundFile) {
        msg="FxFish MA Alert on "+Symbol()+", period "+TFtoStr(Period())+": "+msg;
 string emailsubject="MT4 alert on acc. "+AccountNumber()+", "+WindowExpertName()+" - Alert on "+Symbol()+", period "+TFtoStr(Period());
  if (PopupAlerts) Alert(msg);
  if (EmailAlerts) SendMail(emailsubject,msg);
  if (PushNotificationAlerts) SendNotification(msg);
  if (SoundAlerts) PlaySound(SoundFile);
}//void doAlerts(string msg,string SoundFile) {

string TFtoStr(int period) {
 if (period==0) period = Period();
 switch(period) {
  case 1     : return("M1");  break;
  case 5     : return("M5");  break;
  case 15    : return("M15"); break;
  case 30    : return("M30"); break;
  case 60    : return("H1");  break;
  case 240   : return("H4");  break;
  case 1440  : return("D1");  break;
  case 10080 : return("W1");  break;
  case 43200 : return("MN1"); break;
  default    : return(DoubleToStr(period,0));
 }
 return("UNKNOWN");
}//string TFtoStr(int period) {