This script is designed as an automated utility for the MetaTrader trading platform to manage and clear out losing positions quickly. Its primary purpose is to identify open trades that are currently in a deficit (negative profit) and close them immediately to prevent further losses.
How the Logic Works:
1. Scope of Execution The tool offers flexibility in how it scans your account. It can be configured in two ways:
- Global Mode: It scans every single open position across all different currency pairs or assets in your account.
- Symbol-Specific Mode: It focuses exclusively on the specific chart you have open, ignoring all other active trades.
2. Identification of Losing Trades Once the script begins its run, it systematically audits your open orders. It looks specifically for "losing" positions—those where the current profit is less than zero. It filters these trades to ensure they are valid types that can be closed (such as Buy or Sell orders).
3. Market Validation & Execution Before closing a trade, the script performs a real-time check of the market conditions. It retrieves the latest "Bid" and "Ask" prices to ensure the market is active and providing valid data. It then calculates the necessary price points to execute the exit commands accurately.
4. Error Handling & Feedback If the script attempts to close a trade and encounters an issue (such as a connection problem or a price fluctuation during execution), it will generate a specific error message. This allows you to see exactly what happened with a particular trade ticket rather than the script simply failing silently.
5. Visual Identification When the script is activated, it places a small text label on your trading chart. This serves as a visual confirmation that the tool has been initialized and identifies the source of the utility.
Summary for the User
Essentially, this is a "cleanup" tool. If you find yourself in a situation where multiple losing trades are open and you want to wipe out all current losses instantly—either across your whole account or just on one specific pair—this script automates that manual process in a matter of seconds.
#property show_inputs
#property copyright "expforex"
#property link "http://expforex.at.ua"
//+------------------------------------------------------------------+
//| EX4 imports |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| global variables to program: |
//+------------------------------------------------------------------+
double Price[2];
int giSlippage;
bool CloseOrdersWithPlusProfit = true;extern bool AllSymbols=true;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void init()
{
Stamp();
start();
}
void start() {
int iOrders=OrdersTotal()-1, i;
if (AllSymbols==true){
if(CloseOrdersWithPlusProfit) {
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());
}
}
}
}}
if (AllSymbols==false){
if(CloseOrdersWithPlusProfit) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderProfit() < 0)) {
if( OrderSymbol()!=Symbol())continue;
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);
}
//+------------------------------------------------------------------+
void Stamp(){
ObjectCreate("Original", OBJ_LABEL, 0, 0, 0);
ObjectSetText("Original", " ---http://Expforex.at.ua---", 10, "Arial Bold", Red);
ObjectSet("Original", OBJPROP_CORNER, 2);
ObjectSet("Original", OBJPROP_XDISTANCE, 200);
ObjectSet("Original", OBJPROP_YDISTANCE, 10);
}
Comments