ATR Value in MQL5

 

Hi everyone, apologies in advance for the frustrated rant.

I want to know the D1 14 period ATR in MQL5, for use in an EA. That's all. In MQL4, it was a simple, one-line instruction: "ATRD1 = iATR(Symbol(),PERIOD_D1,14,0);"

But in MQL5, it seems to be an absolutely insurmountable development challenge. Dozens of lines of code. Umpteen arrays, buffers, handles, preprocessing, error handling, clearance/destruction after utilisation, otherwise the entire EA/platform/machine will flake out. I mean... seriously?! For a single value? This, in my opinion, typifies why MQL5 struggles to gain traction.

Sarcastic, self-satisfied programming gods on here, don't waste your time. I've seen the way you belittle others with simple queries on here and frankly, I'd rather continue to struggle than put up with your attitude. But any fellow mortals who feel this pain, advocate a simple solution and have found a way, I would be most appreciative to hear from. Thank you!

 
youcrazykids:

Hi everyone, apologies in advance for the frustrated rant.

I want to know the D1 14 period ATR in MQL5, for use in an EA. That's all. In MQL4, it was a simple, one-line instruction: "ATRD1 = iATR(Symbol(),PERIOD_D1,14,0);"

But in MQL5, it seems to be an absolutely insurmountable development challenge. Dozens of lines of code. Umpteen arrays, buffers, handles, preprocessing, error handling, clearance/destruction after utilisation, otherwise the entire EA/platform/machine will flake out. I mean... seriously?! For a single value? This, in my opinion, typifies why MQL5 struggles to gain traction.

Sarcastic, self-satisfied programming gods on here, don't waste your time. I've seen the way you belittle others with simple queries on here and frankly, I'd rather continue to struggle than put up with your attitude. But any fellow mortals who feel this pain, advocate a simple solution and have found a way, I would be most appreciative to hear from. Thank you!

So from my High Grounds:

You use iATR in OnInit() to retrieve a handle. You save it in a global variable of type int.

In OnTick () you retrieve the value from the indicator with CopyBuffer(), and use it as required.

In OnDeinit () you return the handle with IndicatorRelease ().

It's not very difficult, but I suggest you also take care of error code handling as well as return value handling of the functions I mentioned. You then are enabled to take action on these errors.

For example, if the creation fails, you can print out the parameters to iATR and the error code. Also you can return INIT_FAILED so that the EA gets unloaded.

In OnTick(), if CopyBuffer failed for some reason, you can stop processing and wait for the next tick to come in, and continue processing then, if everything works fine...

I would say, this is more complicated than MQL4, but also more reliable.
Reason: