Close All

 
I am looking for a bot that closes all open positions if one position is closed, any suggestions? MT4
 
JON TRADER:
I am looking for a bot that closes all open positions if one position is closed, any suggestions? MT4

Recommendations are not allowed.

If you can't find it in Codebase or in the Market, you can order it in Freelance section.

 
JON TRADER:
I am looking for a bot that closes all open positions if one position is closed, any suggestions? MT4

That one position closed, is it done manually or using bot either? 

If it is done using bot, then search CloseAll script and insert it in the bot (modify it).

 
JON TRADER:
I am looking for a bot that closes all open positions if one position is closed, any suggestions? MT4

can be done simply. If you type the order number that will close upon reaching Closed / Stop or TakeProfite


//+------------------------------------------------------------------+
//|               closes all open positions if one position is close |
//|                                   2006-2020, Haskaya yazilim ltd |
//|                                      ttp://www.haskayayazilim.net|
//+------------------------------------------------------------------+
#property copyright   "2006-2020, Haskaya yazilim ltd."
#property link        "https://www.mql5.com/en/users/drbastem/seller"
#property description "closes all open positions if one position is closed"
#property strict
#property show_inputs
input int one_position_is_closed_OrderTicket=0;// closed any OrdersTickets

int start()
{
  if(one_position_is_closed_OrderTicket>0 && OrderNumberFind()==0) HepsiniKapat();
  
return(0);
}

int OrderNumberFind()
{
  int sonuc=0;
  int total = OrdersTotal();
   for(int ix=total-1;ix>=0;ix--)
    {
      if(OrderSelect(ix,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderTicket()==one_position_is_closed_OrderTicket) sonuc=1;     
    }
 
   
    return(sonuc);
}
int HepsiniKapat()
{
  bool resultord;
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    resultord=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, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

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

 
Mehmet Bastem:

can be done simply. If you type the order number that will close upon reaching Closed / Stop or TakeProfite


This is fantastic, thanks for sharing!