Close all orders and positions - MetaTrader 5 Script | Trading Script Download - MT4/MT5 Resources
This sometimes happens when you create an EA and you do not notice an error that leads to opening too many orders, if not orders. Alternatively, when you use Metaeditor, there may be trade execution code outside of the If statement somewhere in Ontick that opens the trade in the MT5 backend
Trying to close too many orders and positions manually is boring and time-consuming. Let's create a script that turns all of this off.
01: Instantiate the library to be used
#Include <trade\trade.mqh> //Create a trade execution library #Include <Transaction\OrderInfo.mqh> //Create order information database #Include <Transaction\PositionInfo.mqh> //Create position information database //--- CTrade m_trade; // Trading information and execution library COrderInfo m_order; // Order information library CPositionInfo m_position; // Library for all position characteristics and information
02: Our simple input
Input color order color = clr dodge blue ; // Order counter color on the chart Input color position color = clr green yellow ; //Set counter color on chart
03: (Optional) Let's draw objects on the chart to calculate the number of positions and orders available in the MT5 trading section.
Chart Write( "Position" , "Position" + ( Thin String ) Total Positions (), 100 , 80 , 20 , clear green ); // Write the position quantity on the chart Chart Write ( "Order" , "Order" + ( Thin String ) Total Orders (), 100 , 50 , 20 , clr Dodge Blue ); // Write the order quantity on the chart
04: Let our script first process the positions by closing all positions
for ( integer i = total number of positions () - 1 ; i >= 0 ; i - ) // loop through all open positions if (m_position.SelectByIndex(i)) // select a position { m_trade.PositionClose(m_position.Ticket()); // Then delete it --period sleep ( 100 ); // Relax for 100 ms chart write( "position" , "position" + ( string ) total number of positions (), 100 , 80 , 20 , position color); //rewrite the number of positions on the chart }
05: Then finish by closing our order
for ( integer i = total number of orders () - 1 ; i >= 0 ; i - ) // loop through all available orders if (m_order.SelectByIndex(i)) // select an order { m_trade.OrderDelete(m_order.Ticket()); // Delete it -- period sleep ( 100 ); // Relax for 100 ms Chart Write( "Order" , "Order" + ( string ) Total Orders (), 100 , 50 , 20 , Order Color); // Rewrite the order quantity on the chart }
This is where our object is initialized. To understand objects, read the documentation here
Blank chart to write ( string name, string comments, integer x_distance, integer y_distance, integer font size, color clear) { ObjectCreate ( 0 , name, OBJ_LABEL , 0 , 0 , 0 ); object set integer ( 0 ,name, objprop_corner , corner-upper-left ); ObjectSetInteger ( 0 , name, OBJPROP_COLOR , clear); ObjectSet String ( 0 , Name, OBJPROP_TEXT , Comments); ObjectSetInteger ( 0 , name, OBJPROP_FONTSIZE , font size); Object Set String ( 0 , Name, OBJPROP_FONT , "Lucida Console" ); ObjectSetInteger ( 0 , name, OBJPROP_SELECTABLE , false ); ObjectSetInteger ( 0 , name, OBJPROP_XDISTANCE , x_distance); ObjectSetInteger ( 0 , name, OBJPROP_y_distance , y_distance); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+
This script helps you close all orders and positions in the blink of an eye.
Attachment download
📎 close_all_orders_and_positions.mq5 (3.75 KB)
Source: MQL5 #36010
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •