I get error in code for Alert

 
Hi Guys,

I try to create Alert when Bid price cross the trend line. First I would like to position the trend line and then get only one time alert after cross:

1) When the price cross from above to below;
2) When the price cross from below to above;

But I get error message /warning/ during compilation: "expression is always false" at this row of code:

// Above TLine
if (Bid>= val && posFlag==-1)

And if position the trend line below the price and wait for cross, when the cross occurred the alerts are continuous, but if position the trendline above - just from the second I get continuous alerts. I only need one alert per cross.

This is the code:

int start()
{
   int counted_bars=IndicatorCounted(), limit;
      
      ObjectCreate(TLineName, OBJ_TREND, 0, Time[25], Bid, Time[0], Ask);
      ObjectSet(TLineName, OBJPROP_STYLE, LineStyle);
      ObjectSet(TLineName, OBJPROP_COLOR, LineColor);

      double val=ObjectGetValueByShift(TLineName, 0);
      
      
   if(counted_bars>0) counted_bars=counted_bars-2;
   limit=Bars-counted_bars;
   
   if(limit>barsToProcess) limit=barsToProcess;

   for(int i=limit;i>=0;i--)
   {
      TLine_Buffer[i]=ObjectGetValueByShift(TLineName,i);
   }
     
         if(Bid<= val) posFlag=-1;
         if(Bid>= val) posFlag=1;
  
   
     //Alert("Bid=",Bid," val=",val," posFlag=",posFlag);
     
      // Below TLine
      if (Bid<= val && posFlag==1)
      {
          if(UseSound) PlaySound(AlertWav);
 
          posFlag = -1;
      }

      // Above TLine
      if (Bid>= val && posFlag==-1)
      {
          if(UseSound) PlaySound(AlertWav);
          posFlag = 1;
      }


   return(0);
  }

I want to set the position of the trend line manually. And have a code to get the position of the price (above or below) based on the trend line.

if(Bid<= val) posFlag = -1;
if(Bid>= val) posFlag = 1;

Could you please assist in order to fix the errors in code ?

Thank you in advance.

 

"expression is always false" is only a warning letting you know something could be wrong here. The compiler is making an assumption based on its own written rules. Doesn't mean it's bad code. What value is val? If it is 0, then at time of compile, yes the expression will always be false since Bid is always greater than 0.

There are several ways to make a sound play only once. Easiest would be to make the posFlag static. Another method would be to create a text Object and hide it. You could add description for the text Object, say for example "on" and "off". When sound is played, have code set Object to "on" and have code only allow sound play when description is "off". When sound is allowed to play again based on your rules, you have code reset Object to read "off".

These above are only couple ideas. You could also look into Global Variables or writing to a file. However reading from a text Object is easiest to code.

 
     
         if(Bid<= val) posFlag=-1;  //if(Bid<= val) you set posFlag=-1
         if(Bid>= val) posFlag=1;


      if (Bid<= val && posFlag==1)  //Then you check the same condition for Bid and val , posFlag CANNOT ==1


      if (Bid>= val && posFlag==-1) //ditto
 

JPS and GumRai thank you for your comments. Sorry for my late respond. I amended my code and I will test it.