Problems when customizing an ADX indicator

 
Hello,

I'm trying to build a modification of the ADX. I am only interested in the values of D+ and D-. In order to improve the visibility of a signal, as soon as one of the values is higher than the other and additionally a threshold is exceeded, a color change should happen.

I seem to have a problem with the ADXBuffer, but I don't understand which one yet.

//+------------------------------------------------------------------+
//|                                            ADX_Hist.mq5 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   1
//+------------------------------------------------------------------+
input int                  InpMAPeriod       =           14;    // MA period
input ENUM_APPLIED_VOLUME  InpAppliedVolume  =  VOLUME_TICK;    // Volumes
input int                  inpHistWidth      =            4;    // Histogramwidth
input color                inpColor          =clrDodgerBlue;    // Color
input color                inpColorUP        =      clrLime;    // Color UP
input color                inpColorDN        =       clrRed;    // Color DN
input color                inpColorOD        =      clrGray;   
input double               Threshold         =           40;
//+------------------------------------------------------------------+
int      inpLineWidth = 1;
int      MAHandle;
double   ADXBuffer[];
double   DI_plusBuffer[];
double   DI_minusBuffer[];
double   ADXColors[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0,ADXBuffer,INDICATOR_DATA);                                                                           // indicator buffers mapping
   SetIndexBuffer(1,DI_plusBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,DI_minusBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,ADXColors,INDICATOR_COLOR_INDEX);

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod);                        // sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,inpLineWidth);
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_COLOR_HISTOGRAM);
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,inpHistWidth);
   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,3);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,inpColorUP);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,inpColorDN);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,inpColorOD);


   IndicatorSetString(INDICATOR_SHORTNAME,"ADX Hist("+string(InpMAPeriod)+")");  // name for DataWindow and indicator subwindow label

   MAHandle=iADX(NULL,0,InpMAPeriod);
              // get MA handle
   if(MAHandle==INVALID_HANDLE)                                               // check MA handle
      {
         Alert("*ERROR* creating iMA handle");
         return(INIT_FAILED);
      }
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Force Index                                                      |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{
   int i,start,to_copy;

   if(rates_total<InpMAPeriod)               // check for rates total
      return(0);                             // try again next tick

   if(BarsCalculated(MAHandle)<rates_total)  // check for MA ready
      return(0);                             // try again next tick
                                             // calculate amount of data needed
   to_copy=(prev_calculated==0)?rates_total:rates_total-prev_calculated+1;
   
//   if(CopyBuffer(MAHandle,0,0,to_copy,ADXBuffer)!=to_copy)   // get ma buffer
//      return(0);
   if(CopyBuffer(MAHandle,1,0,to_copy,DI_plusBuffer)!=to_copy)   // get ma buffer
      return(0);
   if(CopyBuffer(MAHandle,2,0,to_copy,DI_minusBuffer)!=to_copy)   // get ma buffer
      return(0);
                                             // preliminary calculations
   start=(prev_calculated<InpMAPeriod)?InpMAPeriod:prev_calculated-1;

   if(InpAppliedVolume==VOLUME_TICK)         // the main loop of calculations
      {
         for(i=start; i<rates_total && !IsStopped(); i++)
            {              
               if(DI_plusBuffer[i] > DI_minusBuffer[i]){
                  if(DI_plusBuffer[i] > Threshold){
                     ADXColors[i]=0;
                  }else{
                     ADXColors[i]=2;
                  }
                  ADXBuffer[i]=DI_plusBuffer[i];
                  Print("DI_plusBuffer["+i+"]*"+DI_plusBuffer[i]+"*");
               }else{
                  if(DI_minusBuffer[i] > Threshold){
                     ADXColors[i]=1;
                  }else{
                     ADXColors[i]=2;
                  }
                  ADXBuffer[i]=DI_minusBuffer[i];
                  Print("DI_minusBuffer["+i+"]*"+DI_minusBuffer[i]+"*");
               }
               Print("ADXBuffer["+i+"]*"+ADXBuffer[i]+"*");
            }
      }
   else
      {
         for(i=start; i<rates_total && !IsStopped(); i++)
            {
               if(DI_plusBuffer[i] > DI_minusBuffer[i]){
                  if(DI_plusBuffer[i] > Threshold){
                     ADXColors[i]=0;
                  }else{
                     ADXColors[i]=2;
                  }
                  ADXBuffer[i]=DI_plusBuffer[i];
                  Print("DI_plusBuffer["+i+"]*"+DI_plusBuffer[i]+"*");
               }else{
                  if(DI_minusBuffer[i] > Threshold){
                     ADXColors[i]=1;
                  }else{
                     ADXColors[i]=2;
                  }
                  ADXBuffer[i]=DI_minusBuffer[i];
                  Print("DI_minusBuffer["+i+"]*"+DI_minusBuffer[i]+"*");
               }
               Print("ADXBuffer["+i+"]*"+ADXBuffer[i]+"*");
            }
      }
   return(rates_total);
}

