ADX of MQL4 don't give same result as ADX of Amibroker

 
Hi There,

I have an automatic trading program written in Amibroker Formula Language. I would like to translate it to the MQL4 language.

I found a diffrence between the results of the ADX indicator of the two languages. I applied the indicator on the same database, same timeframe and same period, but I got diffrent results.

So I would like to know how does this ADX() indicator accuratelly work. I tried it with all type of applied price but no one gave me equal results as the ADX indicator of Amibroker.

I don't know yet the calculating method of the ADX indicator of Amibroker too. But already I asked about it in an another forum of Amibroker.

Please help me. Where can I found detailed information about the above mentioned indicator?

Thank you in advance.

Relative
 
Put code of ADX in language AFL. Also what to you have answered at forum of AmiBroker? Simply interestingly.
 
I don't get answare to my question yet.

The code in AFL is just a simple indicator code:

_SECTION_BEGIN("ADX_13");
 
adx13 = ADX(13) ;
pdi13 = PDI(13) ;
mdi13 = MDI(13) ;
 
//Plot the indicator
Plot( adx13, _DEFAULT_NAME(), ParamColor( "ADX color", colorBlue ), ParamStyle("ADX style", styleThick ) );
Plot( pdi13, "+DI", ParamColor( "+DI color", colorGreen ), ParamStyle("+DI style") );
Plot( mdi13, "-DI", ParamColor( "-DI color", colorRed ), ParamStyle("-DI style") );
 
_SECTION_END();
 
It's not code of indicator.
 
I got answare from the Amibroker Technical Support about the ADX indicator. They sad:

"MQL probably is using non-standard formulation or not correct averaging.
Original ADX uses Wilders smoothing.
For details see: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_directional_index_adx"

MQL4 doesn't use the Wilder's smoothing, does it?

 
Hmm... Did not expect such. If it really answer of developers AmiBroker - that it is bad for them. Instead of direct show of the formula for calculation of the indicator, they are covered with the reference to empty page where it is spoken in general. .. bla-bla-bla... We do not hide a code and we give it directly with MetaTrader 4 distributive.
//+------------------------------------------------------------------+
//|                                                          ADX.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net//"
 
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 YellowGreen
#property indicator_color3 Wheat
//---- input parameters
extern int ADXPeriod=14;
//---- buffers
double ADXBuffer[];
double PlusDiBuffer[];
double MinusDiBuffer[];
double PlusSdiBuffer[];
double MinusSdiBuffer[];
double TempBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 3 additional buffers are used for counting.
   IndicatorBuffers(6);
