Download MetaTrader 5

Object creation on different charts

To add comments, please log in or register
xoxman4
13
xoxman4  

Hi guys, 

I create objects on different charts (same symbol). Afterwards when iterating through all open charts in order to collect ALL objects mt4 does NOT return all !! Any ideas ?

    long chartID = ChartFirst();
    while ( chartID > -1 )
    {
      if ( ChartSymbol(chartID) == Symbol() )
      {
        int obj_total = ObjectsTotal ( chartID ); 
        for ( int x = 0; x < obj_total; x++ ) 
        { 
          string name = ObjectName ( x ); 
          if ( StringFind(name,key,0) > -1 )
          {
            ObjectDelete ( chartID, name );
            Print ( "-----> object ", name, " deleted" );
          }
        }      
      }
      ChartRedraw ( chartID );
      chartID = ChartNext ( chartID );
    }  
Keith Watford
Moderator
10612
Keith Watford  

I must admit that as yet I have not had reason to work with objects on a different chart.

You might try

          string name = ObjectName (chartID, x ); 

and see if it works for you

Ex Ovo Omnia
3357
Ex Ovo Omnia  

In addition to Gumrai. This coding problem was thousand times here.

        for ( int x = 0; x < obj_total; x++ ) 
        { 
          string name = ObjectName ( x ); 
          if ( StringFind(name,key,0) > -1 )
          {
            ObjectDelete ( chartID, name );
            Print ( "-----> object ", name, " deleted" );
          }
        }      
      }
      ChartRedraw ( chartID );
whroeder1
16108
whroeder1  
Ovo: In addition to Gumrai. This coding problem was thousand times here.
 for ( int x = 0; x < obj_total; x++ ) 

As has been repeated thousands of time here, you must count down in position loops.
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
whroeder1
16108
whroeder1  
xoxman4: I create objects on different charts (same symbol). Afterwards when iterating through all open charts in order to collect ALL objects mt4 does NOT return all !!
string name = ObjectName ( x ); 

  1. Not going to work. ObjectName only works on the current chart. There is no ObjectName(chartId, position)
  2. I'd ask Metaquotes what use is ObjectsTotal(chartID) when you can't get their names.
peter.MT4Web
157
peter.MT4Web  

Use the MQL5 definition:

string  ObjectName( 
   long  chart_id,           // Identifikator des Charts  
   int   pos,                // Nummer in der Liste der Objekte  
   int   sub_window=-1,      // Nummer des Fensters 
   int   type=-1             // Typ des Objekts 
   );

It will also work on MQL4.


xoxman4
13
xoxman4  
thanks a lot, works now !! :)
whroeder1
16108
whroeder1  
eddie:

Use the MQL5 definition:

It will also work on MQL4.

Then the missing documentation should be reported to the service desk.
Zirkon
121
Zirkon  

You should use the ObjectsDeleteAll() with a prefix, easier and faster.

   long chartID=ChartFirst();
   while(chartID>-1)
     {
      if(ChartSymbol(chartID)==Symbol())
        {
         ObjectsDeleteAll(chartID,key);
        }
      ChartRedraw(chartID);
      chartID=ChartNext(chartID);
     }
To add comments, please log in or register