statement is false yet executed

 

Hi

i've a ordermodify cmd that always execute even when the if statement is false.

please would any experts here tell me why is this so ?

if (OrdersTotal() >0 ) {
OrderSelect (ticket, SELECT_BY_TICKET);
double currsl = OrderStopLoss ();

if ((currsl - pivotlow) != 0) { //if pivot low is NOT the same as current SL , execute 
Print(" pivot is " ,pivotlow ,"sl is " ,OrderStopLoss());
bool modify= OrderModify(OrderTicket(),OrderOpenPrice () ,pivotlow,OrderTakeProfit(), OrderExpiration(), Blue);
if (!modify) { 
Print ("last modify error is ",GetLastError());
      ObjectCreate("modiSL"+counterr,0,0,Time[0],Time[0]);
      ObjectSet("modiSL"+counterr,6,Chartreuse);
      }//end modify 
      
      } //end if currsl
   }//end if ordertotal>0
 
if ((currsl - pivotlow) != 0)
my guess is it never equals zero so it always executes
 
SDC:
my guess is it never equals zero so it always executes
I agree. Search this site for the trouble with comparing doubles. Getting two doubles to be equal is almost impossible unless both are zero. NormaliseDouble is one solution. Another is to compare using a threshold value such as Point as a value below which is considered zero.
 
Have a read of this thread: https://www.mql5.com/en/forum/136997
 

Look at stdlib.mq4 on your MetaEditor libraries, it said

//+------------------------------------------------------------------+
//| right comparison of 2 doubles                                    |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
  {
   if(NormalizeDouble(number1-number2,8)==0) return(true);
   else return(false);
  }
 
  1. if (OrdersTotal() >0 ) {
    OrderSelect (ticket, SELECT_BY_TICKET);
    If the orderselect fails, everything below is bogus. Always test return codes.
  2. if ((currsl - pivotlow) != 0) // Equivalent to: if(currsl != pivotlow)
    Doubles rarely compare equal (or not equal usually compares true.) Can price != price ? - MQL4 forum



 

Hi Guys

Thanks for your help .. guess i didn't search the forum deep enough. my bad.

 
if ((currsl - pivotlow) != 0)

BTW. with that code, it will executed on every tick point. Try make a band of it, for example :

if (  ((currsl - 10*Point) - pivotlow) || ((currsl + 10*Point) - pivotlow) != 0)
 
onewithzachy:

BTW. with that code, it will executed on every tick point. Try make a band of it, for example :


Hi onewithzachy

I don't get it how by making a band will optimize the code. care to elaborate more?

 
praetorian:

Hi onewithzachy

I don't get it how by making a band will optimize the code. care to elaborate more?

Your codes here ...

double currsl = OrderStopLoss ();
if ( (currsl - pivotlow) != 0 )//if pivot low is NOT the same as current SL , execute 

... is the problem. Because the pivotlow may change on every tick (read : on every single Point), especially if calculated using current price of bar 0.

If for example you band pivotlow like this :

double Upper_pivotlow = pivotlow + 10*Point // << this is depend on ...
double Lower_pivotLow = pivotlow - 10*Point // ... whether you using 4 or 5 digits broker

double currsl = OrderStopLoss ();
if ( (Upper_pivotlow - currs1) <= 0 || // if the real pivotlow is 10 point below ...
     (Lower_pivotLow - currs1) >= 0  ) // ... or 10 Point higher than SL
    {
    // do some act
    
    }

... it only be executed if pivotlow is 10 point above or below SL (that's 20 Point bands, btw).

just my IMHO ...

:)

hope I'm not wrong code them ;)