//+------------------------------------------------------------------+
//|                                        LWMA-Crossover_Signal.mq4 |
//|                    Copyright © 2005, Jason Robinson (jnrtrading) |
//|                                       http://www.jnrtading.co.uk |
//+------------------------------------------------------------------+

/*
  +-------------------------------------------------------------------+
  | Allows you to enter two lwma periods and it will then show you at |
  | Which point they crossed over. It is more usful on the shorter    |
  | periods that get obscured by the bars / candlesticks and when     |
  | the zoom level is out. Also allows you then to remove the emas    |
  | from the chart. (lwmas are initially set at 5 and 6)              |
  +-------------------------------------------------------------------+
*/

#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)"
#property link      "http://www.jnrtrading.co.uk"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 SpringGreen
#property indicator_color2 Red


extern string _AlertSetting       = "---Alert Settings ---";
extern bool   alertsOn            = false;  //Turn alerts on?
extern bool   alertsOnCurrentBar  = false; //Alerts on (still opened) bar true/false?
extern bool   alertsMessage       = false;  //Alerts Message true/false?
extern bool   alertsSound         = false;   //Alerts sound true/false?
extern bool   alertsNotification  = false;  //Alerts push notification true/false?
extern bool   alertsEmail         = false;  //Alerts email true/false?
extern string soundFile           = "alert.wav";




double CrossUp[];
double CrossDown[];
extern int FasterLWMA = 5;
extern int SlowerLWMA = 6;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   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 fasterLWMAnow, slowerLWMAnow, fasterLWMAprevious, slowerLWMAprevious, fasterLWMAafter, slowerLWMAafter;
   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;
       
      fasterLWMAnow = iMA(NULL, 0, FasterLWMA, 0, MODE_LWMA, PRICE_CLOSE, i);
      fasterLWMAprevious = iMA(NULL, 0, FasterLWMA, 0, MODE_LWMA, PRICE_CLOSE, i+1);
      fasterLWMAafter = iMA(NULL, 0, FasterLWMA, 0, MODE_LWMA, PRICE_CLOSE, i-1);

      slowerLWMAnow = iMA(NULL, 0, SlowerLWMA, 0, MODE_LWMA, PRICE_CLOSE, i);
      slowerLWMAprevious = iMA(NULL, 0, SlowerLWMA, 0, MODE_LWMA, PRICE_CLOSE, i+1);
      slowerLWMAafter = iMA(NULL, 0, SlowerLWMA, 0, MODE_LWMA, PRICE_CLOSE, i-1);
      
      if ((fasterLWMAnow > slowerLWMAnow) && (fasterLWMAprevious < slowerLWMAprevious) && (fasterLWMAafter > slowerLWMAafter)) {
         CrossUp[i] = Low[i] - Range*0.5;
      }
      else if ((fasterLWMAnow < slowerLWMAnow) && (fasterLWMAprevious > slowerLWMAprevious) && (fasterLWMAafter < slowerLWMAafter)) {
         CrossDown[i] = High[i] + Range*0.5;
      }
   }
   
     manageAlerts();
     
   return(0);
}


void manageAlerts()
{
   int barshift = 0;
   string msg = Symbol()+ ", " + TF2Str(Period());
   
   if (alertsOnCurrentBar == false) barshift = 1;
   
   
   if (alertsOn)
   {
   
    if (CrossUp[barshift] != EMPTY_VALUE)
     {
        msg = msg + " Pman Cross Signal Up " + " @ " + TimeToStr(TimeLocal(),TIME_SECONDS);
        doAlert(barshift,msg);
     }
     
    else if (CrossDown[barshift] != EMPTY_VALUE)
     {
       msg = msg + " Pman Cross Signal Down " + " @ " + TimeToStr(TimeLocal(),TIME_SECONDS);
      
       doAlert(barshift,msg);
     }
      
        
   }
}

  
//+------------------------------------------------------------------+
//|     doAlert                                                             |
//+------------------------------------------------------------------+

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;

   
   if (previousAlert != doWhat && previousTime != Time[forBar]) 
   {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

  
       //

     //  message =  StringConcatenate(Symbol(),", ",timeFrameToString(_Period)," HP ",doWhat," @ ",TimeToStr(TimeLocal(),TIME_SECONDS));
          if (alertsMessage)      Alert(doWhat);
          if (alertsEmail)        SendMail(StringConcatenate(Symbol(),"Pman Cross over "),doWhat);
          if (alertsNotification) SendNotification(doWhat);
          if (alertsSound)        PlaySound("alert.wav");
   }

}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string TF2Str(int nperiod)
  {
   switch(nperiod)
     {
      case PERIOD_M1: return("M1");
      case PERIOD_M5: return("M5");
      case PERIOD_M15: return("M15");
      case PERIOD_M30: return("M30");
      case PERIOD_H1: return("H1");
      case PERIOD_H4: return("H4");
      case PERIOD_D1: return("D1");
      case PERIOD_W1: return("W1");
      case PERIOD_MN1: return("MN");
     }
   return(Period());
  }
//+------------------------------------------------------------------+

