ATR Value From EA different to ATR Value on chart

 

Hi, I'm new to mt4 but not to programming, I'm working on a EA and one thing I've noticed is that the value for ATR from the EA is different to the ATR on the chart window, see attached screenshot


This is my code in the EA

double atr = iATR(NULL, 0, 14, 0);
Print("ATR FROM EA = ",NormalizeDouble(atr,8));


In the attached screenshot on the 19th October at 03:00 you will see

  • ATR on the chart shows "0.0014"
  • ATR in the terminal shows "0.00131929"

Now if the value in the chart is using the "NormalizeDouble" function then surely it would round down and show "0.0013"


The issue is the same when I compare the ATR values for other hours, any help or suggestions would be greatly appreciated.


Files:
atr.png  132 kb
 
chillydk147:

Hi, I'm new to mt4 but not to programming, I'm working on a EA and one thing I've noticed is that the value for ATR from the EA is different to the ATR on the chart window, see attached screenshot


This is my code in the EA


In the attached screenshot on the 19th October at 03:00 you will see

  • ATR on the chart shows "0.0014"
  • ATR in the terminal shows "0.00131929"

Now if the value in the chart is using the "NormalizeDouble" function then surely it would round down and show "0.0013"


The issue is the same when I compare the ATR values for other hours, any help or suggestions would be greatly appreciated.


It depends when you call iATR() in your EA. E.G. if you call it at the beginning of the candle you get a different result comparing to the chart value (there are values for finished candles). The ATR is changing during the candle lasting.

 
Petr Nosek:

It depends when you call iATR() in your EA. E.G. if you call it at the beginning of the candle you get a different result comparing to the chart value (there are values for finished candles). The ATR is changing during the candle lasting.

Thanks for your response, I'm calling the code inside the EA "OnTick()" function. How am I meant to know whether the code I execute in the EA is at the beginning or end of a candle?
 
chillydk147:
Thanks for your response, I'm calling the code inside the EA "OnTick()" function. How am I meant to know whether the code I execute in the EA is at the beginning or end of a candle?

Without your code, I can't help you. But I guess (from your log), the code is executed at the beginning of a candle. You should execute iATR() for the last finished candle.

double atr = iATR(_Symbol, _Period, 14, 1);
Print("ATR[1] FROM EA = ",NormalizeDouble(atr,8));

Don't forget your log will be shifted. At 2018.10.19 04:00:00 you will get the ATR for 1-Hour candle 2018.10.19 03:00:00 .

 
Petr Nosek:

Without your code, I can't help you. But I guess (from your log), the code is executed at the beginning of a candle. You should execute iATR() for the last finished candle.

Don't forget your log will be shifted. At 2018.10.19 04:00:00 you will get the ATR for 1-Hour candle 2018.10.19 03:00:00 .

Thanks so much, I never realised the log was shifted, all my values from the EA now match up with the value from the Chart, thanks again
Reason: