Indicator not visible in strategy tester

 

Hello, I used the original code from the RSI indicator provided bei MetaQuotes and tried to adjust it to print two RSI in the same window. I basically copied and pasted the original code and added "_1" and "_2". The first indicator is drawn and seems to work but the other is not and I do not understand why. I have no experience with coding indicators.

//+------------------------------------------------------------------+
//|                                                          RSI.mq5 |
//|                   Copyright 2009-2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2009-2017, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description "Relative Strength Index"
//--- indicator settings
#property indicator_separate_window
#property indicator_height 150
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 30
#property indicator_level2 70
#property indicator_buffers 6
#property indicator_plots   2
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE
#property indicator_color1  DodgerBlue
#property indicator_color2  Red
//--- input parameters
input int InpPeriodRSI_1=14; // Period 1
input int InpPeriodRSI_2=8;  // Period 2
//--- indicator buffers
double    ExtRSIBuffer_1[];
double    ExtPosBuffer_1[];
double    ExtNegBuffer_1[];

double    ExtRSIBuffer_2[];
double    ExtPosBuffer_2[];
double    ExtNegBuffer_2[];

int       ExtPeriodRSI_1;
int       ExtPeriodRSI_2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
{
//--- check for input
   if(InpPeriodRSI_1<1 || InpPeriodRSI_2<1)
   {  ExtPeriodRSI_1=8;
      ExtPeriodRSI_2=14;
      Print("Incorrect value(s) for input variable InpPeriodRSI_1 and/or InpPeriodRSI_2. Indicator will use default values of 8 and 14 for calculations");
   }
   else
   {  ExtPeriodRSI_1=InpPeriodRSI_1;
      ExtPeriodRSI_2=InpPeriodRSI_2;
   }

//--- indicator buffers mapping
   SetIndexBuffer(0,ExtRSIBuffer_1,INDICATOR_DATA);
   SetIndexBuffer(1,ExtPosBuffer_1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,ExtNegBuffer_1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,ExtRSIBuffer_2,INDICATOR_DATA);
   SetIndexBuffer(4,ExtPosBuffer_2,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,ExtNegBuffer_2,INDICATOR_CALCULATIONS);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,2);
   PlotIndexSetString(0,PLOT_LABEL,"RSI 1");
   PlotIndexSetString(1,PLOT_LABEL,"RSI 2");
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriodRSI_1);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtPeriodRSI_2);
//--- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME,"RSI("+string(ExtPeriodRSI_1)+"), RSI("+string(ExtPeriodRSI_2)+")");
}
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{  if(rates_total<=ExtPeriodRSI_1 || rates_total<=ExtPeriodRSI_2)
      return(0);

//--- preliminary calculations for 1
   int pos_1=prev_calculated-1;
   if(pos_1<=ExtPeriodRSI_1)
   {  double sum_pos=0.0;
      double sum_neg=0.0;
      //--- first RSIPeriod values of the indicator are not calculated
      ExtRSIBuffer_1[0]=0.0;
      ExtPosBuffer_1[0]=0.0;
      ExtNegBuffer_1[0]=0.0;
      for(int i=1; i<=ExtPeriodRSI_1; i++)
      {  ExtRSIBuffer_1[i]=0.0;
         ExtPosBuffer_1[i]=0.0;
         ExtNegBuffer_1[i]=0.0;
         double diff=price[i]-price[i-1];
         sum_pos+=(diff>0?diff:0);
         sum_neg+=(diff<0?-diff:0);
      }
      //--- calculate first visible value
      ExtPosBuffer_1[ExtPeriodRSI_1]=sum_pos/ExtPeriodRSI_1;
      ExtNegBuffer_1[ExtPeriodRSI_1]=sum_neg/ExtPeriodRSI_1;
      if(ExtNegBuffer_1[ExtPeriodRSI_1]!=0.0)
         ExtRSIBuffer_1[ExtPeriodRSI_1]=100.0-(100.0/(1.0+ExtPosBuffer_1[ExtPeriodRSI_1]/ExtNegBuffer_1[ExtPeriodRSI_1]));
      else
      {  if(ExtPosBuffer_1[ExtPeriodRSI_1]!=0.0)
            ExtRSIBuffer_1[ExtPeriodRSI_1]=100.0;
         else
            ExtRSIBuffer_1[ExtPeriodRSI_1]=50.0;
      }
      //--- prepare the position value for main calculation
      pos_1=ExtPeriodRSI_1+1;
   }
//--- preliminary calculations for 2
   int pos_2=prev_calculated-1;
   if(pos_2<=ExtPeriodRSI_2)
   {  double sum_pos=0.0;
      double sum_neg=0.0;
      //--- first RSIPeriod values of the indicator are not calculated
      ExtRSIBuffer_2[0]=0.0;
      ExtPosBuffer_2[0]=0.0;
      ExtNegBuffer_2[0]=0.0;
      for(int i=1; i<=ExtPeriodRSI_2; i++)
      {  ExtRSIBuffer_2[i]=0.0;
         ExtPosBuffer_2[i]=0.0;
         ExtNegBuffer_2[i]=0.0;
         double diff=price[i]-price[i-1];
         sum_pos+=(diff>0?diff:0);
         sum_neg+=(diff<0?-diff:0);
      }
      //--- calculate first visible value
      ExtPosBuffer_2[ExtPeriodRSI_2]=sum_pos/ExtPeriodRSI_2;
      ExtNegBuffer_2[ExtPeriodRSI_2]=sum_neg/ExtPeriodRSI_2;
      if(ExtNegBuffer_2[ExtPeriodRSI_2]!=0.0)
         ExtRSIBuffer_2[ExtPeriodRSI_2]=100.0-(100.0/(1.0+ExtPosBuffer_2[ExtPeriodRSI_2]/ExtNegBuffer_2[ExtPeriodRSI_2]));
      else
      {  if(ExtPosBuffer_2[ExtPeriodRSI_2]!=0.0)
            ExtRSIBuffer_2[ExtPeriodRSI_2]=100.0;
         else
            ExtRSIBuffer_2[ExtPeriodRSI_2]=50.0;
      }
      //--- prepare the position value for main calculation
      pos_2=ExtPeriodRSI_2+1;
   }

//--- the main loop of calculations for 1
   for(int i=pos_1; i<rates_total && !IsStopped(); i++)
   {  double diff=price[i]-price[i-1];
      ExtPosBuffer_1[i]=(ExtPosBuffer_1[i-1]*(ExtPeriodRSI_1-1)+(diff>0.0?diff:0.0))/ExtPeriodRSI_1;
      ExtNegBuffer_1[i]=(ExtNegBuffer_1[i-1]*(ExtPeriodRSI_1-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI_1;
      if(ExtNegBuffer_1[i]!=0.0)
         ExtRSIBuffer_1[i]=100.0-100.0/(1+ExtPosBuffer_1[i]/ExtNegBuffer_1[i]);
      else
      {  if(ExtPosBuffer_1[i]!=0.0)
            ExtRSIBuffer_1[i]=100.0;
         else
            ExtRSIBuffer_1[i]=50.0;
      }
   }
//--- the main loop of calculations for 2
   for(int i=pos_2; i<rates_total && !IsStopped(); i++)
   {  double diff=price[i]-price[i-1];
      ExtPosBuffer_2[i]=(ExtPosBuffer_2[i-1]*(ExtPeriodRSI_2-1)+(diff>0.0?diff:0.0))/ExtPeriodRSI_2;
      ExtNegBuffer_2[i]=(ExtNegBuffer_2[i-1]*(ExtPeriodRSI_2-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI_2;
      if(ExtNegBuffer_2[i]!=0.0)
         ExtRSIBuffer_2[i]=100.0-100.0/(1+ExtPosBuffer_2[i]/ExtNegBuffer_2[i]);
      else
      {  if(ExtPosBuffer_2[i]!=0.0)
            ExtRSIBuffer_2[i]=100.0;
         else
            ExtRSIBuffer_2[i]=50.0;
      }
   }

//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
}
//+------------------------------------------------------------------+
 
eatrader1231231231231233r5235134:

Hello, I used the original code from the RSI indicator provided bei MetaQuotes and tried to adjust it to print two RSI in the same window. I basically copied and pasted the original code and added "_1" and "_2". The first indicator is drawn and seems to work but the other is not and I do not understand why. I have no experience with coding indicators.

   SetIndexBuffer(0,ExtRSIBuffer_1,INDICATOR_DATA);
   SetIndexBuffer(1,ExtPosBuffer_1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,ExtNegBuffer_1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,ExtRSIBuffer_2,INDICATOR_DATA);
   SetIndexBuffer(4,ExtPosBuffer_2,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,ExtNegBuffer_2,INDICATOR_CALCULATIONS);

should be:

   SetIndexBuffer(0,ExtRSIBuffer_1,INDICATOR_DATA);
   SetIndexBuffer(1,ExtRSIBuffer_2,INDICATOR_DATA);
   SetIndexBuffer(2,ExtPosBuffer_1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,ExtNegBuffer_1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,ExtPosBuffer_2,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,ExtNegBuffer_2,INDICATOR_CALCULATIONS);

The buffers required to draw plots need to be the first buffers you index. Since DRAW_LINE plots is plotted with only 1 buffer each, your first 2 indexed buffers need to be the arrays that plot the first and second RSI, in this order. Once the arrays required to draw each buffer are indexed, the order for the other buffers doesn't matter. If the plots required 2 buffers each, like with DRAW_COLOR_LINE, index 0 would be the first RSI plot values, index 1 would be the color index buffer for that plot, index 2 would be the 2nd RSI's plot values, index 3 would be that plot's color index buffer, and then the rest could be in any order you want. That's how buffers need to be indexed to allow MT5 to know what buffers are used to draw each plot.

Reason: