Moving Average of ADX

 

Hello,


i tried to build an indicator in a separate windows that shows two lines. First the ADX indicator and second a moving average of this indicator.

As a result i get a correct ADX line but the Moving Average line is always zero.

Where is the failure/error in the code?

// Indicator
// Berechnung des ADX mit SMA zur Ausbruchsindikation
//--------------------------------------------------------------------
#property indicator_separate_window // Indicator is drawn in the separate window
#property indicator_buffers 2       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line
//--- input parameters
extern int Period_ADX = 25;         // Calculated MA period
extern int Period_MA = 10;          // Calculated MA period
//--- intern variables
double ADX[];                       // Wert Steigung
double SMA[];                       // SMA
//--------------------------------------------------------------------
int init()                          // Special function init()
  {  
   string short_name;
   IndicatorBuffers(2);
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexBuffer(0,ADX);           // Assigning an array to a buffer
   SetIndexBuffer(1,SMA);           // Assigning an array to a buffer
   short_name="ADX("+Period_ADX+") vs. SMA("+Period_MA+")";
   IndicatorShortName(short_name);  // Indicator name
   SetIndexLabel(0,short_name);     // Label im DataWindows
   return;                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int counted_bars;                // Number of counted bars 
   int i;                           // Bar index
   //ArrayInitialize(StMA,0.0);   
//---- initial zero
   counted_bars=IndicatorCounted(); // Number of counted bars
   if(counted_bars<1)
      for(i=1;i<=Period_ADX;i++) ADX[Bars-i]=0.0;
   i=Bars-counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      ADX[i]=iADX(NULL,0,Period_ADX,PRICE_CLOSE,MODE_MAIN,i); // Value of 0 buffer on i bar
      SMA[i]=iMA(NULL,0,Period_MA,0,MODE_SMA,iADX(NULL,0,Period_ADX,PRICE_CLOSE,MODE_MAIN,i),i);    // Value of 1st buffer on i bar
      i--;                          // Calculating index of the next bar
     }
   return;                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------
 
FamWue:

Hello,


i tried to build an indicator in a separate windows that shows two lines. First the ADX indicator and second a moving average of this indicator.

As a result i get a correct ADX line but the Moving Average line is always zero.

Where is the failure/error in the code?

Did you look at the documentation for iMA()  ?   why are using an iADX() call for the Applied Price ?  the Applied Price is an int iADX returns a double,  that in itself should have told you it was wrong.  What you need to use is iMAOnArray()  with the ADX[]  buffer . . . read the documentation.
 
RaptorUK:
Did you look at the documentation for iMA()  ?   why are using an iADX() call for the Applied Price ?  the Applied Price is an int iADX returns a double,  that in itself should have told you it was wrong.  What you need to use is iMAOnArray()  with the ADX[]  buffer . . . read the documentation.

I thougt it worked but now there is only the ADX line and no red SMA line.


Is there something wrong in this code?

//+------------------------------------------------------------------+
//|                                                   ADX vs SMA.mq4 |
//|                                                           FamWue |
//|                                                                  |
//+------------------------------------------------------------------+
//--------------------------------------------------------------------
// Indicator
// Berechnung des ADX mit SMA zur Ausbruchsindikation
//--------------------------------------------------------------------
#property indicator_separate_window // Indicator is drawn in the separate window
#property indicator_buffers 2       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line
//--- input parameters
extern int Period_ADX = 25;         // Calculated MA period
extern int Period_MA = 10;          // Calculated MA period
//--- intern variables
double ADX[];                       // Wert Steigung
double SMA[];                       // SMA
//--------------------------------------------------------------------
int init()                          // Special function init()
  {  
   ArraySetAsSeries(SMA,true);
   string short_name;
   IndicatorBuffers(2);
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexBuffer(0,ADX);           // Assigning an array to a buffer
   SetIndexBuffer(1,SMA);           // Assigning an array to a buffer
   short_name="ADX("+Period_ADX+") vs. SMA("+Period_MA+")";
   IndicatorShortName(short_name);  // Indicator name
   SetIndexLabel(0,short_name);     // Label im DataWindows
   return;                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int counted_bars;                // Number of counted bars 
   int i;                           // Bar index
   //ArrayInitialize(StMA,0.0);   
//---- initial zero
   counted_bars=IndicatorCounted(); // Number of counted bars
   if(counted_bars<1)
      for(i=1;i<=Period_ADX;i++) ADX[Bars-i]=0.0;
   i=Bars-counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      ADX[i]=iADX(NULL,0,Period_ADX,PRICE_CLOSE,MODE_MAIN,i); // Value of 0 buffer on i bar
      SMA[i]=iMAOnArray(ADX,0,Period_MA,0,MODE_SMA,i);    // Value of 1st buffer on i bar
      i--;                          // Calculating index of the next bar
     }
   return;                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------
 
FamWue:

I thougt it worked but now there is only the ADX line and no red SMA line.


Is there something wrong in this code?


Anyone for a help?
 
FamWue:

Anyone for a help?
Print your SMA values and figure out what you did wrong.
 
FamWue:

Anyone for a help?

Get rid of this line . . .

   ArraySetAsSeries(SMA,true);

 . . .  buffers are already series arrays,  and in any case,  the array that needs to be a series array is the ADX buffer.

 

and make this change . . .

   i=Bars-counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
      {
      ADX[i]=iADX(NULL,0,Period_ADX,PRICE_CLOSE,MODE_MAIN,i); // Value of 0 buffer on i bar
      i--;                          // Calculating index of the next bar
      }
   i=Bars-counted_bars-1; 
   while(i>=0)                      // Loop for uncounted bars
      {
      SMA[i]=iMAOnArray(ADX,0,Period_MA,0,MODE_SMA,i);    // Value of 1st buffer on i bar
      i--;                          // Calculating index of the next bar
      }
   return;                          // Exit the special funct. start()
 
RaptorUK:

Get rid of this line . . .

 . . .  buffers are already series arrays,  and in any case,  the array that needs to be a series array is the ADX buffer.

 

and make this change . . .



Thanks a lot. Can you explain me why the last change is different to my code? Now it works but i like to understand why.
 
FamWue:

Thanks a lot. Can you explain me why the last change is different to my code? Now it works but i like to understand why.

To be honest . . .  no I can't.  

What I mean is I don't fully understand how iMAOnArray() works so although I have enough of an idea to try a few things to get it to work I don't understand it well enough to be able to explain it to someone else.  I suspect it is because you are using the whole of array/buffer  ADX  (2nd parameter in the iMAOnArray() call)  rather than just part of it.

 
Since you are counting down, the unspecified elements shouldn't matter. A year ago I couldn't get one of two iMAOnArray to work in my code, never was able to. So I coded my own. RaptorUK may be correct.