a6 Close ALL LOSING Pstns

Author: Copyright � 2006, Ronald Verwer, Forex MetaSoft
a6 Close ALL LOSING Pstns

This script is a specialized automated tool designed for traders who need to quickly manage their risk by identifying and closing out all unprofitable trades simultaneously. It acts as an "emergency exit" button that scans your active portfolio and executes immediate actions based on the current profit or loss status of each position.

How the Logic Works

1. Comprehensive Portfolio Scan The script begins by scanning every single active trade currently open in your trading account. It looks at all currency pairs or assets you are trading at that exact moment to ensure nothing is overlooked.

2. Identifying Unprofitable Positions As it goes through your list of trades, the tool performs a specific check: it looks for any "Buy" or "Sell" positions where the current profit value is less than zero (meaning the trade is currently in a loss). It ignores any trades that are currently profitable or at a break-even point.

3. Immediate Market Execution Once a losing position is identified, the script automatically triggers a command to close that trade at the current market price. Because markets move quickly, the script is designed to find the best available "Bid" or "Ask" price instantly to ensure the position is exited as fast as possible.

4. Error Handling and Persistence The script includes a "persistent" logic to handle high-volatility moments. 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 will automatically refresh its data and try again immediately until the position is successfully finalized.

Summary of Action

In simple terms, this tool serves as an automated "Stop Loss" for your entire account. Instead of you having to manually click "Close" on every individual losing trade one by one, this script does the heavy lifting for you—identifying every losing trade and closing them instantly to protect your remaining capital.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
16 Views
0 Downloads
0 Favorites
a6 Close ALL LOSING Pstns
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                      a6 Cls ALL LOSING Pstns.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Ronald Verwer, Forex MetaSoft"
#property link "www.forexmetasoft.com"
#property show_confirm
//+------------------------------------------------------------------+
//| script to close all LOSING 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