Close_All_Pair

Download script
Author: Copyright � 2008, David E. Fulton
Close_All_Pair

This script is a specialized automation tool designed for MetaTrader 4 users who need to quickly exit all active positions for a specific currency pair at once. Instead of manually closing every open trade one by one—which can be time-consuming during fast-moving market conditions—this script handles the process automatically.

How It Works

The logic behind the script follows a straightforward three-step process:

1. User Identification When you run the script, it looks for a specific currency pair that you have defined in the settings (for example, "GBPUSD"). This allows the tool to be precise; it will only affect the trades on that specific pair and leave your other active trades untouched.

2. Scanning Active Trades The script performs a comprehensive sweep of your current trading account. It identifies every open position, checking two primary pieces of information for each:

  • Which currency pair the trade belongs to.
  • Whether the trade is a "Buy" or a "Sell" position.

3. Selective Execution Once the script identifies a trade that matches your chosen currency pair, it executes an immediate closure command. It differentiates between the two types of trades to ensure accuracy:

  • If it finds a Buy position on your chosen pair, it closes it at the current market price.
  • If it finds a Sell position on your chosen pair, it also closes it at the current market price.

The script repeats this scanning process multiple times in rapid succession to ensure that every single open order for that specific pair is successfully closed, providing a "clean sweep" of your active positions in just a few seconds.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
15 Views
0 Downloads
0 Favorites
Close_All_Pair
//+------------------------------------------------------------------+
//|                                               Close_All_Pair.mq4 |
//|                                Copyright © 2008, David E. Fulton |
//|                                    http://www.defulton.com/forex |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2008, David E. Fulton"
#property link      "http://www.defulton.com/forex"
#property show_inputs   //shows the Properties dialog box to the user
#include <stdlib.mqh>

extern string INFO="Close_All_Pair V1.0";
extern string OWN="Copyright © 2008, David E. Fulton";
extern string title="--PAIR TO CLOSE--";
extern string pair="";

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   int total_trades=OrdersTotal();
   for (int i=0; i<5; i++)
      {
         for(int j=0;j<total_trades;j++)
            {
               OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
               int order_type=OrderType();
               int order_ticket=OrderTicket();
               double order_volume=OrderLots();
               string order_symbol=OrderSymbol();
                                   
               //close long gbpusd orders
               if ((order_symbol==pair) && (order_type==OP_BUY))
                  OrderClose(order_ticket,order_volume,MarketInfo(order_symbol,MODE_BID),3,CLR_NONE);
         
               //close short gbpusd orders
               if ((order_symbol==pair) && (order_type==OP_SELL))
                  OrderClose(order_ticket,order_volume,MarketInfo(order_symbol,MODE_ASK),3,CLR_NONE);
      
            }
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments