//+------------------------------------------------------------------+ //| 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.
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](https://c.mql5.com/36/23/event-handling-functions-functions.png)
- docs.mql4.com
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.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.