//---- indicator buffers
   SetIndexBuffer(0,ADXBuffer);
   SetIndexBuffer(1,PlusDiBuffer);
   SetIndexBuffer(2,MinusDiBuffer);
   SetIndexBuffer(3,PlusSdiBuffer);
   SetIndexBuffer(4,MinusSdiBuffer);
   SetIndexBuffer(5,TempBuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("ADX("+ADXPeriod+")");
   SetIndexLabel(0,"ADX");
   SetIndexLabel(1,"+DI");
   SetIndexLabel(2,"-DI");
//----
   SetIndexDrawBegin(0,ADXPeriod);
   SetIndexDrawBegin(1,ADXPeriod);
   SetIndexDrawBegin(2,ADXPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Average Directional Movement Index                               |
//+------------------------------------------------------------------+
int start()
  {
   double pdm,mdm,tr;
   double price_high,price_low;
   int    starti,i,counted_bars=IndicatorCounted();
//----
   i=Bars-2;
   PlusSdiBuffer[i+1]=0;
   MinusSdiBuffer[i+1]=0;
   if(counted_bars>=i) i=Bars-counted_bars-1;
   starti=i;
//----
   while(i>=0)
     {
      price_low=Low[i];
      price_high=High[i];
      //----
      pdm=price_high-High[i+1];
      mdm=Low[i+1]-price_low;
      if(pdm<0) pdm=0;  // +DM
      if(mdm<0) mdm=0;  // -DM
      if(pdm==mdm) { pdm=0; mdm=0; }
      else if(pdm<mdm) pdm=0;
           else if(mdm<pdm) mdm=0;
      //---- вычисляем истинный интервал
      double num1=MathAbs(price_high-price_low);
      double num2=MathAbs(price_high-Close[i+1]);
      double num3=MathAbs(price_low-Close[i+1]);
      tr=MathMax(num1,num2);
      tr=MathMax(tr,num3);
      //---- counting plus/minus direction
      if(tr==0) { PlusSdiBuffer[i]=0; MinusSdiBuffer[i]=0; }
      else      { PlusSdiBuffer[i]=100.0*pdm/tr; MinusSdiBuffer[i]=100.0*mdm/tr; }
      //----
      i--;
     }
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
//---- apply EMA to +DI
   for(i=0; i<=limit; i++)
      PlusDiBuffer[i]=iMAOnArray(PlusSdiBuffer,Bars,ADXPeriod,0,MODE_EMA,i);
//---- apply EMA to -DI
   for(i=0; i<=limit; i++)
      MinusDiBuffer[i]=iMAOnArray(MinusSdiBuffer,Bars,ADXPeriod,0,MODE_EMA,i);
//---- Directional Movement (DX)
   i=Bars-2;
   TempBuffer[i+1]=0;
   i=starti;
   while(i>=0)
     {
      double div=MathAbs(PlusDiBuffer[i]+MinusDiBuffer[i]);
      if(div==0.00) TempBuffer[i]=0;
      else TempBuffer[i]=100*(MathAbs(PlusDiBuffer[i]-MinusDiBuffer[i])/div);
      i--;
     }
//---- ADX is exponential moving average on DX
   for(i=0; i<limit; i++)
      ADXBuffer[i]=iMAOnArray(TempBuffer,Bars,ADXPeriod,0,MODE_EMA,i);
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
Tomorrow I'll specially look for algorithm in the directory.
 

Rosh wrote >>
Tomorrow I'll specially look for algorithm in the directory.

Well, today isn't exactly tomorrow, but hope ADX will be fixed some day. The built-in MT4 version doesn't work right, just try to call it with PRICE_OPEN and zero shift from an expert that printing iADX() buffers' values into log and compare the results with ones shown by attached ADX on a chart with Data Window open to see what I mean. May be in MT5?

 
pisara:

Well, today isn't exactly tomorrow, but hope ADX will be fixed some day. The built-in MT4 version doesn't work right, just try to call it with PRICE_OPEN and zero shift from an expert that printing iADX() buffers' values into log and compare the results with ones shown by attached ADX on a chart with Data Window open to see what I mean. May be in MT5?

When I was looking for the difference between the calculation of ADX at the Amibroker and at the MT4, I had founded. They use different calculation method. Bud I don't remember exactly what was that.

 

Relative wrote >>

When I was looking for the difference between the calculation of ADX at the Amibroker and at the MT4, I had founded. They use different calculation method. Bud I don't remember exactly what was that.

Apart from the difference you mentioned, I was talking about clear bugs in the MT4 internal ADX implementation. I mean when run from the tester on M1 with iADX parameters explicitly set to say M15 and PRICE_OPEN, the results returned will differ from internal ADX indicator's buffers data (that can be seen on Data Window). Seems like the bugs were (re)introduced after fixing the tester's "can see the future" issues. Also, reading ADX indicator's official source code from year 2004 suggests the code shouldn't work without modifications on opening bar prices, as High[0] and Low[0] involved into calculations. My deducted guess is the MT4 is dead end for developers now, and MT5 will inherit the same problems. At least Rosh doesn't willing to follow the subject so far. Until proved otherwise, of course...

 
pisara:

At least Rosh doesn't willing to follow the subject so far. Until proved otherwise, of course...

See 'WildersDMI_v1'

Reason: