crossover not picking up the previous value

 

The alert is sounding every hour with this indicator, it should be picking up the previous value and only alerting if the lines have crossed back over.

ANy ideas on what is wrong?


//+------------------------------------------------------------------+
//|                                      Alert SMA-EMA CrossOver.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 MediumOrchid
#property indicator_color2 Yellow
#property indicator_color3 DodgerBlue
#property indicator_color4 Magenta

//---- Variables Externes
extern int SlowPeriod = 8;
extern int FastPeriod = 5;
extern int TimeFrame = 60;

//---- Indicateurs
double SlowPeriodCurrent, SlowPeriodPrevious, FastPeriodCurrent, FastPeriodPrevious;
int    nShift, digit, digits;
int    i,j,limit,counted_bars;
datetime LastAlertTime;

//---- Buffers
double ExtMapBuffer1[];    
double ExtMapBuffer2[];    
double ExtMapBuffer3[];    
double ExtMapBuffer4[];    
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//---- Styles et couleur des Lignes
      SetIndexStyle(0,DRAW_LINE);
      SetIndexBuffer(0,ExtMapBuffer1);
      SetIndexStyle(1,DRAW_LINE);
      SetIndexBuffer(1,ExtMapBuffer2);
//---- Styles et couleur des Fleches      
      SetIndexStyle(2, DRAW_ARROW, 0, 1);    // Fleche vers le haut
      SetIndexArrow(2, 233);
      SetIndexBuffer(2, ExtMapBuffer3);
      SetIndexStyle(3, DRAW_ARROW, 0, 1);    // Fleche vers le bas
      SetIndexArrow(3, 234);
      SetIndexBuffer(3, ExtMapBuffer4);
//----       
      switch(Period())
         {
            case     1: nShift = 1;   break;
            case     5: nShift = 3;   break;
            case    15: nShift = 5;   break;
            case    30: nShift = 10;  break;
            case    60: nShift = 15;  break;
            case   240: nShift = 20;  break;
            case  1440: nShift = 80;  break;
            case 10080: nShift = 100; break;
            case 43200: nShift = 200; break;
         }
//----
      digits = MarketInfo(Symbol(),MODE_DIGITS);
//----
      return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
      counted_bars=IndicatorCounted();

      if(counted_bars<0) 
         return(-1);
      if(counted_bars>0) 
         counted_bars--;
      limit=Bars-counted_bars;
   
      for(i=0; i<limit; i++)
         {
            SlowPeriodCurrent=iMA(NULL,0,SlowPeriod,0,MODE_SMA,PRICE_CLOSE,i);
            SlowPeriodPrevious=iMA(NULL,0,SlowPeriod,0,MODE_SMA,PRICE_CLOSE,i+1);
            ExtMapBuffer1[i]=SlowPeriodCurrent;
            FastPeriodCurrent=iMA(NULL,0,FastPeriod,0,MODE_SMA,PRICE_CLOSE,i);
            FastPeriodPrevious=iMA(NULL,0,FastPeriod,0,MODE_SMA,PRICE_CLOSE,i+1);
            ExtMapBuffer2[i]=FastPeriodCurrent;

//---- Buy
            if((FastPeriodCurrent > SlowPeriodCurrent) && ( FastPeriodPrevious <= SlowPeriodCurrent) && LastAlertTime < Time[0])  // Croisement WMA8  up bord supérieur du tunnel
               {
                  ExtMapBuffer3[i] = Low[i] - nShift*Point;
                  Alert("MA CrossOver Going for a BUY Trend Sesion ",SlowPeriodCurrent," Price ",Close[1]," for ", Symbol(),"-",Period());
                  PlaySound("alert.wav");
                  LastAlertTime = Time[0];
               }
//---- Sell
            if((FastPeriodCurrent < SlowPeriodCurrent) && ( FastPeriodPrevious >= SlowPeriodCurrent) && LastAlertTime < Time[0])  // Croisement WMA8  down bord inférieur du tunnel
               {
                  ExtMapBuffer4[i] = High[i] + nShift*Point;
                  Alert("MA CrossOver Going for a SELL Trend Sesion ",SlowPeriodCurrent," Price ",Close[1]," for ", Symbol(),"-",Period());
                  PlaySound("alert.wav");
                  LastAlertTime = Time[0];
               }
         }
}