Why do My INDICATOR repaints

 

I see last time to my charts on 12:30 AM Tuesday, There was 2 Arrows showing on the chart, But when I see again on 10:30 AM , I can see only 1 Arrow on my chart , CAD-CHF MONDAY 18 DEC 2017

Why do my Indicator REPAINTS like this ??? Please help me to correct this.

Also on CAD-JPY M5 MONDAY 18 DEC 2017  11:30 PM GMT+2   There was 2 Arrows, But when I Load the chart at 10:30 AM 19 Dec 2017 I can see 3 Arrows ??? Why is this happening , any Idea ?  PLEASE HELP


I did try to reload the charts by changing the chart to be shown on screen from 150000 to 600 bars


//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 2
#property indicator_color1 RoyalBlue
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 2
#property indicator_color2 OrangeRed
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern ENUM_DAY_OF_WEEK DAY = MONDAY;

       
datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | MTF @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | MTF @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 233);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 234);
  //watermark();
  
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   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;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      
      if (i >= MathMin(9999999-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      //Indicator Buffer 1
      
    
      int y = iBarShift(NULL, PERIOD_H1, Time[1+i], false);
      
      int d = iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,2+y) - iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y);
      int d2 = iRSI(NULL,0,3,PRICE_CLOSE,2+i) - iRSI(NULL,0,3,PRICE_CLOSE,1+i);
      int d3 = iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y) - iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,2+y);
      int d4 = iRSI(NULL,0,3,PRICE_CLOSE,1+i) - iRSI(NULL,0,3,PRICE_CLOSE,2+i);
      
      
     if( iHigh(NULL,PERIOD_H1,y) > iHigh(NULL,PERIOD_H1,1+y)
      && iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,y) > iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y)
      && iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,2+y) > iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y)
      && d > 8
      && iRSI(NULL,0,3,PRICE_CLOSE,i) > iRSI(NULL,0,3,PRICE_CLOSE,1+i)
      && iRSI(NULL,0,3,PRICE_CLOSE,2+i) > iRSI(NULL,0,3,PRICE_CLOSE,1+i)
      && d2 > 8
      && iRSI(NULL,0,3,PRICE_CLOSE,i) < 50
      
      )
     
        { if( (ENUM_DAY_OF_WEEK)TimeDayOfWeek(Time[i])== DAY)
        Buffer1[i] = Low[i] - 0.5 * myPoint; //Set indicator value at Candlestick Low - fixed value
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = 0;
        }  
        

        
        //Indicator Buffer 2
      if( iLow(NULL,PERIOD_H1,y) < iLow(NULL,PERIOD_H1,1+y)
      && iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,y) < iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y)
      && iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,2+y) < iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y)
      && d3 > 8
      && iRSI(NULL,0,3,PRICE_CLOSE,i) < iRSI(NULL,0,3,PRICE_CLOSE,1+i)
      && iRSI(NULL,0,3,PRICE_CLOSE,2+i) < iRSI(NULL,0,3,PRICE_CLOSE,1+i)
      && d4 > 8
      && iRSI(NULL,0,3,PRICE_CLOSE,i) > 50
      
      )
     
     
    
        { if( (ENUM_DAY_OF_WEEK)TimeDayOfWeek(Time[i])== DAY)    
         Buffer2[i] = High[i] + 0.5 * myPoint; //Set indicator value at Candlestick High + fixed value
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = 0;
        }
      
     }
   return(rates_total);
  }
 
pkeylb:

I see last time to my charts on 12:30 AM Tuesday, There was 2 Arrows showing on the chart, But when I see again on 10:30 AM , I can see only 1 Arrow on my chart , CAD-CHF MONDAY 18 DEC 2017

Why do my Indicator REPAINTS like this ??? Please help me to correct this.

Also on CAD-JPY M5 MONDAY 18 DEC 2017  11:30 PM GMT+2   There was 2 Arrows, But when I Load the chart at 10:30 AM 19 Dec 2017 I can see 3 Arrows ??? Why is this happening , any Idea ?  PLEASE HELP


I did try to reload the charts by changing the chart to be shown on screen from 150000 to 600 bars


Because you do not do the "cleaning" part outside the conditions

Add these at the top of your loop :

Buffer1[i] = EMPTY_VALUE;
Buffer2[i] = EMPTY_VALUE;
 
Mladen Rakic:

Because you do not do the "cleaning" part outside the conditions

Add these at the top of your loop :

is my code ok now ???



