Why the second line-object is not auto-updated as programmed !

 
I have run the block of code as below:

//------------------------------------------------------------   
static double LastTop, LastBottom;

   if (ObjectFind("Topline")==-1)
        { 
         ObjectCreate("Topline",OBJ_HLINE,0,30,Top);
         LastTop=Top;
        }
   if (LastTop!=Top) 
         {   
          ObjectDelete("Topline");
          LastTop=Top;
         }
 
   if (ObjectFind("Bottom")==-1)
        { 
         ObjectCreate("BottomBline",OBJ_HLINE,0,30,Bottom);
         LastBottom=Bottom;
        }
   if (LastBottom!=Bottom) 
         {   
          ObjectDelete("Bottomline");
          LastBottom=Bottom;
         }
//------------------------------------------------------------   

The purpose is to draw 2 horizontal lines at Top and Bottom values (defined by the EA) 

Of course, Top and Bottom will sooner or later changed their values.

So the coding above is intended to update (redraw) each line whenever its value changes

Output is ok with 2 line drawn exactly at the values expected.

But I soon saw that just one of the two lines is redrawn when their corresponding values change.

When I delete manually a line which is not updated as expected, then it is redrawn correctly (at next quote).

So what is wrong with my coding above? or what is wrong with MQL4 ? Please ..., Thank you.

 

Don't keep deleting the lines, you just need to move them . . read the Documentation about Object Functions

In particular ObjectSet()

 
RaptorUK:
Don't keep deleting the lines, you just need to move them
And always test your return codes
void HLine(string name, double P0, color clr){  //      #define WINDOW_MAIN 0
    if (!Show.Objects)  return;
    if      (ObjectMove( name, 0, Time[0], P0 )){}
    else if (!ObjectCreate( name, OBJ_HLINE, WINDOW_MAIN, Time[0], P0 ))
        Alert("ObjectCreate(",name,",HLINE) failed: ", GetLastError() );
    if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
        Alert("ObjectSet(", name, ",Color) [1] failed: ", GetLastError() );
    if (!ObjectSetText(name, PriceToStr(P0), 10))
        Alert("ObjectSetText(",name,") [3] failed: ", GetLastError());
}
from my code
 

Well you started off ok

   if (ObjectFind("Topline")==-1)
        { 
         ObjectCreate("Topline",OBJ_HLINE,0,30,Top);
         LastTop=Top;
        }
   if (LastTop!=Top) 
         {   
          ObjectDelete("Topline");
          LastTop=Top;
         }

You used "Topline" in all three cases.

Then you went NUTS

 
   if (ObjectFind("Bottom")==-1)
        { 
         ObjectCreate("BottomBline",OBJ_HLINE,0,30,Bottom);
         LastBottom=Bottom;
        }
   if (LastBottom!=Bottom) 
         {   
          ObjectDelete("Bottomline");
          LastBottom=Bottom;
         }

and used

three different names

for what should be the same thing. Why not just stick with "Bottomline"?

 

Dabbler is right ! "Bottomline" instead of "Bottom". This error is just a name mismatch. I didnot check and correct the mismatch

before asking you. I am sorry for disturbing and thank all of you, especially DABBLER.