Exp-Close-----PROFIT-SELL---
Download scriptThis script is an automated utility designed for MetaTrader users who need to quickly manage their open trading positions. Its primary purpose is to identify and close specific types of profitable trades automatically based on predefined criteria.
Core Functionality
The script scans your active trading account to find "Sell" positions that are currently in a profitable state (meaning the profit value is zero or greater). Once these specific trades are identified, the script executes an automatic closure for them.
Key Features and Logic:
- Profit-Based Filtering: The tool is designed to be safe; it only acts on trades that are already showing a profit. It will not touch trades that are currently in a loss.
- Flexible Scope (All Symbols vs. Specific Symbol):
- You can set the script to act globally, meaning it will scan every currency pair or asset you have open and close profitable sells across the entire account.
- Alternatively, you can restrict its action to only the specific chart/symbol you are currently viewing.
- Smart Market Data Handling: Before executing any trade closure, the script performs a real-time "handshake" with the market. It refreshes the latest prices and calculates the current "slippage" (the difference between the expected price and the actual execution price). This ensures that the trade is closed using the most accurate and up-to-date data available at that exact microsecond.
- Error Reporting: If the script encounters an issue while trying to close a trade (such as a connection problem or a requote from the broker), it captures the specific error code and prints a detailed message in the terminal log. This allows you to see exactly what happened if a trade failed to close as expected.
- Visual Branding: Upon execution, the script places a small text label on your chart identifying the source of the tool.
How It Works for the User
When you run this script, it performs a rapid "sweep" of your open orders. It filters out all Buy positions and all losing trades instantly. For every profitable Sell position it finds, it checks the current market price and closes the position immediately. It is essentially a "one-click" cleanup tool to lock in gains on specific types of trades across your portfolio.
#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