Values differ on Indicator and on EA

 

Hello,

I've developped an indicator in mql4.

However when i call it in an EA, eg:

trend = iCustom(NULL, 0, "ATR Stop", 2, 0);

bull_stop_loss = iCustom(NULL, 0, "ATR Stop", 0, 0);

The values are different when i attach the indicator to a chart and when i use it in a custom EA.

Any reason why?

 

If ATRStop is based on Open Price, then

A) you won't have this problem

B) the following does not apply

The indicator on the chart will show value base on Close, High or Low value. These usually vary during the time that the bar is the latest/current. So what you see on your chart is the indicator showing the value based on the bar's 'latest value' for Close, High or Low.

Whilst the bar was being created (which is for the duration of Period), those values vary up & down. So (for example) your chart for time T yesterday shows an ATR Stop value of say 1.2345, but your EA saw of value of say 1.2356, because the Low dropped during that bar; the EA saw a Low that gave an ATR Stop of 1.2356 half way through the bar and used that, but by the time that the bar finished drawing, the Low had dropped further, and calc-ed a value of 1.2345

If you want to verify (or disprove) my reasoning, change your custom ATR Stop to just use OPEN price; I predict that the values for EA use & Chart display will be the same.

HTH

BTW, if you disprove by reasoning, I'll be disappointed, confused, wiser, or else realise that ATR Stop uses some logic that I haven't anticipated!

 

I had a similar problem, but with a ZigZag indicator. I tried using Bar=1 when calling the indicator via iCustom, but it always brought back different values! Found a workaround though, using GlobalVariables. :D

Reason: