MACD Values seems inconsistent

 

I am struggling to match the MACD H1 values from Strategy Tester Logs from one hour to the next. It seems that when I print the MACD H1 value in the Tester logs for the current hour eg: I print the 8:00 Macd value at 8:00, I get a different value than if I print the 8:00 Macd value at 9:00.


These are the results from the Strategy Tester Logs:

2018.01.03 08:00:00   MacdBuffer[0]:23.851 [1]:24.629 [2]:25.292 [3]:26.252

2018.01.03 09:00:00   MacdBuffer[0]:24.128 [1]:24.485 [2]:24.629 [3]:25.292

2018.01.03 10:00:00   MacdBuffer[0]:21.953 [1]:23.19  [2]:24.485 [3]:24.629

2018.01.03 11:00:00   MacdBuffer[0]:20.588 [1]:21.864 [2]:23.19  [3]:24.485

2018.01.03 12:00:00   MacdBuffer[0]:19.23  [1]:20.508 [2]:21.864 [3]:23.19


As you can see:

At 08:00 MacdVal[0] = 23.851 

At 09:00 MacdVal[1] = 24.485 (This should be the same value as 8:00-MacdVal[0], but its not!)

At 10:00 MacdVal[2] = 24.485 (This is the same value as 9:00-MacdVal[1] and it is the same...)

At 11:00 MacdVal[3] = 24.485 (This is the same as 10:00-MacdVal[2] and it is the same...)


So my question is, why does the current MACD values in the current hour seem to differ from the MACD value for the same hour pulled in the subsequent hour. 


The code is below:

//1) Variables
int MACDBufferHandle;
double MacdVal[];
datetime CurrentBarTime[1];
datetime LastBarTime;


//2.1) Event: On init
int OnInit()
   {return(INIT_SUCCEEDED);}       


//2.2) Event: On tick
void OnTick()
{   
   if(IsNewBar()==true && TimeCurrent() >= StringToTime("2018-01-03 08:00") && TimeCurrent() <= StringToTime("2018-01-03 12:00"))
    {  
      Func_CreateMacdBuffer();
      Print("MacdBuffer[0]:", NormalizeDouble(10000*MacdVal[0],3), " [1]:", 
                              NormalizeDouble(10000*MacdVal[1],3), " [2]:", 
                              NormalizeDouble(10000*MacdVal[2],3), " [3]:", 
                              NormalizeDouble(10000*MacdVal[3],3));
    }
}


//3.1) Function: New Bar
bool IsNewBar()
   {     
   CopyTime("GBPUSD",PERIOD_H1,0,1,CurrentBarTime);
   
      if(LastBarTime==(CurrentBarTime[0]))
        return(false);
                else if (LastBarTime!=CurrentBarTime[0])
                {LastBarTime=(CurrentBarTime[0]);    
                        return(true);}
                else
                 return(false);  
   }

        
//3.2) Function: Create buffer  
void Func_CreateMacdBuffer()
   {
   MACDBufferHandle=iMACD("GBPUSD",PERIOD_H1,12,30,9,PRICE_CLOSE);
   if(MACDBufferHandle<0)
     {Print("MacdBufferHandle failed");}
   CopyBuffer(MACDBufferHandle,0,0,4,MacdVal);
   ArraySetAsSeries(MacdVal,true);
   }

 
forxtradr1: I print the 8:00 Macd value at 8:00, I get a different value than if I print the 8:00 Macd value at 9:00.

The candle was forming when you printed the value. The close and everything derived from it (MACD) will change every tick. Why does this surprise you?

 
whroeder1:

The candle was forming when you printed the value. The close and everything derived from it (MACD) will change every tick. Why does this surprise you?

Hi whroeder1,  I am printing the MACD values as soon as the "NewBar" is formed (the "IsNewBar()" function in my sample code is used for that purpose). In my mind, that means that the old bar is now "complete" and a new bar is starting to form, and the old bar cannot change anymore.  You say that "The candle was forming when you printed the value", does it mean my IsNewBar function is not working as described above and I am printing the values "too soon"?