Is it viable to use "return"?

 

Here is a snipet from my trailstop function, here i select the orders to be trailed

 if(!OrderSelect(i, SELECT_BY_POS,MODE_TRADES)){
                     Comment(">> Order select failed, error # ", GetLastError() );

                     Print(">> Order select failed, error # ", GetLastError() );

                     return;
                     }


And at the end is the return;

Is is viable to use it in this structure, i mean it will return the program reader to the first { , in an infinite loop until it selects that order, or until another tick comes and restarts the function.

The problem is  that i cant decide that to use this or not, because if the infinite loop slows down the EA then its bad, but also missing out the OrderSelect for some time can be bad too.

Please help!

 
Proximus: if the infinite loop slows down the EA then its bad, but also missing out the OrderSelect for some time can be bad too.
  1. You don't show the rest of the loop. Aren't you counting down? Then there can't be.
  2. OrderSelect can fail if while you were processing one order, another EA on another chart closed an order, or another order hits a stop and closes. Loops and Closing or Deleting Orders - MQL4 forum
 

return doesn't mean repeat or go back

Return Operator


The return operator terminates the current function execution and returns control to the calling program. The expression calculation result is returned to the calling function. The expression can contain an assignment operator.

 

Proximus, just like you I was concerned on how to handle the OrderSelect() function. I came up with OrderSelectError().

When updating a tally such as total of profit or pips you are not too concerned as you know that the routine gets repeated,

likewise in a maintenance routine when moving the StopLoss it gets calculated repeatedly, but when closing orders then

your function needs to take corrective action if the OrderSelect() fails such as retrying to close/cancel the orders.

//+------------------------------------------------------------------+
//| Function..: OrderSelectError 27     Copyright 2014 sxTed@gmx.com |                     |
//| Parameters: bRecord - 1 = record error number,                   |
//|                       0 = display error message and reset error  |
//|                           number to zero.                        |
//|             bUrgent - 1 = mission critical error!, like closing  |
//|                           orders,                                |
//|                       0 = relax when displaying pip totals.      |
//| Purpose...: Record last OrderSelect() error message of a process.|
//|             Allows the processing of other orders before alerting|
//|             of the OrdersSelect() error so as to enable remedial |
//|             action to be taken.                                  |
//| Returns...: iError - OrderSelect() error number as retrieved from|
//|                      GetLastError().
//| Notes.....: Order does not get selected when using the incorrect |
//|             selecting flag, like SELECT_BY_POS (order pool index)|
//|             in lieu of SELECT_BY_TICKET (order ticket index). The|
//|             function will display a message with no error.       |
//| Sample....: bool CloseOrder(int iMagic, string sSymbol,          |
//|                             int iSlippage) {                     |
//|               int iErrors, i, cmd;                               |
//|               for (i=OrdersTotal()-1; i >= 0; i--) {             |
//|                 if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {|
//|                   if (iMagic == OrderMagicNumber()) {            |
//|                     if (sSymbol == OrderSymbol()) {              |
//|                       cmd=OrderType();                           |
//|                       if (cmd==OP_BUY || cmd==OP_SELL) {         |
//|                         if(!OrderClose(OrderTicket(),OrderLots(),|
//|                           OrderClosePrice(), iSlippage)) {       |
//|                           Alert("close error "+GetLastError());  |
//|                           iErrors++;                             |
//|                         }                                        |
//|                       }                                          |
//|                     }                                            |
//|                   }                                              |
//|                 } else OrderSelectError();                       |
//|               }                                                  |
//|               return(iErrors+OrderSelectError(0) == 0);          |
//|             }                                                    |
//|                                                                  |
//|             if (!CloseOrder(123,"EURUSD",30)) Alert("notClosed");|
//+------------------------------------------------------------------+

// public memvars for function OrderSelectError()
int iError27, iReturn27;

int OrderSelectError(bool bRecord=true, bool bUrgent=true) {
   if (bRecord) {
      iError27=GetLastError();
      iReturn27=iError27;
   } else {
      if (iError27 > 0) Msg(StringConcatenate(gsPrg,"OrderSelect() error ",iError27," encountered - retry ",ifS(bUrgent,"now!","later")));
      iReturn27=iError27;
      iError27=0;
   }
   return(iReturn27);
}

 

A bit of advertising as MetaQuotes are not doing it. My little expert has just been published at https://www.mql5.com/en/market/product/4751

Take care, Teddy. (and Moderator AngeVoyageur or other, leave this in!!!)

 
qjol:

return doesn't mean repeat or go back

Return Operator


The return operator terminates the current function execution and returns control to the calling program. The expression calculation result is returned to the calling function. The expression can contain an assignment operator.



So shall i understand that "return" is equivalent to "break" ?

if so then its even worse because i cannot exit from the for loop as we must select all orders, if we were to use the "return" then we would exit prematurely from the loop and the rest of the orders would be in trouble.

Suppose we have 5 trades open, and we trail the stops of those 5 orders, but for whatever reason order nr 3 cant be selected, so if we were to exit from the loop at 3, the remaining 2 orders will not be trailed until a new tick comes.

Now not trailing for 1 tick may not be a huge problem but still its very silly and very ineficient for an EA, when we know that it can be done better.

So if that is the case, both "return" and "break" is useless when programming trail stops.

 
Proximus:

So shall i understand that "return" is equivalent to "break" ?

if so then its even worse because i cannot exit from the for loop as we must select all orders, if we were to use the "return" then we would exit prematurely from the loop and the rest of the orders would be in trouble.

Suppose we have 5 trades open, and we trail the stops of those 5 orders, but for whatever reason order nr 3 cant be selected, so if we were to exit from the loop at 3, the remaining 2 orders will not be trailed until a new tick comes.

Now not trailing for 1 tick may not be a huge problem but still its very silly and very ineficient for an EA, when we know that it can be done better.

So if that is the case, both "return" and "break" is useless when programming trail stops.


No, return is not equivalent to break. If you mean to code, you should take a course, as understanding of return is essential. Break steps out of the closest loop or switch block prematurely, while return steps out of a function. It is ridiculous calling statements to be useless just because you lack knowledge.

 
It is ridiculous calling statements to be useless just because you lack knowledge.
Yes, when people have enough knowledge, they would feel very embarrassing when thinking how foolish they are when they said it.