EMPTY_VALUE Buffer returning a value in strategy tester

 

Hi friends

Please I have an indicator which have 3 buffers.

The 3rd buffer should return EMPTY_VALUE for buy condition  OR same value as second Buffer in sell condition.

But in strategy tester, the indicator constantly returns thesame value for first and second buffers.

Please help me take a look.

Thanks


//+------------------------------------------------------------------+
//|                                         Slope Direction Line.mq4 |
//|                                               Asikhemhen Osazee  |
//|                                               http://            |
//+------------------------------------------------------------------+
#property copyright "Asikhemhen Osazee"
#property link      "http:/"
#property version   "1.00"
#property strict
#property indicator_chart_window
//---
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color2 Red
#property indicator_color3 LimeGreen
#property indicator_width2 2
#property indicator_width3 2

//---
extern int       period         = 20;
extern double    FilterNumber   = 2;
extern int       ma_method      = 3;
extern int       applied_price  = 0;
//---- buffers
double Buffer0[];
double Buffer1[];
double Buffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   int shift_begin=int(MathSqrt(period)+period+1);
   IndicatorShortName("Slope_Direction_Line("+DoubleToStr(period,0)+")");
   SetIndexBuffer(0,Buffer0);
   SetIndexBuffer(1,Buffer1);
   SetIndexBuffer(2,Buffer2);
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexDrawBegin(1,shift_begin);
   SetIndexDrawBegin(2,shift_begin);
//---
   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 limit=rates_total-prev_calculated;

   if(prev_calculated==0)
     {
      limit--;  //Print("prev_calculated: ",prev_calculated,"    limit: ",limit);
     }
   else
     {
      limit++; //Print("prev_calculated: ",prev_calculated,"    limit: ",limit);
     }
//---
   for(int i = 0; i < limit && !IsStopped(); i++)
      Buffer0[i] = 2 * MA(i, (int)MathRound((double)period / FilterNumber)) - MA(i, period);
//---
   for(int i = 0; i < limit && !IsStopped(); i++)
      Buffer1[i] = iMAOnArray(Buffer0, 0, (int)MathRound(MathSqrt(period)), 0, ma_method, i);

//xm7 - ArraySize adjustment
   if(ArraySize(Buffer0) < 100 || ArraySize(Buffer1) < 100)
     {
      limit = 1000;
      ArrayFree(Buffer0);
      ArrayFree(Buffer1);
      // Initialize Buffer2 array here if not already initialized
      ArrayFree(Buffer2);
      ArrayResize(Buffer0, limit);
      ArrayResize(Buffer1, limit);
      // Resize Buffer2 to have a size of 'limit'
      ArrayResize(Buffer2, limit);
      //---
      for(int i = 0; i < limit && !IsStopped(); i++)
         Buffer0[i] = 2 * MA(i, (int)MathRound((double)period / FilterNumber)) - MA(i, period);
      //---
      for(int i = 0; i < limit && !IsStopped(); i++)
         Buffer1[i] = iMAOnArray(Buffer0, 0, (int)MathRound(MathSqrt(period)), 0, ma_method, i);
     }

//Check array sizes one more time
   if(ArraySize(Buffer0) < 100 || ArraySize(Buffer1) < 100 || ArraySize(Buffer2) < 100)
     {
      Print("Detected either array Buffer0[], Buffer1[] or Buffer2[] size is less than 100, if this continues check code at line: ", __LINE__);
      return(0);
     }

//---
   for(int i = 0; i < limit && !IsStopped(); i++)
     {
      // Ensure we do not access an index out of the bounds of Buffer1
      if(i < limit - 1)
        {
         if(Buffer1[i] > Buffer1[i+1])
            Buffer2[i] = Buffer1[i];
         else
            Buffer2[i] = EMPTY_VALUE;
        }
      else
        {
         // Handle the last element case here
         Buffer2[i] = Buffer1[i]; // Or however you wish to handle the last element
        }
     }

   int error = GetLastError();

//--- return value of prev_calculated for next call
   return(rates_total);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double MA(int shift,int p)
  {
   return(iMA(Symbol(), 0, p, 0, ma_method, applied_price, shift));
  }

//+------------------------------------------------------------------+