How to close all orders before weekend. - page 2

 

Beautiful!

I wonder if it can even handle "CloseAll"?

 
void CloseAllType(int _iType = -1)
{
    int ordticket;
    
    for( int i = OrdersTotal()-1; i>=0; i-- ){
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
         continue;
      }
      if( OrderMagicNumber() != EA_magic ){
          continue;
      }
      if( OrderSymbol() != Symbol() ){
          continue;
      }
      if( OrderType() != _iType && _iType != -1){
          continue;
      }
      RefreshRates();
      ordticket = OrderTicket();
      double ldPrice = 0;
      if (OrderType() == OP_BUY){
          ldPrice = Bid;
      }      
      if (OrderType() == OP_SELL){
           ldPrice = Ask;          
      }
      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),ldPrice ,slippage,White);
      } else {
         Print("CloseAllType() returned an error: ",GetLastError());
      }
    }
    return(0);
}
to close all just call
CloseAllType();
 

This is turning out to be a high-level education in programming. Unbelievable that such generosity still exists.

So if no parameter like OP_BUY or OP_SELL is passed, the function closes all orders?

If OP_BUY is passed it closes all longs and if OP_SELL is passed it closes all shorts?

There is no risk that it closes the wrong order if the spread is less than the slippage?

 

There is no risk that it closes the wrong order if the spread is less than the slippage?

Yes, if no param is passed, it should close all buy and sell orders. Otherwise the given ordertype.

You don't have a check for the spread though.

Please note: i didn't test the adjustments I made in your function. But at first glance it seems to be right

 

btw you allready checked if orderselect was valid so this check

      if( OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true ){
         OrderClose(ordticket,OrderLots(),ldPrice ,slippage,White);
      } else {
         Print("CloseAllType() returned an error: ",GetLastError());
      }
can be skipped.
 

By having separate functions for longs and shorts I ensure that the correct orders are selected.

 
Russell:

btw you allready checked if orderselect was valid so this check

can be skipped.


Early in the discussion, Gordon wrote "And u should also check OrderSelect()'s return value."
 
The first OrderSelect is SELECT_BY_POS, the final, which is checked, is SELECT_BY_TICKET
 

8) Yes and gordon was right about that.

OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false

here you select your order on it's position in the array of open trades, and you check if it was successful. If not, you go to the next position. (the continue)

with the order already selected, you select it once more

OrderSelect(ordticket, SELECT_BY_TICKET, MODE_TRADES) == true 
but there is no need to do so, because it is already selected, and you checked if it was. So the chance that the order selection isn't valid anymore, is almost zero. There are only a few instructions between your first select and the execution of OrderClose.
 

Very educational. Thank you both, Gordon and Russell, for your fantastic contribution.

Warmest regards, Helmut

Reason: