Exp-Close-----PROFIT---------

Author: expforex
Exp-Close-----PROFIT---------

This script is designed to automate the management of your active trading positions by identifying and closing profitable trades based on specific user-defined criteria. It acts as a high-level "clean-up" tool for your trading account, ensuring that winning positions are exited systematically.

Core Functionality

The script operates by scanning your open trade list and applying a set of rules to decide which orders should be closed immediately. Its primary logic follows these steps:

1. Profit Verification The script first filters through all your active trades to identify positions that currently have a profit equal to or greater than zero. It ignores any trades that are currently in a loss, ensuring that only "winning" positions are targeted for closure.

2. Scope Selection (Global vs. Specific) The tool provides flexibility in how it scans your account:

  • All Symbols: If enabled, the script will look across your entire trading platform and attempt to close profitable trades on every asset you are currently trading.
  • Specific Symbol: If disabled, the script restricts its actions solely to the specific currency pair or asset that is currently open on your active chart.

3. Execution Logic For every trade that meets the profit requirement, the script validates the market data (such as current Ask and Bid prices) to ensure the execution is valid. Once confirmed, it automatically sends a command to close the position at the best available market price.

4. Error Handling and Logging To ensure transparency, the script includes a built-in reporting mechanism. If an order fails to close due to a platform error or connectivity issue, the script captures that specific error message and logs it, allowing you to see exactly why a trade might have stayed open.

Key Features Summary

  • Automated Profit Taking: Removes the need to manually monitor and click "Close" on every winning trade.
  • Dynamic Market Awareness: It constantly refreshes price data to ensure it is calculating the correct exit points in real-time.
  • Customizable Scope: Gives you the choice between a global "sweep" of all profitable trades or a surgical approach limited to one specific asset.
  • Visual Branding: Includes an automated visual label on the chart identifying the source of the tool.
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
10 Views
0 Downloads
0 Favorites
Exp-Close-----PROFIT---------
#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