//+------------------------------------------------------------------+
//|                                             Close All Trades.mq4 |
//|                                                    Keith Watford |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "Keith Watford"
#property link      "none"
#property version   "1.00"
#property strict
#property  show_inputs

input bool ClosePendings=true;//Close Pending Orders
input bool CloseBS=true;//Close Open Orders
#include <stdlib.mqh>
#include <stderror.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
  for(int i=OrdersTotal()-1;i>=0;i--)
  {
    if(OrderSelect(i, SELECT_BY_POS))
    {
    int type   = OrderType();
    bool result = true;
    
    switch(type)
    {
      case OP_BUY   : if(CloseBS) result = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 50, Red );break;
      case OP_SELL  : if(CloseBS) result = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 50, Red );break;
      default       : if(ClosePendings)result = OrderDelete( OrderTicket() );break;
    }
    
    if(result == false)
    {
      Alert("Error Closing " , OrderTicket() , " -" , ErrorDescription(GetLastError()) );
      Sleep(3000);
    }
    }  
  }
  
  return;
//---
   
  }
//+------------------------------------------------------------------+