//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 2
#property indicator_color1 RoyalBlue
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 2
#property indicator_color2 OrangeRed
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern ENUM_DAY_OF_WEEK DAY = MONDAY;

       
datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | MTF @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | MTF @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 233);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 234);
  //watermark();
  
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   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;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      
      if (i >= MathMin(9999999-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      //Indicator Buffer 1
      Buffer1[i] = EMPTY_VALUE;
      Buffer2[i] = EMPTY_VALUE;
    
      int y = iBarShift(NULL, PERIOD_H1, Time[1+i], false);
      
      int d = iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,2+y) - iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y);
      int d2 = iRSI(NULL,0,3,PRICE_CLOSE,2+i) - iRSI(NULL,0,3,PRICE_CLOSE,1+i);
      int d3 = iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y) - iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,2+y);
      int d4 = iRSI(NULL,0,3,PRICE_CLOSE,1+i) - iRSI(NULL,0,3,PRICE_CLOSE,2+i);
      
      
     if( iHigh(NULL,PERIOD_H1,y) > iHigh(NULL,PERIOD_H1,1+y)
      && iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,y) > iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y)
      && iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,2+y) > iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y)
      && d > 8
      && iRSI(NULL,0,3,PRICE_CLOSE,i) > iRSI(NULL,0,3,PRICE_CLOSE,1+i)
      && iRSI(NULL,0,3,PRICE_CLOSE,2+i) > iRSI(NULL,0,3,PRICE_CLOSE,1+i)
      && d2 > 8
      && iRSI(NULL,0,3,PRICE_CLOSE,i) < 50
      
      )
     
        { if( (ENUM_DAY_OF_WEEK)TimeDayOfWeek(Time[i])== DAY)
        Buffer1[i] = Low[i] - 0.5 * myPoint; //Set indicator value at Candlestick Low - fixed value
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = 0;
        }  
        

        
        //Indicator Buffer 2
      if( iLow(NULL,PERIOD_H1,y) < iLow(NULL,PERIOD_H1,1+y)
      && iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,y) < iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y)
      && iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,2+y) < iRSI(NULL,PERIOD_H1,3,PRICE_CLOSE,1+y)
      && d3 > 8
      && iRSI(NULL,0,3,PRICE_CLOSE,i) < iRSI(NULL,0,3,PRICE_CLOSE,1+i)
      && iRSI(NULL,0,3,PRICE_CLOSE,2+i) < iRSI(NULL,0,3,PRICE_CLOSE,1+i)
      && d4 > 8
      && iRSI(NULL,0,3,PRICE_CLOSE,i) > 50
      
      )
     
     
    
        { if( (ENUM_DAY_OF_WEEK)TimeDayOfWeek(Time[i])== DAY)    
         Buffer2[i] = High[i] + 0.5 * myPoint; //Set indicator value at Candlestick High + fixed value
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = 0;
        }
      
     }
   return(rates_total);
  }
 
Aimak:

Why not using PRICE_OPEN instead or avoid bar[0]? No repainting will happen

Sorry I didnt get you sir, can u put that in my code and show me. I will appriciate
 
Aimak:

These are the changes I would try for making it non-repainting

Let me try this and will let you know if it works
 
pkeylb:
Let me try this and will let you know if it works

When I try to load that indicator, Now I can see some new problem, I see few Arrows which are hidden at the bottom, initially they were not there, I dont know why did they appear ???

some hidden arrows

 
Aimak:

Why not using PRICE_OPEN instead or avoid bar[0]? No repainting will happen

Why?

Current bar update is not repainting - it never was - and that is not the issue he had

 
pkeylb:

is my code ok now ???


Yes - as far as repainting is concerned
 
Mladen Rakic:
Yes - as far as repainting is concerned
I can see some strange arrows , how to remove them
 
pkeylb:
I can see some strange arrows , how to remove them

Adjust the part where you assign values to arrow buffers

Also, unless you have explicitly set the empty value to be 0, do not use 0 instead of EMPTY_VALUE. Use EMPTY_VALUE

 
Aimak:

PRICE_CLOSE is changing at bar[0] until bar ends. 

Does it? I have a suggestion then : why don't we leave the current bar out of the chart completely? That way nothing will "repaint" and we will not be "confused" why the hell does the current indicator value change when the current price changes too


That changing is not repainting. Repainting is and always was a coding error. Limiting the work of any tool to only closed bars because the current bar "repaints" is not leading to any meaningful solution. Using signals for a closed bar is a widely accepted solution but if you use open price for calculation then you have limited yourself to using, in 99.9999% of cases, something very similar to the close of the previous bar for calculation, instead of using all the available prices. Judge for yourself where does that lead
Reason: