//+------------------------------------------------------------------+
//|                                                   CloseAllButton.mq4|
//|                                                                  |
//+------------------------------------------------------------------+
#property strict

// Create button and text
#define BUTTON_NAME "CloseAllButton"
#define INFO_NAME "OrderInfo"
input color ButtonColor = clrBlack;       // Button color
input int ButtonWidth = 100;             // Button width
input int ButtonHeight = 40;            // Button height
input color TextColor = clrBlack;       // Info text color
input int TextSize = 12;                // Info text size
input string ButtonFont = "Arial Black";      // Button font
input int ButtonFontSize = 12;          // Button font size
input color ButtonTextColor = clrRed; // Button text color

// Inputs for custom X and Y positions of the button
input int ButtonXPos = 10;   // X position of button
input int ButtonYPos = 100;   // Y position of button
input int InfoXPos = 10;     // X position of info text
input int InfoYPos = 150;     // Y position of info text

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Create button on chart
   CreateButton();
   
   // Create text on chart to show the number of buy and sell orders
   CreateInfo();

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Remove button and info on deinitialization
   ObjectDelete(BUTTON_NAME);
   ObjectDelete(INFO_NAME);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // Check if button is pressed
   if (ObjectFind(BUTTON_NAME) >= 0 && ObjectGetInteger(0, BUTTON_NAME, OBJPROP_STATE) == true)
     {
      CloseAllOrders(); // Close all orders when button pressed
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_STATE, false); // Reset button state
     }

   // Update the buy and sell order info
   UpdateInfo();
  }
//+------------------------------------------------------------------+
//| Create button                                                    |
//+------------------------------------------------------------------+
void CreateButton()
  {
   if (ObjectFind(BUTTON_NAME) < 0)
     {
      // Create the "Close" button
      ObjectCreate(0, BUTTON_NAME, OBJ_BUTTON, 0, 0, 0);
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_CORNER, 0);              // Corner of the chart (0: Top-Left)
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_XDISTANCE, ButtonXPos);  // X distance (customizable)
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_YDISTANCE, ButtonYPos);  // Y distance (customizable)
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_COLOR, ButtonColor);     // Button color
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_XSIZE, ButtonWidth);     // Button width
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_YSIZE, ButtonHeight);    // Button height
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_FONTSIZE, ButtonFontSize); // Button font size
      ObjectSetString(0, BUTTON_NAME, OBJPROP_TEXT, "CLOSE");           // Button text
      ObjectSetInteger(0, BUTTON_NAME, OBJPROP_COLOR, ButtonTextColor); // Button text color
      ObjectSetString(0, BUTTON_NAME, OBJPROP_FONT, ButtonFont);        // Button font
     }
  }
//+------------------------------------------------------------------+
//| Create info text for displaying number of buy and sell orders    |
//+------------------------------------------------------------------+
void CreateInfo()
  {
   if (ObjectFind(INFO_NAME) < 0)
     {
      // Create the info text
      ObjectCreate(0, INFO_NAME, OBJ_LABEL, 0, 0, 0);
      ObjectSetInteger(0, INFO_NAME, OBJPROP_CORNER, 0);       // Corner of the chart (0: Top-Left)
      ObjectSetInteger(0, INFO_NAME, OBJPROP_XDISTANCE, InfoXPos);   // X distance (customizable)
      ObjectSetInteger(0, INFO_NAME, OBJPROP_YDISTANCE, InfoYPos);   // Y distance (customizable)
      ObjectSetInteger(0, INFO_NAME, OBJPROP_COLOR, TextColor);  // Text color
      ObjectSetInteger(0, INFO_NAME, OBJPROP_FONTSIZE, TextSize); // Text size
      ObjectSetInteger(0, INFO_NAME, OBJPROP_HIDDEN, false);      // Ensure the label is visible
      ObjectSetString(0, INFO_NAME, OBJPROP_TEXT, "Loading...");  // Set initial text
     }
  }
//+------------------------------------------------------------------+
//| Update the number of buy and sell orders                         |
//+------------------------------------------------------------------+
void UpdateInfo()
  {
   int buyOrders = 0, sellOrders = 0;

   // Count the number of buy and sell orders
   for (int i = 0; i < OrdersTotal(); i++)
     {
      if (OrderSelect(i, SELECT_BY_POS))
        {
         if (OrderType() == OP_BUY) buyOrders++;
         if (OrderType() == OP_SELL) sellOrders++;
        }
     }

   // Update the info text
   string infoText = "BUY: " + IntegerToString(buyOrders) + "\n  SELL: " + IntegerToString(sellOrders);
   ObjectSetString(0, INFO_NAME, OBJPROP_TEXT, infoText);
  }
//+------------------------------------------------------------------+
//| Close all open positions and pending orders                      |
//+------------------------------------------------------------------+
void CloseAllOrders()
  {
   int totalOrders = OrdersTotal();
   for (int i = totalOrders - 1; i >= 0; i--)
     {
      if (OrderSelect(i, SELECT_BY_POS))
        {
         if (OrderType() == OP_BUY || OrderType() == OP_SELL)
           {
            // Close market orders and check return value
            if (OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3, clrNONE))
              Print("Order closed: ", OrderTicket());
            else
              Print("Failed to close order: ", OrderTicket());
           }
         else
           {
            // Delete pending orders and check return value
            if (OrderDelete(OrderTicket()))
              Print("Pending order deleted: ", OrderTicket());
            else
              Print("Failed to delete pending order: ", OrderTicket());
           }
        }
     }
  }
//+------------------------------------------------------------------+
