iATR in mql5

 

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.

 
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:


 
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).

 
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.
 
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]);
  }
}
 

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
 
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.

 
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
 
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!