problem with (MQL4) iADX to (MQL5) iADX

 

Hello everyone,

Why is there such a big difference?


//+------------------------------------------------------------------+
//|   MQL 4                                                          |
//+------------------------------------------------------------------+
double  iADX(
   string       symbol,        // symbol
   int          timeframe,     // timeframe
   int          period,        // averaging period
   int          applied_price, // applied price
   int          mode,          // line index
   int          shift          // shift
   );
//+------------------------------------------------------------------+
//|   MQL 5                                                          |
//+------------------------------------------------------------------+   
int  iADX( 
   string           symbol,         // symbol name 
   ENUM_TIMEFRAMES  period,         // period 
   int              adx_period      // averaging period 
   );   
   
 
GrumpyDuckMan: Why is there such a big difference?

Indicator functions work differently in MQL5.

In MQL4, they are called and return data directly.

In MQL5, those same function only return a "handle", which is then used by other functions like CopyBuffer() to fetch the relevant data.

Also, in MQL5 there are two variants of iADX, namely iADX() and iADXWilder(). Consult the MQL5 documentation for more details:

 
Fernando Carreiro:

Indicator functions work differently in MQL5.

In MQL4, they are called and return data directly.

In MQL5, those same function only return a "handle", which is then used by other functions like CopyBuffer() to fetch the relevant data.

Also, in MQL5 there are two variants of iADX, namely iADX() and iADXWilder(). Consult the MQL5 documentation for more details:

Thank you

 

Hello everyone,

Sorry if my simplified version of using  iADX  with MQL5.

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 

#property indicator_separate_window 
#property indicator_buffers 3 
#property indicator_plots   3 
//--- plot ADX 
#property indicator_label1  "ADX" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- plot DI_plus 
#property indicator_label2  "DI_plus" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//--- plot DI_minus 
#property indicator_label3  "DI_minus" 
#property indicator_type3   DRAW_LINE 
#property indicator_color3  clrYellow
#property indicator_style3  STYLE_SOLID 
#property indicator_width3  1 

ENUM_TIMEFRAMES TimeFrame=PERIOD_CURRENT;
string symbol=_Symbol;
double ADXBuffer[],DI_plusBuffer[],DI_minusBuffer[];
long   Chart_ID=0;
int    adx_period=14,
ADX_handle,
bars_calculated=0,
values_to_copy;
//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int OnInit()
  {
   ChartSetInteger(Chart_ID,CHART_MODE,CHART_CANDLES);
   ChartSetInteger(Chart_ID,CHART_SHIFT,1);
   ChartSetInteger(Chart_ID,CHART_AUTOSCROLL,true);
   ChartSetInteger(Chart_ID,CHART_SCALE,4);
   ChartSetInteger(Chart_ID,CHART_SHOW_GRID,false);
   ChartSetInteger(Chart_ID,CHART_SHOW_PERIOD_SEP,false);
   ChartSetInteger(Chart_ID,CHART_COLOR_CANDLE_BEAR,clrRed);
   ChartSetInteger(Chart_ID,CHART_COLOR_CANDLE_BULL,clrBlue);
   ChartSetInteger(Chart_ID,CHART_SHOW_ASK_LINE,true);

   SetIndexBuffer(0,ADXBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,DI_plusBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,DI_minusBuffer,INDICATOR_DATA);

   symbol=NULL;
   ADX_handle=iADX(symbol,TimeFrame,adx_period);

   if(ADX_handle==INVALID_HANDLE)
     {
      return(INIT_FAILED);
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
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 calculated=BarsCalculated(ADX_handle);
   if(calculated<=0)
     {
      Alert("Failed",calculated,GetLastError());
      return(0);
     }
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
     {
      if(calculated>rates_total) values_to_copy=rates_total;
      else values_to_copy=calculated;
     }
   else
     {values_to_copy=(rates_total-prev_calculated)+1; }
   if(!AdxFromBuffer(ADXBuffer,DI_plusBuffer,DI_minusBuffer,ADX_handle,values_to_copy))
      return(0);
   bars_calculated=calculated;
   return(rates_total);
  }
//+------------------------------------------------------------------+ 
//| Filling indicator buffers from the iADX indicator                | 
//+------------------------------------------------------------------+ 
bool AdxFromBuffer(double &adx_ADX[],   // indicator buffer of the ADX line 
                   double &ADXplus[],   // indicator buffer for DI+ 
                   double &ADXminus[],  // indicator buffer for DI- 
                   int adx_handle,      // handle of the iADX indicator 
                   int copied_values
                   )
  {
 //  ResetLastError();
   if(CopyBuffer(adx_handle,0,0,copied_values,adx_ADX)<0)
     { return(false); }
   if(CopyBuffer(adx_handle,1,0,copied_values,ADXplus)<0)
     { return(false); }
   if(CopyBuffer(adx_handle,2,0,copied_values,ADXminus)<0)
     { return(false); }
   return(true);
  }
//+------------------------------------------------------------------+ 
//| Indicator deinitialization function                              | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+