Script to get value of indicator ATR - page 2

 

Personally, I'm a big fan of Mladen Rakic's self-contained ATR calculation:


Forum on trading, automated trading systems and testing trading strategies

most efficient way to call daily ATR from lower timeframes?

Mladen Rakic, 2017.12.25 21:12

Using handles to indicators like that tends to be clumsy (what if we decide to change the time frame or the period "on the fly" - new handle? ... yeeah ...)

More or less mt5 is making us code a bit more to get better results - ATR is a simple case (excluding the time frame and the period, it is not so much lines of code after all, and allows us flexibility that handle usage would not let us). I did not bother with the case explanation when there is less data available than the ATR desired period+1 rates available in the target time frame data - that is the case that built in ATR does not solve intuitively, and this simple code does that better (at least that is my opinion ...). Making this a function is no big deal either and will work better, faster, and in more flexible way than the built in ATR ...

ENUM_TIMEFRAMES _atrTimeFrame = PERIOD_D1;
int             _atrPeriod    = 20;
double          _atrValue     = 0;
   MqlRates _rates[]; int _ratesCopied = CopyRates(_Symbol,_atrTimeFrame,0,_atrPeriod+1,_rates);
                      if (_ratesCopied>0)
                           for (int i=1; i<_ratesCopied; i++) _atrValue += MathMax(_rates[i].high,_rates[i-1].close)-MathMin(_rates[i].low,_rates[i-1].close);
                                                              _atrValue /= MathMax(_ratesCopied,1);


 
@Ryan L Johnson #Personally, I'm a big fan of Mladen Rakic's self-contained ATR calculation:

The example you have quoted, may be simpler and self-contained, but it will give different ATR values. It is still acceptable but the values will not be the same.

That is because the implementation you have shown is equivalent to a Simple Moving Average, while Wilder's original is based on an Exponential Moving Average with a (1/p) weight (which is what iATR uses).

However, it is still possible to implement the original Wilder calculation in the EA without depending on calling an Indicator function. It will however be a little more complex than the example you have quoted.

 
Fernando Carreiro #:

The example you have quoted, may be simpler and self-contained, but it will give different ATR values. It is still acceptable but the values will not be the same.

That is because the implementation you have shown is equivalent to a Simple Moving Average, while Wilder's original is based on an Exponential Moving Average with a (1/p) weight (which is what iATR uses).

However, it is still possible to implement the original Wilder calculation in the EA without depending on calling an Indicator function. It will however be a little more complex than the example you have quoted.

No worries. I have used the general framework of that code snippet to embed several entirely different indicator formulas within custom functions, and they are more efficient than standard function/iCustom calls. This merely expands upon possible implementation.