Skip following conditions ?

 

Hi, have a look at this code example:

   for (int i = 0; i < OrdersTotal(); i++)
      {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == FALSE) break;//If no order then get outta here!
      //-------------------
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
         {
         if (OrderType() == OP_BUY)
            {
            if (Condition_Ba) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);   continue;
            if (Condition_Bb) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);   continue;
            if (Condition_Bc) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);    continue;     
            if (Condition_Bd) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);  continue; 
            }
//-----------------------------------         
         if (OrderType() == OP_SELL)
            {
            if (Condition_Sa) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);   continue;
            if (Condition_Sb) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
            if (Condition_Sc) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
            if (Condition_Sd) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);continue; 
            }
         }                    
      }

The conditions Xx are fairly complex and time consuming. So lets say Condition_Ba=true and the selected order get closed..

no point in looking for other conditions that would close the already closed order... so the rest of the if-statements should be skipped and we should go right to the next index in the loop. HOW ?

I've tried both with break and continue, but they alters the controlflow and trade result

 

You need to use braces . . . .

if (Condition_Ba)  {   OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);   continue; }
 

Or

if (Condition_Sa) if( OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red) ) continue;
 
  1. You MUST count down when closing and deleting when you have multiple orders (multiple charts.) And you don't need the break or the multiple indents.

        for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
        &&  OrderMagicNumber()  == Magic.Number                 // my magic number
        &&  OrderSymbol()       == chart.symbol                 // and my pair.
        ){
    
  2. You must check your return codes for errors. The continue is unnecessary
    if (Condition_Ba || Condition_Bb || Condition_Bc || Condition_Bd){
        if(!OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue) )
           Alert("OrderClose failed: ", GetLastError());
    }

 

Hello programmer, i will leave here an example how to optimize your code and do what you want.Pay attention when programing to avoid loss of cpu time and work, for that you should try to wrote the code the more effeciently as possible, this means it should do what you desire but in the most simply way.Take a look in the example:

for(int i=0;i<OrdersTotal();i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)continue; // Order ticket don't exist in trading terminal
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!= Magic)continue; // Symbol don't match -or- magic number don't match

bool CloseOrder=false; // Define default action to Close Selected Order

if(OrderType() == OP_BUY)
{
if(Condition_Ba)CloseOrder=true; // Set to Close Selected Order
if(Condition_Bb)CloseOrder=true; // Set to Close Selected Order
if(Condition_Bc)CloseOrder=true; // Set to Close Selected Order
if(Condition_Bd)CloseOrder=true; // Set to Close Selected Order

if(CloseOrder)if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue))Alert("OrderClose Error: ",GetLastError()); // Close Order and check for errors
}
//-----------------------------------
if(OrderType() == OP_SELL)
{
if(Condition_Sa)CloseOrder=true; // Set to Close Selected Order
if(Condition_Sb)CloseOrder=true; // Set to Close Selected Order
if(Condition_Sc)CloseOrder=true; // Set to Close Selected Order
if(Condition_Sd)CloseOrder=true; // Set to Close Selected Order

if(CloseOrder)if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Red))Alert("OrderClose Error: ",GetLastError()); // Close Order and check for errors
}
}

 
nqueiros:

Hello programmer, i will leave here an example how to optimize your code and do what you want.Pay attention when programing to avoid loss of cpu time and work, for that you should try to wrote the code the more effeciently as possible, this means it should do what you desire but in the most simply way.Take a look in the example:

You are missing the point . . . the OP said " The conditions Xx are fairly complex and time consuming. " . . . so as soon as one of these conditions returns true the order should be closed without executing the other conditions . . .
 
RaptorUK:
You are missing the point . . . the OP said " The conditions Xx are fairly complex and time consuming. " . . . so as soon as one of these conditions returns true the order should be closed without executing the other conditions . . .

You are right, for avoid that it's easy, in the if(Condition_Sa) we input the bool condiction, that's enough to block the processing of another condiction if some has changed the bool, and should be like this if(!CloseOrder && Condition_Sa).

