Indicator history changes when a new bar is shown

 

Hi there,

I have this indicator which draws SR lines from fractal levels.

I appears as below and looks like it is working.

 

 Then, I go in to Strategy Tester and start testing a system using the indicator.

It starts off as expected as below.

 

 The lines are being drawn at the same levels in Strategy Test as in the regular view. 

Then I press F12 for the next bar to appear and the red line has changed its position as below.

 

 The code for the indicator is below:

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

int      i;
int      DeadPeriod=20;
int      HigherTF;
double   UpperFractal;
double   LowerFractal;
double   UpperLine;
double   LowerLine;
int      NewHighStartBar;
int      NewLowStartBar;
double   HighLevel;
double   LowLevel=99999;
double   Hv[];
double   Lv[];

string   text;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,Red);
   SetIndexDrawBegin(0,i);
   SetIndexBuffer(0,Hv);
   SetIndexLabel(0,"Resistance");

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,Blue);
   SetIndexDrawBegin(1,i);
   SetIndexBuffer(1,Lv);
   SetIndexLabel(1,"Support");
//---
   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[])
  {
//---

   i=Bars;

   while(i>=0)
     {

      UpperLine=NormalizeDouble(iCustom(NULL,HigherTF,"AG Keltner Channel",0,i+2),8);
      LowerLine=NormalizeDouble(iCustom(NULL,HigherTF,"AG Keltner Channel",2,i+2),8);

      UpperFractal=NormalizeDouble(iFractals(Symbol(),HigherTF,MODE_UPPER,i+1),8);
      LowerFractal=NormalizeDouble(iFractals(Symbol(),HigherTF,MODE_LOWER,i+1),8);

      //Find new highs
      if(UpperFractal>UpperLine && 
         UpperFractal>HighLevel)
        {
         NewHighStartBar=i;
         HighLevel=UpperFractal;
        }
      if(UpperFractal>UpperLine && 
         NewHighStartBar-i>=DeadPeriod)
        {
         NewHighStartBar=i;
         HighLevel=UpperFractal;
        }

      if(HighLevel>0) Hv[i+1]=HighLevel;


      //Find new lows
      if(LowerFractal<LowerLine && 
         LowerFractal<LowLevel &&
         LowerFractal>0)
        {
         NewLowStartBar=i;
         LowLevel=LowerFractal;
        }
      if(LowerFractal<LowerLine && 
         NewLowStartBar-i>=DeadPeriod &&
         LowerFractal>0)
        {
         NewLowStartBar=i;
         LowLevel=LowerFractal;
        }

      if(LowLevel<99999) Lv[i+1]=LowLevel;

      i--;
     }//End While i>=0

   Comment(text);

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

 Can someone please advise how to deal with this?

Thanks. 

 

So?

UpperFractal=NormalizeDouble(iFractals(Symbol(),HigherTF,MODE_UPPER,i+1),8);

fractals can appeirs with pause 2-5 bars.+from higher tf is 2-5bars*(HigherTF/CurrentTF)= bars can be redraw.

 

You can't do anything with this. 

Reason: