need a little help with this indicator

 
Hello, I made a little modification of the conventional ADX indicator, I'd like to call it Smoothed ADX, please refer to the pic below for comparison, the upper one is the smoothed ADX, lower one is the normal ADX. I wrote the code in EasyLanguage, but i'm not that proficient in MQL4 programming, I was hoping to have it translated into MQL4, so please give a hand if you can.

As you can see, the upper one looks a lot better and with much less whipsaws. Normally if you smooth a line or price data, there is always a lagging effect since you are taking averages of the data, however this smoothed ADX will NOT incur any lag due to the non-lag smoothing method introduced by John Ehlers. Therefore the Smoothed ADX is more suitable for building trading systems due to less whipsaws. The formular for the smoothed ADX is pretty much the same as the original ADX, only with a couple of addtional statements (highlighted in Bold text), since I'm not that good at MQL4, I suppose you could help me to convert it into MQL4 language?
Here is the code in EasyLanguage format, the codes in Bold format are the parts that responsible for smoothing the DI+, DI- and ADX, the rest of the code is identical to the original ADX indicator. I was trying to convert it into MQL4 language, since I'm not good at programming, so i need a little help. We just need to use the existing ADX formular, and plug the DI+, DI- and ADX into the smoothing calculations(codes in bold format).
Inputs: {declaring inputs}
Length( 14 ),
ADXTrend( 25 ), alpha1(0.25), alpha2(0.33);
variables: {declaring variables}
DMIPlus( 0 ), DMIMinus( 0 ), DMI( 0 ), ADX( 0 ),
DIPlusLead(0), DIMinusLead(0), DIPlusFinal(0), DIMinusFinal(0),
ADXLead(0), ADXFinal(0);

{now calling the built-in ADX functions, so we don't need to calculate them}
Value1 = DirMovement( H, L, C, Length, DMIPlus, DMIMinus, ADX);
{this part is the actual smoothing of the original ADX indicator, DI+, DI- and ADX lines are smoothed}
DIPlusLead = 2*DMIPlus + (alpha1 - 2) * DMIPlus[1] + (1 - alpha1) * DIPlusLead[1];
DIPlusFinal = alpha2*DIPlusLead + (1 - alpha2) * DIPlusFinal[1];
DIMinusLead = 2*DMIMinus + (alpha1 - 2) * DMIMinus[1] + (1 - alpha1) * DIMinusLead[1];
DIMinusFinal = alpha2*DIMinusLead + (1 - alpha2) * DIMinusFinal[1];
ADXLead = 2*ADX + (alpha1 - 2) * ADX[1] + (1 - alpha1) * ADXLead[1];
ADXFinal = alpha2*ADXLead + (1 - alpha2) * ADXFinal[1];
{Plotting them on chart}
Plot1( DIPlusFinal, "DMI+" ) ;
Plot2( DIMinusFinal, "DMI-" ) ;
Plot3( ADXFinal, "ADX" ) ;
I would be grateful if anyone who can help me to code this indicator into MQL4, hope it is not too difficult to achieve, looking forward to see it in light.
cheers
 
No problem:

//+------------------------------------------------------------------+
//|                                                 ADX Smoothed.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net/"
 
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 DarkBlue
#property indicator_color2 FireBrick
#property indicator_color3 DarkGreen
#property indicator_level1 25
//---- input parameters
extern int       per=14;
extern double    alpha1=0.25;
extern double    alpha2=0.33;
extern int       PriceType=0;
//---- buffers
double DiPlusFinal[];
double DiMinusFinal[];
double ADXFinal[];
double DIPlusLead[];
double DIMinusLead[];
double ADXLead[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(6);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,DiPlusFinal);
   SetIndexLabel(0,"Di Plus");
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,DiMinusFinal);
   SetIndexLabel(1,"Di Minus");
 
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ADXFinal);
   SetIndexLabel(2,"ADX");
   
   SetIndexBuffer(3,DIPlusLead);
   SetIndexBuffer(4,DIMinusLead);
   SetIndexBuffer(5,ADXLead);
   
   IndicatorDigits(2);
   IndicatorShortName("ADX("+per+")smothed");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int i,k,limit;
   double DIPlus, DIMinus, ADX, DIPlus1, DIMinus1, ADX1;
