A code to close all open orders?

 
I remember someone posted a simple code(MQL II) to close all open/pending orders prior to closing of the MT server on Friday. Can you post that code once again? Thanks.
 
FrankC,

Here is a script that will close all the open and pending orders

//+------------------------------------------------------------------+
//|                                            close-all-orders.mq4  |
//|                                  Copyright © 2005, Matias Romeo. |
//|                                       Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link      "mailto:matiasDOTromeoATgmail.com"

int start()
{
  int total = OrdersTotal();
  for(int i=0;i<total;i++)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}




Hope this help

Matias Romeo
Custom Metatrader Systems
matiasdotromeoatgmail.com

 
Thanks Matias for your help. That is what I needed. Been away from this forum for awhile.
 
there may be a problem: after order close this closed order removed from list of opened orders. therefore You need for "back loop"
for(int i=total-1; i>=0;i--)
 
Slawa,
Thanks for the help, here is the corrected version.
Anyway, i tried this script and it works flawlessly, could it be possible?

//+------------------------------------------------------------------+
//|                                            close-all-orders.mq4  |
//|                                  Copyright © 2005, Matias Romeo. |
//|                                       Custom Metatrader Systems. |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, Matias Romeo."
#property link      "mailto:matiasDOTromeoATgmail.com"

int start()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}



Matias Romeo
Custom Metatrader Systems
matiasdotromeoatgmail.com

 
Hi Matias,

I also had this idea to use my existing Order Management routine where I check for any open buy/sell orders, and add an extra "if clause" to close any order if dayofthe week=Friday, and Hour=MT4's server closing time. Something like this:

if (TimeDayOfWeek(CurTime())==4 && TimeHour(CurTime())==0 )
{
<close open orders...>
}

Makes sense?
 
FrankC,

If you want to close all your open orders on friday at that hour ... it make sense ;)
 

if (TimeDayOfWeek(CurTime())==4 && TimeHour(CurTime())==0 )


just want to be sure that the statement above is correct to check for Friday and at the closing hour of the Mt's server time.

I did the Alert on TimeDayOfWeek(CurTime()) now, and it shows the int value 1. So Monday is 1, but in the documentation Tue should be 1! I am a bit confused with the correct value for Friday. Also is CurTime())==0 is a valid check for the closing Hour?

EDIT(30/5) : No worry. Found the answer.
 

Can someone help me define "inverse_close - true/false" 

"tradelock - %"

and " tradeinverse - %)

I'd like a link to some kind of list of all the codes that can be used. Of Course, I'm a noob.

Thanks

Reason: