//+------------------------------------------------------------------+
//|                             CloseTrades_MaxEquity_or_MaxLoss.mq4 |
//|            Copyright © 2011, Tradinator, Don Perry, Gene Katsuro |
//|                                          Modded by WNW and magft |
//|                                http://www.forexfactory.com/magft |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Tradinator, Don Perry, Gene Katsuro,WNW and magft"
#property link      " http://www.forexfactory.com/magft"


                                               //+---------------------------------------------------------------------+
extern double Open_Loss_To_CloseTrades=-1000;  //|The amount of money at which you want to take a loss & close ALL     |
                                               //|open trades. eg if the floating loss in your account reaches or goes |
                                               //|beyond -$1000 then ALL the open positions in your account will be    |
                                               //|closed.                                                              |      
                                               //+---------------------------------------------------------------------+
extern double EquityAmount = 1050;
int Slippage=5;                                 
int i;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   string msg="";
   
   //Show user feedback
   msg=StringConcatenate("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit(),"\n");
   if (Open_Loss_To_CloseTrades!=0) msg=StringConcatenate(msg,"My Account Cutoff Limit: ",Open_Loss_To_CloseTrades);
   if (EquityAmount!=0) msg=StringConcatenate(msg,"My Account Equity Target: ",EquityAmount);
   Comment(msg);
           
   //check for equity limit
   if(AccountEquity() >= EquityAmount)
   { 
      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);
         }  
      }
  
      Print("ALL ORDERS CLOSE-->Locked in on Profits");
   }   
   
   if (AccountProfit()<= Open_Loss_To_CloseTrades)
   {
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         OrderSelect(i, SELECT_BY_POS);
         type   = OrderType();
               
         result = false;
              
         switch(type)
         {
            //Close opened long positions
            case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Pink);
                           break;
               
            //Close opened short positions
            case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Pink);
                          
         }
          
         if(result == false)
         {
            Sleep(3000);
         }  
      }
      Print ("Account Cutoff Limit Reached. All Open Trades Have Been Closed");
      return(0);
   }  

//----
   return(0);
  }
//+------------------------------------------------------------------+