Close all open position when certain margin level reached

 
Can someone guide me which's EA will force to close all of open positions (including profit and/or loss) if the margin level reach certain percentage?
​Thanks in advanced.
 
Panjianom Adi Pratomo:
Can someone guide me which's EA will force to close all of open positions (including profit and/or loss) if the margin level reach certain percentage?
​Thanks in advanced.
//+------------------------------------------------------------------+
//|                                          CloseMarginPercetEA.mq4 |
//|                                 Copyright 2019, Haskaya Software |
//|                                   https://www.haskayayazilim.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Haskaya Software"
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict
input double MarginLevelEntry=75;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  // OnTick();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
   if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)<=MarginLevelEntry) CloseAllOrders();
   
  }
//+------------------------------------------------------------------+
int CloseAllOrders()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    bool sds=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:

Thank you so much bro!

 
You can add a reference to this library in case you need this for MT5: https://www.mql5.com/en/code/16006
MT4Orders
MT4Orders
  • www.mql5.com
Parallel use of the MetaTrader 4 and MetaTrader 5 order systems.