Close_All_Open

Download script
Author: (c) 2007 Investatech Inc., www.investatech.com
Close_All_Open

This script is a management tool designed to automatically clear your trading portfolio by closing all active positions simultaneously. Instead of manually clicking "close" on every individual trade, this tool automates the process to ensure your account is cleared of open market exposure quickly.

How the Logic Works

The script operates through a systematic three-step process:

1. Detection and Scanning When you run the script, it first looks at your entire trading account to count how many active trades (positions) are currently open. If it detects that there are no active trades at all, it immediately informs you that the job is complete.

2. Systematic Execution If active trades are found, the script enters a loop where it examines every single open position one by one. It identifies the specific details of each trade, including:

  • The type of trade (whether it is a Buy or a Sell).
  • The volume (the size of the position).
  • The current market price to ensure the trade is closed at the most accurate available rate.

3. Order Finalization For every position identified, the script performs the necessary action to exit the market:

  • If it finds a Sell position, it executes a command to close it immediately.
  • If it finds a Buy position, it executes a command to close it immediately.

To ensure high reliability, the script repeats this scanning and closing process five times in rapid succession. This redundancy is designed to handle instances where multiple orders might be processed at once or where trade updates might cause minor delays in the platform's reporting.

Final Confirmation

Once the script finishes its cycles, it performs a final check on your account:

  • Success: If all positions are closed, the script remains silent or concludes its task.
  • Error Handling: If there are still remaining open trades after all cycles are complete, the script will trigger an alert notification. This tells you that something went wrong (such as a connection issue or a specific trade being locked) and prompts you to run the script again to ensure your portfolio is completely clear.
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
16 Views
0 Downloads
0 Favorites
Close_All_Open
//+------------------------------------------------------------------+
//|                                               Close_All_Open.mq4 |
//|                  (c) 2007  Investatech Inc., www.investatech.com |
//|                                       http://www.investatech.com |
//+------------------------------------------------------------------+
#property copyright "(c) 2007  Investatech Inc., www.investatech.com"
#property link      "http://www.investatech.com"
#property show_inputs
#include <stdlib.mqh>

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   for (int i=0;i<5;i++)
      {
         int total_trades=OrdersTotal();
   
         if (total_trades==0) 
           {
            Alert("Job Done! Visit http://www.pipboxer.com");
            return(0);
           }
   
         for(int j=0;j<total_trades;j++)
            {
              if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES)) 
               {
                  int order_type=OrderType();
                  double order_openprice=OrderOpenPrice();
                  int order_ticket=OrderTicket();
                  double order_volume=OrderLots();
                  string order_symbol=OrderSymbol();
                  
                  //close pending orders
                  //if (order_type>=OP_BUYLIMIT) OrderDelete(order_ticket,CLR_NONE);
                  
                  //close sell orders
                  if (order_type==OP_SELL) OrderClose(order_ticket,order_volume,MarketInfo(order_symbol,MODE_ASK),3,CLR_NONE);

                  
                  //close buy orders
                  if (order_type==OP_BUY) OrderClose(order_ticket,order_volume,MarketInfo(order_symbol,MODE_BID),3,CLR_NONE);
               }
            }
      }
   total_trades=OrdersTotal();
   
   if (total_trades>0)
      Alert("Something went wrong! Repeat the script.");
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments