Need help for a simple function

 

hello anyone. I need a function to recognize a manually drawn horizontal line and return its price value. The function should be also able to refresh the returned value if i manually move the line.

double GetLineValue()
  {
   double linev;
   long _chart=ChartID();
   for(int i=0;i<ObjectsTotal(_chart);i++)
     {
      string _name=ObjectName(_chart,i);
      if(StringFind(_name,"linename",0)>0) linev=ObjectGetDouble(_chart,_name,OBJPROP_PRICE1);
      else (linev=0);
     }
   return(linev);


If i create an horizontal line named "linename", this code returns always zero... I'm getting mad trying to understand why... can someone help me? Thank you in advance.

 

Because StringFind() returns 0 and you compare to >0.

if(StringFind(_name,"linename",0)!=-1) ...
 
Alain Verleyen:

Because StringFind() returns 0 and you compare to >0.

if(StringFind(_name,"linename",0)==3) linev=ObjectGetDouble(_chart,_name,OBJPROP_PRICE1);

Hi Alain, thanks for your answer! If i use this codeline and manually create a line object with name "AAAlinename" it should find it and return line price value... but it doesn't work!  Still returning 0.0 and there aren't other objects on chart. How can this be possible? Mql4 reference tells that StringFind returns substring position... Is there a better way to implement this? if i drag the line should the function update the value it returns? Thanks again.

 

Maybe you did find it, but reset it to zero on the next iteration?

double GetLineValue()
  {
   double linev;
   long _chart=ChartID();
   for(int i=0;i<ObjectsTotal(_chart);i++)
     {
      string _name=ObjectName(_chart,i);
      if(StringFind(_name,"linename",0)>0) linev=ObjectGetDouble(_chart,_name,OBJPROP_PRICE1);
      else (linev=0);
     }
   return(linev);
 
lippmaje:

Maybe you did find it, but reset it to zero on the next iteration?

Yes, thank you very much!