Exp-Close-----LOOSE--BUY---
Download scriptThis script is designed as an automated risk management tool for MetaTrader users who need to quickly clear out losing positions from their trading accounts. Its primary purpose is to identify and close "underwater" trades—those currently showing a negative profit—based on specific user-defined filters.
Core Functionality
When the script is executed, it scans your active trading dashboard to locate open orders. The logic follows a structured sequence to determine which trades should be closed:
- Loss Detection: The system automatically checks all open positions to see if they are currently in a loss (negative profit).
- Position Filtering: Depending on your settings, you can choose how the script behaves:
- Global Mode: If enabled, the script will scan every single open position across all different currency pairs and assets in your account.
- Specific Symbol Mode: If disabled, the script limits its actions only to the specific currency pair or asset that is currently active on your chart.
- Execution: Once a losing "Buy" position meets the criteria, the script automatically sends a command to close that trade at the current market price.
Technical Reliability Features
To ensure the script operates smoothly without errors, it includes several background safety checks:
- Live Price Syncing: Before closing any trade, the script refreshes the latest market data to ensure it is using the most accurate current "Ask" and "Bid" prices.
- Slippage Calculation: It calculates the minimum allowed price difference (slippage) to ensure that trades are executed correctly even during periods of high market volatility.
- Error Reporting: If the system fails to close a specific trade due to a connection issue or a market restriction, it generates a specific error message to help you identify what went wrong.
User Interface
Upon running, the script also places a small visual label on your chart. This serves as a watermark to identify the source of the tool, ensuring you can easily track which utilities are active on your workspace.
In short, this is a "clean-up" utility designed to help traders quickly eliminate losing positions to protect their capital, offering the flexibility to either wipe out losses across your entire portfolio or target just one specific instrument at a time.
#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_BUY) && 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_BUY) && GetMarketInfo()&& OrderSymbol()==Symbol()) {
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