Problem with the function iMA

 
I have the following start function in my expert :

static double EMA_Short_Saved=0;
 
int start()
  {
//----
   
   if(!isNewBar())
   {
      return(0);
   }
   
   
   double EMA_Short_Current   = NormalizeDouble(iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, 0), 4); 
   double EMA_Short_Previous  = NormalizeDouble(iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, 1), 4); 
   
   
   Print("EMA_Short_Current = " + EMA_Short_Current + ", EMA_Short_Previous = " + EMA_Short_Previous + ", EMA_Short_Saved = " + EMA_Short_Saved); 
 
   
   
   if(EMA_Short_Saved != 0)
      if(EMA_Short_Saved != EMA_Short_Previous)
         Print("Something wrong ??");
         
         
    EMA_Short_Saved = EMA_Short_Current;
   
//----
   return(0);
  }
What I don't understand is why the variables EMA_Short_Saved and EMA_Short_Previous are not egal !!

They should be !

EMA_Short_Saved
is the value of the EMA calculated at time i and saved
EMA_Short_Previous is the value of the EMA for shift = 1, calculated at time i + 1

so they should be egal.

Do I miss something ?
 
At the time the EA reads off bar 0, that bar has just started, and it's close is still in the future; it's a very thin bar at the time.
Either use PRICE_OPEN for the iMA, or review bars 1 and 2 instead.
 
You do one mistake. Use this modified code and try to find answer.

static double EMA_Short_Saved=0;
static double PreviousPrice=0;
 
 
int start()
  {
//----
   
   if(!isNewBar())
   {
      return(0);
   }
   
   
   double EMA_Short_Current   = NormalizeDouble(iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, 0), 4); 
   double EMA_Short_Previous  = NormalizeDouble(iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, 1), 4); 
   
   
   Print("EMA_Short_Current = " + EMA_Short_Current + ", EMA_Short_Previous = " + EMA_Short_Previous + ", EMA_Short_Saved = " + EMA_Short_Saved); 
 
   
   
   if(EMA_Short_Saved != 0)
      if(EMA_Short_Saved != EMA_Short_Previous)
         Print("Something wrong ??");
         
         
    EMA_Short_Saved = EMA_Short_Current;
    PreviousPrice=Close[0];
//----
   return(0);
  }
Reason: