# How to loop over chart objects?

73

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.

73

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

It seems my google searches have become better...

62946

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)
Moderator
33503

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)
62946

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)

36

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??

36

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;
}
}
62946

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);

Moderator
33503

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.
62946

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.

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

36

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!