for loop - skip last 6 objects on chart, delete all others

 

Hi!

My ea puts opening and closing arrows plus trend lines connecting them on chart.

It names those objects, starting with '#'.

When testing, for the sake of not cluttering the chart, I want to delete all those chart objects (starting with '#') but last 6. I have been riddling this quest for few weeks, no success. The code I use:

// --- DELETE ORDER OBJECTS WHEN TESTING ------------------------------------------------------------------
void DeleteTestingObjects()
  {
   int total=ObjectsTotal()-1;
   for(int i=total;i>=0;i--)
     {
      if(IsStopped())break;
      string name=ObjectName(i);
      if(StringSubstr(name,0,1)=="#") // ObjectDelete(name); // this simply deletes all (but last one)
        {
         for(int cnt=total;cnt>=6;cnt--) // I want skip last 6 objects, delete all others (older)
           {
            ObjectDelete(name);
           }
        }
     }
  }

This is where above code is called from:

void OnTick()
  {
   if(getNumOpenOrders(-1,magic)>0)
     {
...
      if(DeleteTestingOrderArrows && IsTesting())DeleteTestingObjects(); // external bool variable
     }
...
  }


Thank you for your help,

Best regards,

Simon

S love nia

 
This makes no sense, you are trying to delete the same name, total-6 times.
         for(int cnt=total;cnt>=6;cnt--) // I want skip last 6 objects, delete all others (older)
           {
            ObjectDelete(name);
           }
Count how many you've seen.
   int count=0;  // haven't seen any.
   int total=ObjectsTotal()-1;                                                                        
   for(int i=total;i>=0;i--)                                                                          
     {                                                                                                
      if(IsStopped())break;                                                                           
      string name=ObjectName(i);                                                                      
      if(StringSubstr(name,0,1)=="#") // ObjectDelete(name); // this simply deletes all (but last one)
        {      // Found one
         if(++count > 6)                 // I want skip last 6 objects, delete all others (older)     
           {                                                                                          
            ObjectDelete(name);                                                                       
           }                                                                                          
        }                                                                                             
     }                                                                                                

Don't lie with your variable names. Total is not ObjectsTotal() - 1
   int total=ObjectsTotal()-1;
   for(int i=total;i>=0;i--)
iPos is a position index, the last is one less than Total.
 int total=ObjectsTotal();
 for(int iPos=total - 1; iPos >= 0; iPos--)
or if you don't need the total after the loop
 int iPos = ObjectsTotal();
 while(--iPos >= 0)
 
WHRoeder:
This makes no sense, you are trying to delete the same name, total-6 times.
Count how many you've seen.

Don't lie with your variable names. Total is not ObjectsTotal() - 1
iPos is a position index, the last is one less than Total.
or if you don't need the total after the loop

He he, if I would at any moment of my coding life knew that 'total=ObjectsTotal()-1' qualifies as a lie, I swear, I would never write this lie down onto the hard disk!

Thank you for your valuable replies and code bits. I haven't succeed to make the code work as I wanted (hit & miss didn't work this time, lol). Neither is it mandatory. Nevertheless, you Rock! Bow!

Best regards,

Simon

S love nia

 

I guess that for something like this, you could have a globally declared variable

int NameNumber =7;

 Then you could search the Objects for a name beginning with

string to_search_for="#"+IntegerToString(NameNumber)+" ";

 Then, if found, delete all object names beginning with

string to_delete="#"+IntegerToString(NameNumber-6)+" ";

Then

NameNumber++;

Does that make sense? 

Reason: