string array[] ={"a",...} - ObjectDescription(array); ObjectDelete()

 

Hi!

On chart, I have TREND and HORIZONTAL lines;

'Description' fields are filled with 'orders';

'orders' are recognized by crossedLine() function - if price crosses the line (trend or horizontal), do something...;

'orders' are {"buy", "sell"...};

if line has 'order' "delete_trading_lines", I want to delete those lines with orders (specific ones (4 in number), not all);

For the sake of learning how to code as pro, I want to learn to use arrays (it is about time, I am 'coding' for 4 years now, mas o menos). So, I want to iterate through ObjectsTotal, find those that have NOT empty 'Description' field, and check if one of those 4 'orders' is in 'Description':

// --- DELETE HORIZONTAL LINES ----------------------------------------------------------------------------
void DeleteOrderLines()
  {
//if(ObjectFind(0,"sell") || ObjectFind(0,"buy") || ObjectFind(0,"instant_sell") || ObjectFind(0,"instant_buy")) 
//- trend or horizontal lines with those 'descriptions' should be deleted
   string sOrderLines[]={"buy","sell","instant_buy","instant_sell"}; // initialize/define string array
   int total=ObjectsTotal();
   int iArraySize=ArraySize(sOrderLines);
   for(int i=0;i<total;i++) // iterate through all objects, from first object
     {
      if(IsStopped())break;
      string name=ObjectName(i); // search through object names, and set its name as string
//if((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && ObjectDescription(name)==sOrderLines[4])ObjectDelete(name);
      if((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && ObjectDescription(name)!="") // if those 'types' are found
        {
         for(int cnt=0;cnt<iArraySize;cnt++) // iterate through array size
           {
            if(ObjectDescription(name)==sOrderLines[iArraySize])ObjectDelete(name); // x[sOrderLines] ?
           }
        }
     }
  }

I know how to do it the long way, but, like I said, it is time to accommodate the arrays, which I have been avoiding, though I am not the only one, as the devil is avoiding the cross... LOL.

Thank you for your help,

Best regards,

Simon

S love nia

Aaaaa, aaarrraayssss!

 

OK, first of all, it looks I MUST iterate backwards

for(int i=total-1;i>=0;i--)
{
...
}
 

OK, an hour later, here is the code, working as a Swiss cheese, sorry, clock! (Interesting enough, the third famous Swiss thing also starts with c - chocolate! LOL!)

// --- DELETE HORIZONTAL LINES ----------------------------------------------------------------------------
void DeleteOrderLines()
  {
   string sOrderLines[]={"buy","sell","instant_buy","instant_sell"}; // initialize/define string array
   int total=ObjectsTotal();
   int iArraySize=ArraySize(sOrderLines);
   for(int i=total-1;i>=0;i--)
     {
      if(IsStopped())break;
      string name=ObjectName(i); // search through object names, and set its name as string
      if((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && ObjectDescription(name)!="") // if those 'types' are found
/*if(ObjectDescription(name)=="buy" || ObjectDescription(name)=="sell" || ObjectDescription(name)=="instant_buy"
         || ObjectDescription(name)=="instant_sell")ObjectDelete(name); // long code, works OK*/
           {
            Print(name+": INSIDE!"); // test
            for(int cnt=iArraySize-1;cnt>=0;cnt--) // both iteration types work OK
               //for(int cnt=0;cnt<iArraySize;cnt++) // iterate through array size
              {
               if(ObjectDescription(name)==sOrderLines[cnt])ObjectDelete(name);
              }
           }
        }
     }

Long Live the Code!

Simon

S love nia

 
Chistabo: OK, first of all, it looks I MUST iterate backwards
You must count down. You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
 
WHRoeder:
Chistabo: OK, first of all, it looks I MUST iterate backwards
You must count down. You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum

Yep, I did figure this out (by world-known coding technique - hit & miss, lol). Thank you for the tip.

Best regards,

Long live WHRoeder (& Co.),

Simon

S love nia

Reason: