iATR in mql5

Wai Li
28
Wai Li  

hi all,


from what i read in the documentation its the Average Trading Range, and as I imagine the value should be a single number.


But when i use iATR I reailized that the result value is an array instead, should I only use the first ([0]) value of the array?

Thanks.

Vladimir Karputov
Moderator
233642
Vladimir Karputov  
Wai Li :

hi all,


from what i read in the documentation its the Average Trading Range, and as I imagine the value should be a single number.


But when i use iATR I reailized that the result value is an array instead, should I only use the first ([0]) value of the array?

Thanks.

Example (for the iMA indicator) - displaying the last three values on the chart

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2020.11.13 06:01

Receiving data from an indicator in an MQL5.

Scheme:

Getting data from indicators in an MQL5 Expert Advisor

An example for the iMA indicator:


William Roeder
24230
William Roeder  
Wai Li: from what i read in the documentation its the Average Trading Range, and as I imagine the value should be a single number.

It is an average — it changes with every new bar. Just like a moving average.

Wai Li: should I only use the first ([0]) value of the array?

That value is the average of the last length-1 bars plus the forming bar. The forming bar starts at zero length. Don't use zero (except, perhaps, near the end of period).

Wai Li
28
Wai Li  
Vladimir Karputov:

Example (for the iMA indicator) - displaying the last three values on the chart



thanks but i found that seems the array do not need to use ArraySetAsSeries() , looks like the result buffer is already starting from the right most candle.
Wai Li
28
Wai Li  
William Roeder:

It is an average — it changes with every new bar. Just like a moving average.

That value is the average of the last length-1 bars plus the forming bar. The forming bar starts at zero length. Don't use zero (except, perhaps, near the end of period).

thanks a lot, and here is my code, I found that when calling iATR , unless i put 0 for the last param, all the other values I put would result in the same array, is it true? and I find that i don't actually need to reverse the ATR[] as it starts from the right most ( most recent ) candle.

Please let me know if I am wrong, thanks.


void CheckATR(){

  double ATR[];
  ArrayResize(ATR,50);
  hATR = iATR(_Symbol,_Period,15);
  //ArraySetAsSeries( ATR, true);
  CopyBuffer( hATR, 0, 0, 40, ATR );  //take number of average value
  for ( int i=0 ; i<ArraySize(ATR) ; i++ ){
      Print("the ATR " + i + " is " + ATR[i]);
  }
}
Wai Li
28
Wai Li  

also with the above code, the results are different from the indicator on the chart . Here is the print out , from the chart, the ATR(15) is 0.468 or similar value. But the print() I can only got values above 100 (e.g. 106.054 ...etc ) , so whats the mistake i have made?

Files:
atr.jpg 207 kb
Vladimir Karputov
Moderator
233642
Vladimir Karputov  
Wai Li :

thanks but i found that seems the array do not need to use ArraySetAsSeries() , looks like the result buffer is already starting from the right most candle.

You should always use ArraySetAsSeries (***, true) - this way the index '0' in the array will correspond to the rightmost bar on the chart (of course, if you copy from the '0' bar). Study the example I gave.

William Roeder
24230
William Roeder  
Wai Li: , and here is my code,

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. 2004
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
          How to call indicators in MQL5 - MQL5 Articles 12 March 2010
Vladimir Karputov
Moderator
233642
Vladimir Karputov  
Wai Li :

thanks a lot, and here is my code, I found that when calling iATR , unless i put 0 for the last param, all the other values I put would result in the same array, is it true? and I find that i don't actually need to reverse the ATR[] as it starts from the right most ( most recent ) candle.

Please let me know if I am wrong, thanks.

void CheckATR(){

  double ATR[];
  ArrayResize(ATR,50);
  hATR = iATR(_Symbol,_Period,15);
  //ArraySetAsSeries( ATR, true);
  CopyBuffer( hATR, 0, 0, 40, ATR );  //take number of average value
  for ( int i=0 ; i<ArraySize(ATR) ; i++ ){
      Print("the ATR " + i + " is " + ATR[i]);
  }
}

Please read the help. Please read the examples given to you. In MQL5, the indicator handle MUST BE RECEIVED ONCE !!!. You cannot create a new indicator at every tick!