How to loop over chart objects?

 

I've created an indicator that will plot horizontal lines on each tick.

On each tick, I'd like to find the line that is closest to the current price in order do other things (plot vertical line between those 2 points, know how to do that.)

I figured the best way would be to just loop over all chart objects (or only horizontal lines, if possible) and compare the prices.

 

Is there an array that contains all chart objects predefined?

 

If not, otherwise ignore, how would I create a dynamic array, that I could add and remove objects to after initialisation?

 Thanks a lot for your help! Learning mq4l but some things are not so easy to figured out.

 

I think I found what I'm looking for with ObjectsTotal and OBJPROP_Price1.

It seems my google searches have become better... 

 
ZetaTrader:

I've created an indicator that will plot horizontal lines on each tick.

On each tick, I'd like to find the line that is closest to the current price in order do other things (plot vertical line between those 2 points, know how to do that.)

I figured the best way would be to just loop over all chart objects (or only horizontal lines, if possible) and compare the prices.

 

Is there an array that contains all chart objects predefined?

 

If not, otherwise ignore, how would I create a dynamic array, that I could add and remove objects to after initialisation?

 Thanks a lot for your help! Learning mq4l but some things are not so easy to figured out.

There is no predefined array

You have to loop through all the objects, get its name and then you can retrieve the price property of the object with the given name. Something like this :

   double diff = EMPTY_VALUE; string nameOfClosest="";
   for (int i =ObjectsTotal()-1; i>=0; i--)
   {
      string name  = ObjectName(i);
      double price = ObjectGet(name,OBJPROP_PRICE1);
         if (diff>MathAbs(priceToCompareTo-price))
         {
            diff = MathAbs(priceToCompareTo-price); nameOfClosest = name;

         }

    }

But also, you should probably check if the object type is the type that you expect it to be (OBJ_TREND or OBJ_HLINE  I guess is what you are looking for)
 
Mladen Rakic:

There is no predefined array

You have to loop through all the objects, get its name and then you can retrieve the price property of the object with the given name. Something like this :

   double diff = EMPTY_VALUE; string nameOfClosest="";
   for (int i =ObjectsTotal()-1; i>=0; i--)
   {
      string name  = ObjectName(i);
      double price = ObjectGet(name,OBJPROP_PRICE1);
         if (diff>MathAbs(priceToCompareTo-price))
         {
            diff = MathAbs(priceToCompareTo-price); nameOfClosest = name;

         }

    }

But also, you should probably check if the object type is the type that you expect it to be (OBJ_TREND or OBJ_HLINE  I guess is what you are looking for)

You should just use :

... ObjectsTotal(0,-1,OBJ_HLINE)
 
Alain Verleyen:

You should just use :

... ObjectsTotal(0,-1,OBJ_HLINE)

And in that case the rest of the code has to be changed too (not just that part)

Whatever : that code snippet does the job and no need to change anything (regardless of the object type - and we are just assuming that it is horizontal line and not a trend line, or both he is looking for)

 
Alain Verleyen:

You should just use :

... ObjectsTotal(0,-1,OBJ_HLINE)

And????

All objects = 60

H_line objects =20, some of them are with number 43,44,45 at list of objects. 

How you will chek H objects 43,44,45?? 

 
string name;
double price;
double nerest_up_price=EMPTY_VALUE,nerest_down_price=0;
string nerest_up_name,nerest_down_name;

for (int i =ObjectsTotal()-1; i>=0; i--)
   {
      name  = ObjectName(0,i);
      if(ObjectGetInteger(0,name,OBJPROP_TYPE)!=OBJ_HLINE) continue;
      price = ObjectGetDouble(0,name,OBJPROP_PRICE1);
  
    //up
     if(price>Bid && price<nerest_up_price)
     {
     nerest_up_price=price;
     nerest_up_name=name;
     }
      //down
     if(price<Bid && price>nerest_down_price)
     {
     nerest_down_price=price;
     nerest_down_name=name;
     }
   }
 
Fstrifoerr8:

And????

All objects = 60

H_line objects =20, some of them are with number 43,44,45 at list of objects. 

How you will chek H objects 43,44,45?? 

In that case you have to use ObjectName(0,i,-1,OBJ_HLINE); instead of ObjectName(i);

 
Mladen Rakic:

And in that case the rest of the code has to be changed too (not just that part)

Whatever : that code snippet does the job and no need to change anything (regardless of the object type - and we are just assuming that it is horizontal line and not a trend line, or both he is looking for)

Yes but you don't need to loop all chart objects if you only want the horizontal lines (it was OP request). It could make a big difference regarding performance if you have a lot of objects on your chart, and if you want to run this each tick as said by the OP.
 
Alain Verleyen:
Yes but you don't need to loop all chart objects if you only want the horizontal lines (it was OP request). It could make a big difference regarding performance if you have a lot of objects on your chart, and if you want to run this each tick as said by the OP.

Please read my post again ...

If we know the exact object type no problem but in that case it is not enough to change just that - the access to the name must be changed too, so the rest of the code must be changed too

But without knowing the exact object type, there has to be a "general solution" which could work on any criteria. In any case, if there are no 1000s of objects on chart, no significant difference will be noticed. If there are (1000s of objects) then the least of problems will be that loop

 
Mladen Rakic:

In that case you have to use ObjectName(0,i,-1,OBJ_HLINE); instead of ObjectName(i);

Thank you. 

I open MT4 click on ObjectName() F1 button and see.

 

string  ObjectName(
   int    object_index   // number in list
   );

Hahaha!