//+------------------------------------------------------------------+ //| closeallorders.mq5 | //| porquilho | //| | //+------------------------------------------------------------------+ #property copyright "porquilho" #property link "porquilho@gmail.com" #property version "1.00" #property description "Script to set Stop Loss" //+------------------------------------------------------------------+ //| CloseAll.mq5 | //| Copyright 2023, Your Name | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include void OnStart() { CTrade trade; // Create an instance of the CTrade class // Close all open positions for (int i = PositionsTotal() - 1; i >= 0; i--) // Loop backwards { ulong ticket = PositionGetTicket(i); // Get the ticket number of the position if (PositionSelectByTicket(ticket)) // Select the position by ticket { // Close the position if (trade.PositionClose(ticket)) { Print("Closed position: ", ticket); } else { Print("Failed to close position: ", ticket, " Error: ", GetLastError()); } } } // Close all pending orders for (int j = OrdersTotal() - 1; j >= 0; j--) // Loop backwards { ulong orderTicket = OrderGetTicket(j); // Get the ticket number of the order if (OrderSelect(orderTicket)) // Select the order by ticket { // Check if the order is pending if (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_LIMIT || OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_LIMIT || OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP || OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP) { // Close the pending order if (trade.OrderDelete(orderTicket)) { Print("Deleted pending order: ", orderTicket); } else { Print("Failed to delete pending order: ", orderTicket, " Error: ", GetLastError()); } } } } }