In the log I can see that the ADXBuffer gets the correct values. However, nothing is plotted at all in the indicator itself. 


Thanks,

h4rry

 
h4rry: However, nothing is plotted at all in the indicator itself.

You have plots=1 and only ADX is indicator Data. Stop what you are doing and go back to the original code.

Change plots to two, make the first two buffers (DX±) indicator data. Make ADX the third buffer. Four lines are changed, NO OTHER CODE needs to be changed.

 

Hi William,

thanks for the answer. Did you mean like that?

#property indicator_plots   2

and 

   SetIndexBuffer(0,DI_plusBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,DI_minusBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ADXBuffer,INDICATOR_CALCULATIONS); 
   SetIndexBuffer(3,ADXColors,INDICATOR_COLOR_INDEX);

or 

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


But it doesn't matter. I tested it both ways, but I get the same result. Nothing gets plotted.

 

i could fix it

//+------------------------------------------------------------------+
//|                                            ADX_Hist.mq5 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   2
//+------------------------------------------------------------------+
input int                  InpMAPeriod       =           14;    // MA period
input ENUM_APPLIED_VOLUME  InpAppliedVolume  =  VOLUME_TICK;    // Volumes
input int                  inpHistWidth      =            4;    // Histogramwidth
input color                inpColor          =clrDodgerBlue;    // Color
input color                inpColorUP        =      clrLime;    // Color UP
input color                inpColorDN        =       clrRed;    // Color DN
input color                inpColorOD        =      clrGray;   
input double               Threshold         =           40;
//+------------------------------------------------------------------+
int      inpLineWidth = 1;
int      MAHandle;
double   ADXBuffer[];
double   DI_plusBuffer[];
double   DI_minusBuffer[];
double   ADXColors[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
                                                                          // indicator buffers mapping
   SetIndexBuffer(0,ADXBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,ADXColors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,DI_plusBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,DI_minusBuffer,INDICATOR_CALCULATIONS);

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod);                        // sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,inpLineWidth);
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_COLOR_HISTOGRAM);
   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,inpHistWidth);
   PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,3);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,inpColorUP);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,inpColorDN);
   PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,inpColorOD);


   IndicatorSetString(INDICATOR_SHORTNAME,"ADX Hist("+string(InpMAPeriod)+")");  // name for DataWindow and indicator subwindow label

   MAHandle=iADXWilder(NULL,0,InpMAPeriod);
              // get MA handle
   if(MAHandle==INVALID_HANDLE)                                               // check MA handle
      {
         Alert("*ERROR* creating iADXWilder handle");
         return(INIT_FAILED);
      }
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Force Index                                                      |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{
   int i,start,to_copy;

   if(rates_total<InpMAPeriod)               // check for rates total
      return(0);                             // try again next tick

   if(BarsCalculated(MAHandle)<rates_total)  // check for MA ready
      return(0);                             // try again next tick
                                             // calculate amount of data needed
   to_copy=(prev_calculated==0)?rates_total:rates_total-prev_calculated+1;
   
   if(CopyBuffer(MAHandle,1,0,to_copy,DI_plusBuffer)!=to_copy)   // get ma buffer
      return(0);
   if(CopyBuffer(MAHandle,2,0,to_copy,DI_minusBuffer)!=to_copy)   // get ma buffer
      return(0);
                                             // preliminary calculations
   start=(prev_calculated<InpMAPeriod)?InpMAPeriod:prev_calculated-1;

   if(InpAppliedVolume==VOLUME_TICK)         // the main loop of calculations
      {
         for(i=start; i<rates_total && !IsStopped(); i++)
            {
               if(DI_plusBuffer[i] > DI_minusBuffer[i]){
                  ADXBuffer[i]=DI_plusBuffer[i];
                  if(ADXBuffer[i] > Threshold){
                     ADXColors[i]=0;
                  }else{
                     ADXColors[i]=2;
                  }
               }else{
                  ADXBuffer[i]=DI_minusBuffer[i];
                  if(ADXBuffer[i] > Threshold){
                     ADXColors[i]=1;
                  }else{
                     ADXColors[i]=2;
                  }
               }
            }
      }
   else
      {
         for(i=start; i<rates_total && !IsStopped(); i++)
            {
               if(DI_plusBuffer[i] > DI_minusBuffer[i]){
                  ADXBuffer[i]=DI_plusBuffer[i];
                  if(ADXBuffer[i] > Threshold){
                     ADXColors[i]=0;
                  }else{
                     ADXColors[i]=2;
                  }
               }else{
                  ADXBuffer[i]=DI_minusBuffer[i];
                  if(ADXBuffer[i] > Threshold){
                     ADXColors[i]=1;
                  }else{
                     ADXColors[i]=2;
                  }
               }           
            }
      }
   return(rates_total);
}