a5 Close ALL WINNING Pstns

Download script
Author: Copyright � 2006, Ronald Verwer, Forex MetaSoft
a5 Close ALL WINNING Pstns

This script is designed as an automated utility for MetaTrader 4 users who want to quickly secure profits across their entire trading portfolio with a single action. It functions as an "emergency exit" or a "profit-taking tool" that identifies and closes all currently profitable trades instantly at the current market price.

How the Logic Works

The script operates by scanning through every open trade in your account simultaneously. Instead of requiring you to manually select each position, it performs the following logic:

  • Comprehensive Portfolio Scan: The system looks at every active trade you have open across all different currency pairs or assets.
  • Profit Verification: For every trade it finds, it checks the real-time profit status. It specifically ignores any trades that are currently in a loss or "breakeven" state.
  • Targeted Identification: It focuses exclusively on "Buy" and "Sell" positions that have a positive profit value (meaning they are currently "in the green").
  • Automated Execution: Once a winning trade is identified, the script automatically sends a command to close that position at the current market price.
  • Error Handling & Persistence: If the market moves so quickly that a trade cannot be closed on the first attempt (a common occurrence in fast-moving markets), the script is programmed to "refresh" its data and immediately try again until the trade is successfully finalized.

Key User Benefits

  • Speed: It executes these actions much faster than a human could manually click through dozens of open positions.
  • Precision: It ensures that no losing trades are accidentally closed, as it strictly filters for positive profit values only.
  • Reliability: By including a "retry" logic, the script handles minor market fluctuations to ensure your profits are locked in even during volatile price movements.
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
14 Views
0 Downloads
0 Favorites
a5 Close ALL WINNING Pstns
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                     a5 Cls ALL WINNING Pstns.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ronald Verwer, Forex MetaSoft"
#property link "www.forexmetasoft.com"
#property show_confirm
//+------------------------------------------------------------------+
//| script to close all WINNING positions at market                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,type,err,Slippage=3;
   double price;
   bool   result;
//----
   for(i=OrdersTotal()-1;i>=0;i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
         type=OrderType();
         if((type==OP_BUY || type==OP_SELL) && OrderProfit()>0)
            {
            while(true)
               {
               if(type==OP_BUY) price=MarketInfo(OrderSymbol(),MODE_BID);
               else price=MarketInfo(OrderSymbol(),MODE_ASK);
               result=OrderClose(OrderTicket(),OrderLots(),price,Slippage,CLR_NONE);
               if(result!=true) {err=GetLastError(); Print("LastError = ",err);}
               else err=0;
               if(err==135) RefreshRates();
               else break;
         }  }  }
      else Print( "When selecting a trade, error ",GetLastError()," occurred");
      }
   return(0);
   }
//+------------------------------------------------------------------+

Comments