Custom Alert is One Bar Late?

 

I made a custom moving average alert system that is suppose to alert me each time my two moving averages cross each other. I am using MA's and alerts right now for simplicity but I plan on implementing the final strategy to a more advanced EA. I am quite new to coding so for whatever reason I get an alert but it is exactly one candle late. Any help would be greatly appreciated.

Here is the code for my Custom Indicator:

extern int MA1Period=1;
extern int MA1Shift=0;
extern int MA1Method=1;
extern int MA1AppliedPrice=1;

extern int MA2Period=10;
extern int MA2Shift=0;
extern int MA2Method=1;
extern int MA2AppliedPrice=1;

double AlertTag;
int BarsOnChart = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
double CurrentMA1 = iMA(NULL,0,MA1Period,MA1Shift,MA1Method,MA1AppliedPrice,1);
double PreviousMA1 = iMA(NULL,0,MA1Period,MA1Shift,MA1Method,MA1AppliedPrice,2);
double CurrentMA2 = iMA(NULL,0,MA2Period,MA2Shift,MA2Method,MA2AppliedPrice,1);
double PreviousMA2 = iMA(NULL,0,MA2Period,MA2Shift,MA2Method,MA2AppliedPrice,2);

if(PreviousMA1<PreviousMA2 && CurrentMA1>CurrentMA2 && Bars != BarsOnChart)
   if (IsNewCandle()==true)AlertCall(0);
   AlertTag=(double)Time[0];
   
if(PreviousMA1>PreviousMA2 && CurrentMA1<CurrentMA2 && Bars != BarsOnChart)
   if (IsNewCandle()==true)AlertCall(1);
   AlertTag=(double)Time[0];
//--- return value of prev_calculated for next call
   return(0);
  }
//+------------------------------------------------------------------+
void AlertCall(int Call)
{
      if (Call==0)
         Alert("YOU GOT AN (up) CROSS SON");
      if (Call==1)
         Alert("YOU GOT A (down) CROSS SON");
}
bool IsNewCandle()
{
     
      if (Bars == BarsOnChart)
      return (false);
      BarsOnChart = Bars;
      return(true);
}
 

There is no delay if you program as follows. However, since an alert will be issued when the candlestick is not finalized, we usually program it to alert when the candlestick is finalized.

double CurrentMA1 = iMA(NULL,0,MA1Period,MA1Shift,MA1Method,MA1AppliedPrice,0);
double PreviousMA1 = iMA(NULL,0,MA1Period,MA1Shift,MA1Method,MA1AppliedPrice,1);
double CurrentMA2 = iMA(NULL,0,MA2Period,MA2Shift,MA2Method,MA2AppliedPrice,0);
double PreviousMA2 = iMA(NULL,0,MA2Period,MA2Shift,MA2Method,MA2AppliedPrice,1);
 
Naguisa Unada:

There is no delay if you program as follows. However, since an alert will be issued when the candlestick is not finalized, we usually program it to alert when the candlestick is finalized.

Thank you very much!
Reason: