how is the moving average calculated by iATR ?

 

hello all,


I am supposed to re-implement iATR in Python cuz we want to show its values on web.


I have been trying to do this as described by the wiki page 


https://en.wikipedia.org/wiki/Average_true_range


But, unfortunately the numbers don't match up. 


Can somebody please confirm if how the ATR is calculated by iATR ? Particularly how moving average of the True Range is calculated.


Please let me know how I can get numbers right.

Average true range - Wikipedia
Average true range - Wikipedia
  • en.wikipedia.org
The range of a day's trading is simply . The true range extends it to yesterday's closing price if it was outside of today's range. The true range is the largest of the: Most recent period's high minus the most recent period's low Absolute value of the most recent period's high minus the previous close Absolute value of the most recent...
 
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int i,limit;
//--- check for bars count and input parameter
   if(rates_total<=InpAtrPeriod || InpAtrPeriod<=0)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtATRBuffer,false);
   ArraySetAsSeries(ExtTRBuffer,false);
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
//--- preliminary calculations
   if(prev_calculated==0)
     {
      ExtTRBuffer[0]=0.0;
      ExtATRBuffer[0]=0.0;
      //--- filling out the array of True Range values for each period
      for(i=1; i<rates_total; i++)
         ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
      //--- first AtrPeriod values of the indicator are not calculated
      double firstValue=0.0;
      for(i=1; i<=InpAtrPeriod; i++)
        {
         ExtATRBuffer[i]=0.0;
         firstValue+=ExtTRBuffer[i];
        }
      //--- calculating the first value of the indicator
      firstValue/=InpAtrPeriod;
      ExtATRBuffer[InpAtrPeriod]=firstValue;
      limit=InpAtrPeriod+1;
     }
   else
      limit=prev_calculated-1;
//--- the main loop of calculations
   for(i=limit; i<rates_total; i++)
     {
      ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
      ExtATRBuffer[i]=ExtATRBuffer[i-1]+(ExtTRBuffer[i]-ExtTRBuffer[i-InpAtrPeriod])/InpAtrPeriod;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
If you need a moving average you just add all the x period bars and divide the end result by the same amount (period) to get the average.
 
Marco vd Heijden:
If you need a moving average you just add all the x period bars and divide the end result by the same amount (period) to get the average.

I see this is a predefined indicator under indicator's folder ....


1) is it really how iATR calculated ?


2) what is rates_total for ?


Thank you

 

I think I found some of the definitions here:


https://docs.mql4.com/basis/function/events

Event Handling Functions - Functions - Language Basics - MQL4 Reference
Event Handling Functions - Functions - Language Basics - MQL4 Reference
  • docs.mql4.com
The MQL4 language provides processing of some predefined events. Functions for handling these events must be defined in a MQL4 program; function name, return type, composition of parameters (if there are any) and their types must strictly conform to the description of the event handler function. The event handler of the client terminal...
 
Thats the code for the ATR indicator you can extract the formula from within you do not need to use rates_total.
 

Thanks it worked for me .... I was missing the correct formula....

Also, I am wondering if there is any better way to import indicator values to mysql database .... 

I have come across this post which allows to run the strategy tester from command line ... but the thing is that we sometime need to show case our EAs using specific data scenarios and then plot them on the web. 

Do you guys offer sth to take care of such cases?


Thank you very much

 

The formula in wikipedia is how it's calculated in the tradingview platform.

ATR in the metatrader is calculated using simple moving average.