Objects repaint and vanish

 

When I place my EA , my objects repaint or vanish and sometimes appear again after sometime. 

What seems to be the problem?

void OnTick()
 {
  if(TimeCurrent()==Time[0])
  {
   int s = iLowest(Symbol() ,0 , MODE_LOW , 75 ,1+40);
   
   ObjectDelete(0 ,"s");
   ObjectCreate(0 ,"s" ,OBJ_RECTANGLE ,0 ,Time[0] ,Close[s] ,0 ,Low[s]);
   ObjectMove(0 ,"s" ,0 ,Time[0] ,Close[s]);
   ObjectSetInteger(0 ,"s" ,OBJPROP_COLOR ,Green);
   
   int s2 = iLowest(Symbol() ,0 ,MODE_LOW ,150 ,2+20);
   
   ObjectDelete(0 ,"s2");
   ObjectCreate(0 ,"s2" ,OBJ_RECTANGLE ,0 ,Time[0] ,Close[s2] ,0 ,Low[s2]);
   ObjectMove(0,"s2",0 ,Time[0] ,Close[s2]);
   ObjectSetInteger(0,"s2" ,OBJPROP_COLOR ,Green);
  }
  
 }
 
  if(TimeCurrent()==Time[0])

That assumes that a tick arrives in the first second of a new bar. There can be minutes between ticks during the Asian session

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)

 
William Roeder #:

That assumes that a tick arrives in the first second of a new bar. There can be minutes between ticks during the Asian session

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)

 I still encounter the same problem, my objects still  repaint or vanish. I don't want them to repaint or vanish at all, I want them to remain in the same place.

void OnTick()
 {
  static datetime CurTime;
  datetime PreTime=CurTime;
  CurTime=Time[0];
  bool NewBar=CurTime!=PreTime;
  if(NewBar)
  {
   int s = iLowest(Symbol() ,0 , MODE_LOW , 75 ,1+40);
   
   ObjectDelete(0 ,"s");
   ObjectCreate(0 ,"s" ,OBJ_RECTANGLE ,0 ,Time[0] ,Close[s] ,0 ,Low[s]);
   ObjectMove(0 ,"s" ,0 ,Time[0] ,Close[s]);
   ObjectSetInteger(0 ,"s" ,OBJPROP_COLOR ,Green);
   
   int s2 = iLowest(Symbol() ,0 ,MODE_LOW ,150 ,2+20);
   
   ObjectDelete(0 ,"s2");
   ObjectCreate(0 ,"s2" ,OBJ_RECTANGLE ,0 ,Time[0] ,Close[s2] ,0 ,Low[s2]);
   ObjectMove(0,"s2",0 ,Time[0] ,Close[s2]);
   ObjectSetInteger(0,"s2" ,OBJPROP_COLOR ,Green);
  }
  
 }
 
Scalper8 # I still encounter the same problem, my objects still  repaint or vanish. I don't want them to repaint or vanish at all, I want them to remain in the same place.

You are deleting and recreating the objects every time. Don't!

Create them only once, and then update the properties (position on the chart), only when they need to be updated.

 

Here is an example. It was compiled but not tested ...

// Default de-initialisation event handler
   void OnDeinit( const int reason )
   {
      ObjectDelete( 0 , "s1" );
      ObjectDelete( 0 , "s2" );
   };

// Default tick event handler
   void OnTick()
   {
      // Check for new bar (compatible with both MQL4 and MQL5).
         static datetime dtBarCurrent  = WRONG_VALUE;
                datetime dtBarPrevious = dtBarCurrent;
                         dtBarCurrent  = iTime( _Symbol, _Period, 0 );
                bool     bNewBarEvent  = ( dtBarCurrent != dtBarPrevious );

      // React to a new bar event and handle it.
         if( bNewBarEvent )
         {
            int
               s1 = iLowest( _Symbol, 0, MODE_LOW,  75, 1 + 40 ),
               s2 = iLowest( _Symbol, 0, MODE_LOW, 150, 2 + 20 );

            if( dtBarPrevious == WRONG_VALUE )
            {
               ObjectCreate(     0 ,"s1", OBJ_RECTANGLE, 0, Time[0], Close[s1], 0, Low[s1] );
               ObjectCreate(     0 ,"s2", OBJ_RECTANGLE, 0, Time[0], Close[s2], 0, Low[s2] );
               ObjectSetInteger( 0 ,"s1", OBJPROP_COLOR, clrGreen );
               ObjectSetInteger( 0, "s2", OBJPROP_COLOR, clrGreen );
            }
            else
            {
               ObjectMove( 0, "s1", 0, Time[0], Close[s1] );
               ObjectMove( 0, "s1", 1, 0,       Low[s1]   );
               ObjectMove( 0, "s2", 0, Time[0], Close[s2] );
               ObjectMove( 0, "s2", 1, 0,       Low[s2]   );
            };
         };
   };
 
Fernando Carreiro #:

Here is an example. It was compiled but not tested ...

Thanks for helping out but I have tested your code and I still encounter the repaints and objects vanishing again. 
 
Scalper8 #: Thanks for helping out but I have tested your code and I still encounter the repaints and objects vanishing again. 

It is not repainting. However, I wish to call your attention to a problem in your own logic about how rectangles are "painted". They have pseudo-transparency and mix colours.

When the rectangles overlap, they will cancel each other out. When both the "s1" and "s2" identify the same lowest point, they will overlap completely and become invisible.

So, use two different colours for each rectangle, for example clrGreen and clrBlue, so that when they overlap you have a 3rd colour.



 
Fernando Carreiro #:

It is not repainting. However, I wish to call your attention to a problem in your own logic about how rectangles are "painted". They have pseudo-transparency and mix colours.

When the rectangles overlap, they will cancel each other out. When both the "s1" and "s2" identify the same lowest point, they will overlap completely and become invisible.

So, use two different colours for each rectangle, for example clrGreen and clrBlue, so that when they overlap you have a 3rd colour.



I was waiting for the markets to open so I can be sure that everything works perfectly. On back testing I was not convinced if the objects will overlap or not. Thank you for your help!
Reason: