Indicator Buffers Doubles Arrows at every new each bar

 

Hi Everyone.

I am coding an indicator to work on offline renko charts. It shows arrows when certain criteria met. When I run it at first no problem. But after created new renko bar, arrows are doubled and when I check data field on chart I see it is filled the buffers  with previous bars data. 

What is the problem? Code is below , thanks in advance


//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2022, CyberDev"
#property description "CandleChangesMedian"
#property strict

//--- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 5
#property  indicator_color1  Blue
#property  indicator_color2  Black
#property  indicator_color3  Green
#property  indicator_color4  Red
#property  indicator_width1  1
#property  indicator_width2  1
#property  indicator_width3  1
#property  indicator_width4  1




//--- indicator parameters
input double ATRmultiplier=0.05;
input int ATRperiod=14;
input bool changecolorarrow=true;

//--- indicator buffers
double    ExtMacdBuffer[];
double    Up1[],Up2[];
double    Dn1[],Dn2[],UPA[],DNA[];
double    Signal[];
datetime lasttime=0;
datetime lasttime2=0;
int lfvg=0;
int la=0;
//--- right input parameters flag
bool      ExtParameters=false;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(Digits+1);
//--- drawing settings


   SetIndexStyle(0,DRAW_ARROW);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexStyle(4,DRAW_NONE);

   SetIndexArrow(0,115);
   SetIndexArrow(1,115);
   SetIndexArrow(2,233);
   SetIndexArrow(3,234);



//--- indicator buffers mapping
   SetIndexBuffer(0,Up1);
   SetIndexBuffer(1,Dn1);
   SetIndexBuffer(2,UPA);
   SetIndexBuffer(3,DNA);
   SetIndexBuffer(4,Signal);


   SetIndexLabel(0,"High");
   SetIndexLabel(1,"Low");
   SetIndexLabel(2,"UPA");
   SetIndexLabel(3,"DNA");
   SetIndexLabel(4,"Signal");






   return(INIT_SUCCEEDED);
  }

int deinit()

  {



   int obj_total=ObjectsTotal();
   string name;
   int c=0;

   while(c<10)
     {
      for(int i=0; i<obj_total; i++)
        {
         name = ObjectName(i);
         string x= StringSubstr(name,0,3);
         if(x== "FVG")
            ObjectDelete(0,name);
        }
      c++;
     }
//ObjectsDeleteAll(0,OBJ_RECTANGLE);
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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;
//---
   if(prev_calculated==0)
     {
      limit = rates_total-60;
     }
   else
     {
      limit=rates_total-prev_calculated;
     }
     
   //limit = rates_total-60;


   for(int i=limit; i>=0; i--)
     {
      double atr=iATR(NULL,0,ATRperiod,i+1);
      // ExtMacdBuffer[i+1]=mp(i+1);



      int lw= iLowest(NULL,0,MODE_LOW,3,i+1);
      int hw= iHighest(NULL,0,MODE_HIGH,3,i+1);

      int a=2;

      if(lw==i+a)
        {
         Up1[i+a]=Low[i+a];
        }

      if(hw==i+a)
        {
         Dn1[i+a]=High[i+a];
        }

      if(Up1[i+1]<100000 && Low[lastf(i+1,1)]<Low[i+1])
        {
         if(changecolorarrow)
            UPA[i+1]= Low[i+1]-atr*0.2;
         Signal[i+1]=1;
         string ups= StringSubstr(Symbol(),0,6)+"RENKO UP";
         if(lasttime2!=Time[i+1]  && prev_calculated!=0)
           {
            SendNotification(ups);
            lasttime2=Time[i+1];
           }

         /*string t3="FVG GREEN"+IntegerToString(i+1);
         ObjectCreate(0,t3,OBJ_RECTANGLE,0,Time[i+1]-60,Close[i+1],Time[i+1],High[i+2]);
         ObjectSetInteger(0,t3,OBJPROP_COLOR,Gold);
         double mid1= (Close[i+1]+High[i+2])/2;
         string t1="FVG GREEN MIDLINE"+IntegerToString(i+1);
         ObjectCreate(0,t1,OBJ_TREND,0,Time[i+1]-60,mid1,Time[i+1]+60,mid1);
         ObjectSetInteger(0,t1,OBJPROP_COLOR,Black);
         ObjectSetInteger(0,t1,OBJPROP_RAY_RIGHT,False);
         ObjectSetInteger(0,t1,OBJPROP_WIDTH,2);*/

        }
      if(Dn1[i+1]<100000 && High[lastf(i+1,-1)]>High[i+1])
        {
         if(changecolorarrow)
            DNA[i+1]= High[i+1]+atr*0.2;
         Signal[i+1]=-1;
         string dns= StringSubstr(Symbol(),0,6)+"RENKO DOWN";
         if(lasttime2!=Time[i+1] && prev_calculated!=0)
           {
            SendNotification(dns);
            lasttime2=Time[i+1];
           }

         /*
         string t4="FVG RED"+IntegerToString(i+1);
         ObjectCreate(0,t4,OBJ_RECTANGLE,0,Time[i+1]-60,Close[i+1],Time[i+1],Low[i+2]);
         ObjectSetInteger(0,t4,OBJPROP_COLOR,Gray);
         double mid2= (Close[i+1]+Low[i+2])/2;
         string t2="FVG RED MIDLINE"+IntegerToString(i+1);
         ObjectCreate(0,t2,OBJ_TREND,0,Time[i+1]-60,mid2,Time[i+1]+60,mid2);
         ObjectSetInteger(0,t2,OBJPROP_COLOR,Blue);
         ObjectSetInteger(0,t2,OBJPROP_RAY_RIGHT,False);
         ObjectSetInteger(0,t2,OBJPROP_WIDTH,2);*/
        }


     }



//------------------------------
   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
double s(int i)
  {
   return(MathAbs(NormalizeDouble((Close[i]-Open[i]),Digits))*10);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double per(int i)
  {
   double body= MathAbs(NormalizeDouble((Close[i]-Open[i]),Digits))*10;
   double wc= MathAbs(NormalizeDouble((High[i]-Low[i]),Digits))*10;
   if(wc<=0)
      return(100);
   double ratio = (body/wc)*100;
   return(ratio);


  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double mp(int i)
  {
   return(NormalizeDouble((Open[i]+Close[i])/2,Digits));
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double cm(int i)
  {
   return(NormalizeDouble((High[i]+Low[i])/2,Digits));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int R(int i)
  {
   if(Close[i]>Open[i])
      return(1);
   if(Close[i]<Open[i])
      return(-1);
   else
      return(0);

  }
//+------------------------------------------------------------------+
int r(int i)
  {
   if(Close[i]>Open[i])
      return(1);
   if(Close[i]<Open[i])
      return(-1);
   else
      return(0);

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double upw(int i)
  {
   double h=High[i];
   double t=MathMax(Open[i],Close[i]);
   double rv= NormalizeDouble((h+t)/2,Digits);
   return(rv);
  }

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

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double dnw(int i)
  {
   double l=Low[i];
   double t=MathMin(Open[i],Close[i]);
   double rv= NormalizeDouble((l+t)/2,Digits);
   return(rv);
  }
//+------------------------------------------------------------------+
//CHECK FOR LAST RED BAR
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int rb(int i)
  {
   int x=i;
   int count=0;

   while(count<=10)
     {
      if(r(x)>0)
         break;
      count++;
      x++;
     }

   return(count);
  }
//+------------------------------------------------------------------+
//CHECK FOR LAST GREEN BAR
//+------------------------------------------------------------------+
int gb(int i)
  {
   int x=i;
   int count=0;

   while(count<=10)
     {
      if(r(x)<0)
         break;
      count++;
      x++;
     }

   return(count);
  }
//+------------------------------------------------------------------+
//Combine Last Three Bar For UP
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double ltbu(int i)
  {
   double v1 = MathMax(Open[i],Close[i]);
   double v2 = MathMax(Open[i+1],Close[i+1]);
   double v3 = MathMax(Open[i+2],Close[i+2]);
   double v4 = MathMax(v1,v2);
   double v5 = MathMax(v4,v3);
   return(v5);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//Combine Last Three Bar For DOWN
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double ltbd(int i)
  {
   double v1 = MathMin(Open[i],Close[i]);
   double v2 = MathMin(Open[i+1],Close[i+1]);
   double v3 = MathMin(Open[i+2],Close[i+2]);
   double v4 = MathMin(v1,v2);
   double v5 = MathMin(v4,v3);
   return(v5);
  }
//+------------------------------------------------------------------+

///lowest


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int hi(int i)
  {
   return(iHighest(NULL,0,MODE_HIGH,10,i));
  }



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int lo(int i)
  {
   return(iLowest(NULL,0,MODE_LOW,10,i));
  }



//-------------------------------------


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

//+------------------------------------------------------------------+
int lastf(int i,int k)
  {
   int x=i+1;
   int count=0;


   while(count<=50)
     {
      if(Up1[x]<100000 && k==1)
         return(x);
      if(Dn1[x]<100000 && k==-1)
         return(x);
      count++;
      x++;
     }

   return(0);

  }

//+------------------------------------------------------------------+
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
  • www.mql4.com
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
Reason: