CloseAllLosses

Download script
Author: Copyright � 2008, PRMQuotes Software Corp.
CloseAllLosses

This script is designed as an automated utility for MetaTrader 4 users who want to quickly clear out underperforming trades from their accounts. Its primary purpose is to identify and close any open positions that are currently in a loss state.

Core Functionality

The script performs a comprehensive sweep of your active trading portfolio. It looks at every open order simultaneously and checks two main criteria:

  1. Profit Status: It determines if the trade is currently showing a profit of zero or less (a negative balance).
  2. Trade Type Compatibility: It ensures it is interacting with valid buy or sell orders to execute the closing command correctly.

How the Logic Works

When you run this script, it initiates the following automated sequence:

  • Portfolio Scan: The tool scans your entire "Trade" window from the most recent order back to the first.
  • Loss Identification: For every trade found, the script calculates the current floating profit. If the value is negative, it flags that specific trade for closure.
  • Real-Time Price Verification: Before attempting to close a losing trade, the script fetches the most recent "Ask" and "Bid" prices from the market. This ensures that the closing price used by the script is as accurate as possible at the exact moment of execution.
  • Execution & Error Logging: The script then sends a command to the server to close the identified losing trade immediately. If for any reason a trade cannot be closed (such as a connection issue or a market restriction), the script captures that specific error and records it in the system log so you can review what happened.

Summary for the User

Think of this tool as an "emergency exit" button for your trades. Instead of manually clicking on every losing position to close them one by one, this script automatically detects any trade that is "in the red" and shuts it down instantly to prevent further losses on those specific positions.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
12 Views
0 Downloads
0 Favorites
CloseAllLosses

//+------------------------------------------------------------------+
//|                                               CloseAllLosses.mq4 |
//|                       Copyright © 2008, PRMQuotes Software Corp. |
//|                                           Jedimedic77@gmail.com  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, PRMQuotes Software Corp."
#property link      ""
//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| global variables to program:                                     |
//+------------------------------------------------------------------+
double Price[2];
int    giSlippage;
bool   CloseOrdersWithMinusProfit = true;
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void start() {
  int iOrders=OrdersTotal()-1, i;
  
  if(CloseOrdersWithMinusProfit) {
    for(i=iOrders; i>=0; i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderProfit() <= 0)) {
        if((OrderType()<=OP_SELL) && GetMarketInfo()) {
          if(!OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
        }
      }
    }
  }
}
//+------------------------------------------------------------------+
//| Function..: OrderError                                           |
//+------------------------------------------------------------------+
string OrderError() {
  int iError=GetLastError();
  return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError)));
}
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo                                        |
//+------------------------------------------------------------------+
bool GetMarketInfo() {
  RefreshRates();
  Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
  Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
  double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);
  if(dPoint==0) return(false);
  giSlippage=(Price[0]-Price[1])/dPoint;
  return(Price[0]>0.0 && Price[1]>0.0);
}
//+------------------------------------------------------------------+

Comments