//+------------------------------------------------------------------+
//|                                                     CloseAll.mq4 |
//|                                         Copyright © 2008, autofx |
//+------------------------------------------------------------------+

// Use on any chart.
// Use caution.  This closes ALL trades and orders, regardless of
// symbol, magic number, timeframe, profit or loss.

extern int Slippage = 4;

int    i;

int init()
{
  while (true)                                                             // infinite loop for main program
  {
    CloseAll();
  }
   
  return(0);                                                               // end of init function
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
  return(0);
}

void CloseAll()
{
  for(i = 0; i < OrdersTotal(); i++)
  {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      if (OrderType() == OP_BUY)
      {
        Comment("CloseAll is closing a buy...");
        OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,White);
        Sleep(1000);
      }    
      else
      if (OrderType() == OP_SELL)
      {
        Comment("CloseAll is closing a sell...");
        OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,White);
        Sleep(1000);
      }
      else
      {
        Comment("CloseAll is deleting a pending order...");
        OrderDelete(OrderTicket());
        Sleep(1000);
      }
  }
  return(0);
}