//----
   if (counted_bars==0) limit=Bars-per-1;
   if (counted_bars>0)  limit=Bars-counted_bars;
 
   for (i=limit;i>=0;i--)
     {
      DIPlus=iADX(NULL,0,per,PriceType,MODE_PLUSDI,i);
      DIMinus=iADX(NULL,0,per,PriceType,MODE_MINUSDI,i);
      ADX=iADX(NULL,0,per,PriceType,MODE_MAIN,i);
      DIPlus1=iADX(NULL,0,per,PriceType,MODE_PLUSDI,i+1);
      DIMinus1=iADX(NULL,0,per,PriceType,MODE_MINUSDI,i+1);
      ADX1=iADX(NULL,0,per,PriceType,MODE_MAIN,i+1);
     
      DIPlusLead[i]=2*DIPlus + (alpha1 - 2) * DIPlus1 + (1 - alpha1) * DIPlusLead[i+1];
      DIMinusLead[i] = 2*DIMinus + (alpha1 - 2) * DIMinus1 + (1 - alpha1) * DIMinusLead[i+1];
      ADXLead[i] = 2*ADX + (alpha1 - 2) * ADX1 + (1 - alpha1) * ADXLead[i+1];
     }   
   for (i=limit;i>=0;i--)
     {
      DiPlusFinal[i] = alpha2*DIPlusLead[i] + (1 - alpha2) * DiPlusFinal[i+1];
      DiMinusFinal[i] = alpha2*DIMinusLead[i] + (1 - alpha2) * DiMinusFinal[i+1];
      ADXFinal[i] = alpha2*ADXLead[i] + (1 - alpha2) * ADXFinal[i+1];
     }   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+


 
Hi Rosh, many thanks! take care ;-)
 
Rosh,

Please correct me if I am wrong.

You are using the following loop to calculate the smoothed ADX values:

for (i=limit;i>=0;i--) {
DIPlus=iADX(NULL,0,per,PriceType,MODE_PLUSDI,i);
DIMinus=iADX(NULL,0,per,PriceType,MODE_MINUSDI,i);
ADX=iADX(NULL,0,per,PriceType,MODE_MAIN,i);
DIPlus1=iADX(NULL,0,per,PriceType,MODE_PLUSDI,i+1);
DIMinus1=iADX(NULL,0,per,PriceType,MODE_MINUSDI,i+1);
ADX1=iADX(NULL,0,per,PriceType,MODE_MAIN,i+1);

DIPlusLead[i]=2*DIPlus + (alpha1 - 2) * DIPlus1 + (1 - alpha1) * DIPlusLead[i+1];
DIMinusLead[i] = 2*DIMinus + (alpha1 - 2) * DIMinus1 + (1 - alpha1) * DIMinusLead[i+1];
ADXLead[i] = 2*ADX + (alpha1 - 2) * ADX1 + (1 - alpha1) * ADXLead[i+1];
}

Now, assume limit = 50. So for i=50 you do your calcs and find the value for ADXLead[50] and ADXLead[51]. Next you do the same for i=49, and you calculate values for ADXLead[49] and ADXLead[50]. Now, this seems like repainting. So, am I correct in assuming that this indicator will repaint?
 
In MQL indexis of bars incrementing in history. So, to calculate value of indicator on 50 index I use the value of 51 index (which more dept in history) and repainting is absent absolutely. Do you understand me? (my English is not very bad?)
 
Rosh,

I understood your point. Thanks.

Thus, to do this kind of calculations, we have to count down the way you have to avoid repainting.

Thanks again for the nice pointer.
 
Rosh:
No problem:





Dear Coders,

unfortunately i`m not be proficient in MetaTrader programming language,

so i need a little your help, please.

I would like to add the alert when the lines cross ( lines DarkBlue and FireBrick ) and the DarkGreen's

line above 25.

Files:
Reason: