Stop clearing previous CreatedObjects

 

Hi

This should be simple, my code just draws an X under the OpenPrice of the last 100 candles.
My code does this but it keeps clearing the previous X's I would like it to keep them on screen,
as in I would like all 100 X's to stay on the screen. Thanks

int x;
string name = "text_object";

void OnTick()
{ 
   for (x=100; x>0; x--)
   {
      double OpenPrice=Open[x];
  
      ObjectCreate(name, OBJ_TEXT, 0, 0, 0, 0, 0);
      ObjectSetText(name, "X");
      ObjectSet(name, OBJPROP_COLOR, Blue);
      ObjectSet(name, OBJPROP_TIME1, Time[0]-x*Period()*60);
      ObjectSet(name, OBJPROP_PRICE1, OpenPrice);   

      Comment("Bar=",x);
          // I've added the next line to slow the code execution down so you can see the X's being redrawn, or it just shows the final X
      Sleep(300);
  }
   
   return;
}
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
tiffentrade: my code just draws an X under the OpenPrice of the last 100 candles.

It does not. It draws one object. Objects must have unique names.

 
William Roeder:

It does not. It draws one object. Objects must have unique names.

OK how do I fix that then? put the names in an array?

 
tiffentrade:

Right I've solved my own problem (with help from Williams suggestion). Didnt need to use an array either.

int x;
string name;

void OnTick()
{ 
   for (x=100; x>0; x--)
   {
      double OpenPrice=Open[x];
      name="Text_Object";
      name=name+x; //Add value to Text_Object to make name unique
      ObjectCreate(name, OBJ_TEXT, 0, 0, 0, 0, 0);
      ObjectSetText(name, "X");
      ObjectSet(name, OBJPROP_COLOR, Blue);
      ObjectSet(name, OBJPROP_TIME1, Time[0]-x*Period()*60);
      ObjectSet(name, OBJPROP_PRICE1, OpenPrice);   

      Comment("Bar=",x,"\nObject Unique Name: ",name);
      // slow the code execution for testing
      Sleep(200);
  }
   
   return;
}
 
tiffentrade: Right I've solved my own problem (with help from Williams suggestion). Didnt need to use an array either.
  1. Close, but you used as-series indexes. When a new bar start you will try to create name+0 but you have already used that name.

    To make the names unique, either use time or non-series index (Bars-1-index).


  2. You used Time[0] (a constant) for all objects.

Reason: