Different values from Moving Average indicator and its value plotted in the chart

 
Hi everybody,
I'm taking my first steps in MQL5, in particular I'm testing the moving average indicator.

I wrote the following lines of code to print the moving average value as soon as I receive a new H1 candle (with period "14", calculated on H1 timeframe, with mode EMA and on PRICE_CLOSE).

static int numCompletedBarsH1 = INT_MIN;

void OnTick() {
   
   int currentH1bars = Bars(Symbol(), PERIOD_H1);   
   if (numCompletedBarsH1 != INT_MIN && currentH1bars != numCompletedBarsH1) {
        
          int handle_MQL5 = iMA (Symbol (), PERIOD_H1, 14, 0, MODE_EMA, 0);
          double movingAverageValues[];   
          ArraySetAsSeries (movingAverageValues, true);
          if (CopyBuffer(handle_MQL5,0, 0, 100, movingAverageValues) < 0){
                 Print("CopyBuffer error =", GetLastError());
          } // if
          Print ("Moving average value: ", movingAverageValues[0]);    
   } // if
   numCompletedBarsH1 = currentH1bars;
} //OnTick
   
Now if I run my EA with the Strategy Tester with the attached configuration, I can see my moving average printed in the standard output.

The problem is that the values that are printed are different from the MA14 that are shown in the candlesticks chart.

Some examples:

H1 candle of 2020.05.25 11:00:00 -> the code prints: "MQL4 moving average value: 1.088937786897514" but in the candlestick chart I can read the value 1.089115 that is different from the value calculated from my code.

Also in all the others candlestick I'm experiencing a difference of values between the moving average calulated in the code and the one plotted in the chart.

Am I missing something? Why is there this difference?

Thanks in advance :)
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
Files:
 

Because the latest prices are constantly changing over the course of an hour, the value of moving average also changes.

This program calculates the moving average only once when the number of bars changes, and then does not calculate until the next change of number of bars.

I think maybe that's the difference.
 

Thanks a lot @Nagisa Unada

I thought that the candle with shift == 0 was the candle that have just completed when my code printed the output but the one I needed was the candle with shift == 1.

Thanks a lot and have a nice day! :)



Nagisa Unada:

Because the latest prices are constantly changing over the course of an hour, the value of moving average also changes.

This program calculates the moving average only once when the number of bars changes, and then does not calculate until the next change of number of bars.

I think maybe that's the difference.
 
  1. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  2. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
              How to call indicators in MQL5 - MQL5 Articles (2010)

Reason: