indicator not working properly in strategy tester

 

so i made some modifications to supertrend and added a way to track fair value gap, but for some reason, while the indicator does work in live charts, it doesnt in the strategy tester, and that makes it so that i can not backtest an EA that i made based on it. I checked and there are none of the usual suspects when it comes to this issue.

as for the values for the fair value gap being 0,i tried making them EMPTY_VALUE and -1 and nothing worked


//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property version   "1.00"


#property indicator_chart_window
#property indicator_buffers 10
#property indicator_plots 2

#property indicator_label1  "SuperTrend"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrGreen, clrRed
#property indicator_width1  2

#property indicator_label2  "Fair Value Gap"
#property indicator_type2   DRAW_FILLING
#property indicator_color2  clrYellowGreen, clrYellow
#property indicator_width2  1

input int    Periods=240;
input double Multiplier=0.8;

double SuperTrend[];
double ColorBuffer[];
double fvgupper[];
double fvglower[];
double fvgmiddle[];
double Atr[];
double Up[];
double Down[];
double Middle[];
double trend[];

int atrHandle;
int changeOfTrend;
int flag;
int flagh;


bool fvgDrawn = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,SuperTrend,INDICATOR_DATA);
   SetIndexBuffer(1,ColorBuffer,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,fvgupper,INDICATOR_DATA);
   SetIndexBuffer(3,fvglower,INDICATOR_DATA);
   SetIndexBuffer(4,fvgmiddle,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,Atr,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,Up,INDICATOR_CALCULATIONS);
   SetIndexBuffer(7,Down,INDICATOR_CALCULATIONS);
   SetIndexBuffer(8,Middle,INDICATOR_CALCULATIONS);
   SetIndexBuffer(9,trend,INDICATOR_CALCULATIONS);
   
   
   
   
         
   atrHandle=iATR(_Symbol,_Period,Periods);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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 to_copy;
   if(prev_calculated>rates_total || prev_calculated<0)
      to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0)
         to_copy++;
     }
   if(IsStopped())
      return(0); //Checking for stop flag
   if(CopyBuffer(atrHandle,0,0,to_copy,Atr)<=0)
     {
      return(0);
     }

   int first;
   if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of calculation of an indicator
     {
      first=Periods; // starting index for calculation of all bars
     }
   else
     {
      first=prev_calculated-1; // starting number for calculation of new bars
     }
   for(int i=first; i<rates_total && !IsStopped(); i++)
     {
      Middle[i]=(high[i]+low[i])/2;
      Up[i]  = Middle[i] +(Multiplier*Atr[i]);
      Down[i]= Middle[i] -(Multiplier*Atr[i]);
      // checks the current and previous trend and sets a value as a flag to detect change
      if(close[i]>Up[i-1])
        {
         trend[i]=1;
         if(trend[i-1]==-1)
            changeOfTrend=1;

        }
      else
         if(close[i]<Down[i-1])
           {
            trend[i]=-1;
            if(trend[i-1]==1)
               changeOfTrend=1;
           }
         else
            if(trend[i-1]==1)
              {
               trend[i]=1;
               changeOfTrend=0;
              }
            else
               if(trend[i-1]==-1)
                 {
                  trend[i]=-1;
                  changeOfTrend=0;
                 }

      if(trend[i]<0 && trend[i-1]>0)
        {
         flag=1;
        }
      else
        {
         flag=0;
        }

      if(trend[i]>0 && trend[i-1]<0)
        {
         flagh=1;
        }
      else
        {
         flagh=0;
        }

      if(trend[i]>0 && Down[i]<Down[i-1])
         Down[i]=Down[i-1];

      if(trend[i]<0 && Up[i]>Up[i-1])
         Up[i]=Up[i-1];

      if(flag==1)
         Up[i]=Middle[i]+(Multiplier*Atr[i]);

      if(flagh==1)
         Down[i]=Middle[i]-(Multiplier*Atr[i]);

      if(fvgDrawn && changeOfTrend==1)
        {
         fvglower[i] = 0;
         fvgupper[i] = 0;
         fvgmiddle[i] = 0;
         fvgDrawn = false;
        }
      if(fvgDrawn)
        {
         fvglower[i] = fvglower[i - 1];
         fvgupper[i] = fvgupper[i - 1];
         fvgmiddle[i] = fvgmiddle[i - 1];
        }
      //-- Draw the indicator
      if(trend[i]==1)
        {
         SuperTrend[i]=Down[i];
         if(changeOfTrend==1)
           {
            SuperTrend[i-1]=SuperTrend[i-2];
            changeOfTrend=0;
           }
         ColorBuffer[i]=0.0;
        }
      else
         if(trend[i]==-1)
           {
            SuperTrend[i]=Up[i];
            if(changeOfTrend==1)
              {
               SuperTrend[i-1]= SuperTrend[i-2];
               changeOfTrend = 0;
              }
            ColorBuffer[i]=1.0;
           }
      if(trend[i] == 1 && trend[i-2] == 1 && !fvgDrawn)
        {
         if(high[i - 2] < low[i])
           {
            
            fvgupper[i] = low[i];
            fvglower[i] = high[i - 2];
            fvgmiddle[i] = (fvglower[i] + fvgupper[i]) / 2;
            fvgDrawn = true;
           }

        }
      else
         if(trend[i] == -1 && trend[i-2] == -1 && !fvgDrawn)
           {
            if(low[i - 2] > high[i])
              {
               
               fvgupper[i] = high[i];
               fvglower[i] = low[i - 2];
               fvgmiddle[i] = (fvglower[i] + fvgupper[i]) / 2;
               fvgDrawn = true;
              }

           }
     }

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

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

any insight would be great

 

Indicators don't work properly in the strategy tester when the loop starts at prev_calculated-1


Redefine your "first" variable

   int first;
   int bars_back = 1000;

   if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of calculation of an indicator
     {
      first=Periods; // starting index for calculation of all bars
     }
   else
     {
      first= rates_total - bars_back; // starting number for calculation of new bars
     }
 
Conor Mcnamara #:

Indicators don't work properly in the strategy tester when the loop starts at prev_calculated-1


Redefine your "first" variable

Thank you so much, that was just the issue that i was running into. Appreciate your time, your solution solved it for me