How to loop over chart objects? - page 2

 
Fstrifoerr8:

Thank you. 

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

 

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

Hahaha!

Yep - it is missing in the MT4 help file :)

Even though it is undocumented (omitted) in the help file, you can use it (the auto complete is suggesting it to you as one of the two ways how ObjectName() can be called)

 
Mladen Rakic:

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

I really have no idea why you are arguing. If you prefer code less efficient, it's ok for me.
 
Alain Verleyen:
I really have no idea why you are arguing. If you prefer code less efficient, it's ok for me.

Strange. Like talking to the deaf

Whatever ... have a great weekend.

 
Mladen Rakic:

Strange. Like talking to the deaf

Whatever ... have a great weekend.

It's not a general topic about objects, it's a topic to answer a precise question from the OP. He is searching for horizontal lines, so he knows the type. Is it so hard to recognize you are wrong ?

I suggest you to watch your language.

 
Alain Verleyen:

It's not a general topic about objects, it's a topic to answer a precise question from the OP. He is searching for horizontal lines, so he knows the type. Is it so hard to recognize you are wrong ?

I suggest you to watch your language.

I don't want to butt heads here, but I think that what Mladen is trying to say, is that if you use a type filter with ObjectsTotal() you will end up not being able to iterate through all the possible positions that may have those types of objects assigned.

For example, lets say you have 4 objects of which only 2 are horizontal lines and it so happens that they are at index 2 and 3 and that the other objects are at 0 and 1.

Since the filtered ObjectsTotal will return only 2, you will end up iterating over only the first 2 and ObjectName() will return the names of the other objects and not the horizontal lines.

PS! Also, please note that your response below was to Mladen's post and not to the OP's query.

 
Fernando Carreiro:

I don't want to butt heads here, but I think that what Mladen is trying to say, is that if you use a type filter with ObjectsTotal() you will end up not being able to iterate through all the possible positions that may have those types of objects assigned.

For example, lets say you have 4 objects of which only 2 are horizontal lines and it so happens that they are at index 2 and 3 and that the other objects are at 0 and 1.

Since the filtered ObjectsTotal will return only 2, you will end up iterating over only the first 2 and ObjectName() will return the names of the other objects and not the horizontal lines.

PS! Also, please note that your response below was to Mladen's post and not to the OP's query.


Thanks for trying to help.

I understand perfectly what Mladen has written. The fact is when someone ask a question, I suppose people are answering to that question. As Mladen post an answer, I was just suggesting it could be improved by using the code I proposed. All other considerations are useless arguing ("if you don't know the type", etc...while it's perfectly clear the type is well known.

 
Fernando Carreiro:

I don't want to butt heads here, but I think that what Mladen is trying to say, is that if you use a type filter with ObjectsTotal() you will end up not being able to iterate through all the possible positions that may have those types of objects assigned.

For example, lets say you have 4 objects of which only 2 are horizontal lines and it so happens that they are at index 2 and 3 and that the other objects are at 0 and 1.

Since the filtered ObjectsTotal will return only 2, you will end up iterating over only the first 2 and ObjectName() will return the names of the other objects and not the horizontal lines.

PS! Also, please note that your response below was to Mladen's post and not to the OP's query.


Hi,

Please put this code in a script


   int cnt= ObjectsTotal(0,-1,OBJ_HLINE);
   Print(cnt);
   for(int x=cnt-1;x>=0;x--)
     Print(ObjectName(0,x,-1,OBJ_HLINE));
  
   cnt= ObjectsTotal(0,-1,OBJ_VLINE);
   Print(cnt);
   for(int x=cnt-1;x>=0;x--)
     Print(ObjectName(0,x,-1,OBJ_VLINE));
  

Put a few horizontal and vertical lines on a chart and then run the script. You will see that there are separate lists for the object type

 

I used a chart I have open with 1 indicator, it uses objects. I added 3 horizontal lines. There is now 99 objects on the chart.

Then I run some "benchmark" code, with Malden code and mine and I run it 1,000,000 times as it could happen in a backtest for example.

Results :

2016.11.04 22:15:44.804    BenchmarkObjects EURUSD,M1: angevoyageur function, run 1000000 times : total objects=99(3 H_LINE) time to get HLINE objects=2140
2016.11.04 22:15:42.656    BenchmarkObjects EURUSD,M1: Mladen function, run 1000000 times : total objects=99(3 H_LINE) time to get HLINE objects=64094

Time is in milliseconds.

Files:
 
Alain Verleyen: Thanks for trying to help.

I understand perfectly what Mladen has written. The fact is when someone ask a question, I suppose people are answering to that question. As Mladen post an answer, I was just suggesting it could be improved by using the code I proposed. All other considerations are useless arguing ("if you don't know the type", etc...while it's perfectly clear the type is well known.

The OP requested a way to LOOP over all the chart objects or just the horizontal lines, which Mladen's code DOES answer and he even stated that the code was incomplete and needed the object type to be checked.

Your suggestion, however does not improve Mladen's code but actually breaks it and does not answer the OP's query, as he did not request the COUNT of horizontal lines, but how to loop over them to find the closest one to the current price.

Sorry, If I disagree with your assessment of this point!

 
Keith Watford:

Hi,

Please put this code in a script


   int cnt= ObjectsTotal(0,-1,OBJ_HLINE);
   Print(cnt);
   for(int x=cnt-1;x>=0;x--)
     Print(ObjectName(0,x,-1,OBJ_HLINE));
  
   cnt= ObjectsTotal(0,-1,OBJ_VLINE);
   Print(cnt);
   for(int x=cnt-1;x>=0;x--)
     Print(ObjectName(0,x,-1,OBJ_VLINE));
  

Put a few horizontal and vertical lines on a chart and then run the script. You will see that there are separate lists for the object type

Yes, I know, but that is not really my point! However, I think that no amount of explaining is going to "fix" this misunderstanding that originated between Mladen and Alain, so I will just leave it at that and move on.