If statement doesn't skip to next statement even when condition is not full filled.

 

Hi, anyone out there who can help me with this?

I'm struggling with two nested IF statements for OP_BUY and OP_SELL situation as well.

The second IF statement always calls the user-defined function modify() even if the condition is not full filled.

e.g. profit_in_points (PrIP) is still smaller than "threshold x point" (TxP) , see screenshot.


Failing IF statements


Hope I've added the code correctly.

Appreciate your help,


Added part of the code here, just to be sure.


 

  if (OrderType() == OP_BUY) 

     {

     double newsl = OrderOpenPrice() + ( plus * point ); 

     double profit_in_pts = OrderClosePrice() - OrderOpenPrice();

     profit_in_pts = NormalizeDouble ( profit_in_pts, digits );                                           //Added this line

     if ( OrderStopLoss() == 0 || compare_doubles ( newsl, OrderStopLoss(), digits ) > 0 )  // SL already set at BE ?

       {

       Alert("NewSL > SL");

       if (  compare_doubles ( profit_in_pts, ( threshold * point ), digits ) >= 0 )                  //Threshold reached ?

         {

         Alert("Profit_In_Points > Threshold_time_Point");

         result = modify (ticket, newsl);

         }

       }

     }

    else if (OrderType() == OP_SELL) 

     {

     double newsl = ( OrderOpenPrice() - ( plus * point ) ) ;

     double profit_in_pts = OrderOpenPrice() - OrderClosePrice();

     profit_in_pts = NormalizeDouble ( profit_in_pts, digits );                                            // Added this line     

    if ( OrderStopLoss() == 0 || compare_doubles ( newsl, OrderStopLoss(), digits) <= 0 )   // SL already set at BE ?

      {

       Alert("NewSl < SL");

      if (  compare_doubles ( profit_in_pts, ( threshold * point ), digits ) >= 0 )                     //Threshold reached ?

        {  

         Alert("Profit_In_Points > Threshold_time_Point");

         Alert("PrIP = ",profit_in_pts );

         Alert("TxP = ", (threshold * point ) );

         result = modify (ticket, newsl);

          }

        }

      }
 
RLE: The second IF statement always calls the user-defined function modify() even if the condition is not full filled.
 if (  compare_doubles ( profit_in_pts, ( threshold * point ), digits ) >= 0 )
  1. Since  PrIP (0.00037) ≱ TxP (0.003) but the if(bool) is true, your compare_doubles must be lying to you.

  2. There is no need for such functions if you understand the links in:
              The == operand. - MQL4 programming forum 2013

    A ≥ B could be true or false at equality because of round off errors.
    If the equality is not important (false when equal or true when not) just use A > B (don't worry about it.)
    If the equality is important (must be true at A == B) use "definitely greater or equal": A - B > -Ε. (Note the minus.)
    If the equality is important (must be false at A == B) use "definitely greater": A - (B) > Ε.
    Where Epsilon (Ε) depends on what is being compared. For prices, use _Point/2.

 

Thanks William for your quick reply, you're my hero.

1. PrIP is about 8 x smaller than TxP in my screenshot.

    Probably you right about the If (bool).


2. I need to study this, not sure if equality is important, will check it out a.s.a.p.


Thanks again for the great tip and "definitely greater or equal" functionality as well.

 
I think the problem with compare_doubles() gives incorrect results. Can you give the code. 
 
Please try my library for correct comparisons and see if it resolves the problem.. 
https://www.mql5.com/en/code/25723