Heiken Ashi indicator returns wrong values when called from my MT5 EA

Hi all.

I am getting crazy with this issue.
I have a MT5 EA using the native MT5 Heiken Ashi indicator. It just displays the values of open, high, low, close of the current Heiken Ashi candle.
When applying the EA to a graph which has the indicator applied, values printed by the EA do match the one I see on the graph, in real time.
When launching the EA in back test mode, with option “open price only”, displayed values do not match the Heiken Ashi candles I can see on the graph.

Anybody has a clue why and how I can fix that?

Here is the EA code:

#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
 

 double         bufferOpen[], bufferHigh[], bufferLow[], bufferClose[]; 
 double openH, highH, lowH, closeH;
 
int handle; 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
 handle=iCustom(Symbol(),Period(),"Examples\\Heiken_Ashi");
//---
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
CopyBuffer(handle,0,0,1,bufferOpen); 
CopyBuffer(handle,1,0,1,bufferHigh); 
CopyBuffer(handle,2,0,1,bufferLow);
CopyBuffer(handle,3,0,1,bufferClose);


openH = bufferOpen[0];
highH = bufferHigh[0];
lowH = bufferLow[0];
closeH = bufferClose[0];


printf(openH + " " + highH + " " + lowH + " "+closeH);

return;
}

Thanks in advance for your help!

Because what you see on the chart is calculated at close of the candle, but what you printed is at open.

In ‘Open prices only’, your EA receives 1 tick at open of each bar, but there are 2 ticks generated (and used to draw candles and indicators).

Example:     EURUSD,H1: 768 ticks, 384 bars generated. Environment synchronized in 0:00:00.125. Test passed in 0:00:00.687 (including ticks preprocessing 0:00:00.016).

Use the debugger and place a breakpoint on your last “return”, and check the values.

Thank you so much.

I fixed the issue by modifying the following lines :

CopyBuffer(handle,0,0,2,bufferOpen); 
CopyBuffer(handle,1,0,2,bufferHigh); 
CopyBuffer(handle,2,0,2,bufferLow);
CopyBuffer(handle,3,0,2,bufferClose);

If I understand correctly, in backtest mode “open price only”, for each new candle, 1 tick event is sent to the EA, but 2 ticks are actually registered by the indicator : one for the current candle open, one for the last candle close. Is that correct?

Not exactly, see this article

The Fundamentals of Testing in MetaTrader 5 The Fundamentals of Testing in MetaTrader 5

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…

Thanks for the reference!