Exp-Close-----LOOSE--SELL---

Author: expforex
Exp-Close-----LOOSE--SELL---

This tool is designed to automate the management of your trading positions by identifying and closing specific types of losing trades automatically. It acts as a "safety net" for your account, helping to manage risk by clearing out underperforming positions based on pre-defined criteria.

How the Logic Works

The script follows a structured sequence to evaluate your open trades and perform actions:

1. Initial Scan and Identification When executed, the tool scans all of your currently open trading positions. It checks these positions against two primary filters:

  • Profitability: It specifically looks for orders that are currently in a negative state (meaning they are currently at a loss).
  • Trade Type: It focuses on "Sell" orders.

2. Scope Selection You have the flexibility to choose how broadly the script should operate:

  • Global Mode: If enabled, the script will scan every single open position across all different currency pairs or assets in your account.
  • Specific Symbol Mode: If you prefer more control, the script can be restricted to only act on the specific chart/asset you are currently viewing.

3. Market Validation and Execution Before closing a trade, the script performs a real-time check of the current market conditions. It verifies that:

  • Valid price data is available from the broker.
  • The current "Bid" and "Ask" prices are active.
  • It calculates the necessary "slippage" (the difference between expected and actual execution price) to ensure the order can be closed smoothly without errors.

4. Automatic Closure Once a trade meets all the criteria—it is a "Sell" order, it is currently losing money, and the market data is confirmed—the script automatically sends a command to close that position immediately. If the closing action fails for any reason, the tool is designed to record and display a specific error message so you can troubleshoot the issue.

5. Visual Confirmation Upon running, the tool places a small text label on your chart as a signature/reference, confirming that the script has been initialized and is active on your workspace.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
14 Views
0 Downloads
0 Favorites
Exp-Close-----LOOSE--SELL---
#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()&& 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