How to loop over chart objects? - page 3

 
Fernando Carreiro:

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!

What a useless discussion.

My suggestion improve the Mladen code by a factor of 30 on the speed, and doesn't break anything. The topic is closed for my part.

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



   int x= ObjectsTotal(0,-1,OBJ_HLINE)-1;
   string obname=ObjectName(0,x,-1,OBJ_HLINE);
   double level=ObjectGetDouble(0,obname,OBJPROP_PRICE1);
   double difference=MathAbs(Bid-level);
   string closest=obname;
   x--;
   for(;x>=0;x--)
     {
     obname=ObjectName(0,x,-1,OBJ_HLINE);
     level=ObjectGetDouble(0,obname,OBJPROP_PRICE1);
     double diff=MathAbs(Bid-level);
     if(diff<difference)
        {
        closest=obname;
        difference=diff;
        }
     }
   Print("Closest Horizontal Line to Bid is ",closest);
.
Reason: