Download MetaTrader 5

How to say "Do nothing" with if-else operator.

To add comments, please log in or register
Toufik
49
Toufik  

I just want for the EA to not execute anything when a certain condition is met (expression1)

for (int i = 0; i < OrdersTotal(); i++)
         {
         if (!OrderSelect(i1, SELECT_BY_POS, MODE_TRADES))
            {
            Print("Error Selecting Order: ", GetLastError());
            continue;
            }
         if (expression1) 
             operatot1 // Do nothing or end.
         else 
         operator2
         }
Anthony Garot
131
Anthony Garot  
Toufik:

I just want for the EA to not execute anything when a certain condition is met (expression1)

To execute nothing when expression1 is true:

if (expression1)
{
    // NOOP
}
else
{
    operator2 . . . 
}

Of course, you could just rewrite it:

if (! expression1)
{
    operator2 . . . 
}
Toufik
49
Toufik  
Anthony Garot:

To execute nothing when expression1 is true:

Of course, you could just rewrite it:

Thank you, In fact I thought when expression1 is true operator1 will be executed in our case "Do nothing" and control goes to the operator that follows operator2 but the problem is operator2 is executed, I don't know why. so I would prefer something more efficient than "Do nothing" end the for loop or just put the control out.
Fernando Carreiro
3405
Fernando Carreiro  
Toufik: Thank you, In fact I thought when expression1 is true operator1 will be executed in our case "Do nothing" and control goes to the operator that follows operator2 but the problem is operator2 is executed, I don't know why. so I would prefer something more efficient than "Do nothing" end the for loop or just put the control out.
  • If you just want to stop a loop and "get out", then use the "break" operator.
  • If you just want to skip one iteration of the loop and advance to the next one, then use the "continue" operator.

However, if you properly structure your code, you will probably never have to use either of those two operators. So, first consider the logic of your code in detail, and make sure you are doing it correctly.

For example, changing your code into this:

for( int i = OrdersTotal() - 1; i >= 0; i-- ) // It is advisable to count down
{
  if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
  {
    if( !expression1 )
    {
       // Do Something
    }
  }
  else
     Print( "Error Selecting Order: ", GetLastError() );
}
Break Operator - Operators - Language Basics - MQL4 Reference
Break Operator - Operators - Language Basics - MQL4 Reference
  • docs.mql4.com
Break Operator - Operators - Language Basics - MQL4 Reference
Toufik
49
Toufik  
Fernando Carreiro:
  • If you just want to stop a loop and "get out", then use the "break" operator.
  • If you just want to skip one iteration of the loop and advance to the next one, then use the "continue" operator.

However, if you properly structure your code, you will probably never have to use either of those two operators. So, first consider the logic of your code in detail, and make sure you are doing it correctly.

For example, changing your code into this:

Thank you Fernando, I should better explain the problem, so this for loop is made to put a pending order with a market order previously placed. The condition is if a market order is opened then a pending order should be opened that's it.. but the problem here is that my pending order is opened infinitely more than one time whereas I need one single pending order. 

So how to avoid for PO to be opened more than one time.

Logically I thought this could work but unfortunately it repeats infinitely the PO.

for( int i = OrdersTotal() - 1; i >= 0; i-- ) 
{
  if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) )
  {
    if( !(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() > 1) )
    {
       // PO function.
    }
  }
  else
     Print( "Error Selecting Order: ", GetLastError() );
}
whroeder1
16336
whroeder1  
Toufik: The condition is if a market order is opened then a pending order should be opened that's it..
That's not it. On the next tick you will have both but will open another. Loop through the orders and count how many of each you have, then test the condition.
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
Toufik
49
Toufik  
whroeder1
16336
whroeder1  
Toufik: Can someone see it as simple as " if there is one PO then Do not open a second PO " ??

Yes, that is simpler. Whereas your test is only good after the initial order, my (#5) allows the initial order to be pending or market,

To add comments, please log in or register