I missed that point, sorry.

for(int i=0;i<OrdersTotal();i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)continue; // Order ticket don't exist in trading terminal
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!= Magic)continue; // Symbol don't match -or- magic number don't match

bool CloseOrder=false; // Define default action to Close Selected Order

if(OrderType() == OP_BUY)
{
if(!CloseOrder && Condition_Ba)CloseOrder=true; // Set to Close Selected Order
if(!CloseOrder && Condition_Bb)CloseOrder=true; // Set to Close Selected Order
if(!CloseOrder && Condition_Bc)CloseOrder=true; // Set to Close Selected Order
if(!CloseOrder && Condition_Bd)CloseOrder=true; // Set to Close Selected Order

if(CloseOrder)if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue))Alert("OrderClose Error: ",GetLastError()); // Close Order and check for errors
}
//-----------------------------------
if(OrderType() == OP_SELL)
{
if(!CloseOrder && Condition_Sa)CloseOrder=true; // Set to Close Selected Order
if(!CloseOrder && Condition_Sb)CloseOrder=true; // Set to Close Selected Order
if(!CloseOrder && Condition_Sc)CloseOrder=true; // Set to Close Selected Order
if(!CloseOrder && Condition_Sd)CloseOrder=true; // Set to Close Selected Order

if(CloseOrder)if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Red))Alert("OrderClose Error: ",GetLastError()); // Close Order and check for errors
}
}

 
nqueiros:

You are right, for avoid that it's easy, in the if(Condition_Sa) we input the bool condiction, that's enough to block the processing of another condiction if some has changed the bool, and should be like this if(!CloseOrder && Condition_Sa).

I would question the validity of this approach. In C it is guaranteed that once a logical condition has been proven, further evaluation is terminated. For example in an AND test if the first test is false there is no point in doing the second test. HOWEVER ...

https://docs.mql4.com/basis/operations/bool

My reading of this, specifically ...

Logical expressions are calculated completely, i.e., the so-called "short estimate" method does not apply to them

suggests that MQL4 does not work like C in this respect, so complex tests are not efficient.

 
DayTrader:

Hi, have a look at this code example:

The conditions Xx are fairly complex and time consuming. So lets say Condition_Ba=true and the selected order get closed..

First you should follow WHR's sound advice above.

Next you could use a helper function to do the testing ...

bool CloseBuyTests(){

   bool Condition_Ba,Condition_Bb,Condition_Bc, Condition_Bd;
   
   if( Condition_Ba )
      return( true );
   
   if( Condition_Bb )
      return( true );
   
   if( Condition_Bc )
      return( true );
      
   if( Condition_Bd )
      return( true );
   
   return( false );
}

And finally you should not just do an OrderClose and expect it to work. Something like this would be better ...

   if( CloseBuyTests() ){
      for( int n=0; n<3; n++ ){
         if( OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue) )
            break;
         Print( "Order Close Error=" + GetLastError() );
         while( !RefreshRates() ){ /* wait for next tick */ }
      }
   }

Note that I put the OrderClose in an FOR loop to avoid an infinite loop.

 
dabbler:

I would question the validity of this approach. In C it is guaranteed that once a logical condition has been proven, further evaluation is terminated. For example in an AND test if the first test is false there is no point in doing the second test. HOWEVER ...

https://docs.mql4.com/basis/operations/bool

My reading of this, specifically ...

Logical expressions are calculated completely, i.e., the so-called "short estimate" method does not apply to them

suggests that MQL4 does not work like C in this respect, so complex tests are not efficient.


This is a good question, and i think you have reason, mql4 is not c++, so i haven't test that yet, but i think that in mql4 both condicions are processed, an if so, no effeciency is done,for security should be done with if(condiction==false)if(Condition_Ba)....

The function you create is also good, but such function to so little code is in my opinion to avoid the use, for me only justify the use of funtions when they will be called several times during the execution of the code,but anyone has is own way to do, and i respect that,

any way good job dabbler!

Reason: