Exp-Close-----PROFIT--BUY---
Download scriptThis script is an automated management tool designed for the MetaTrader trading platform. Its primary purpose is to automatically identify and close specific "Buy" positions based on their profitability status across your trading account.
Core Functionality
The script scans your active trading portfolio to identify open orders that meet specific criteria. When executed, it performs the following logic:
- Profitability Check: The tool looks for any currently open trades that are in a state of profit (where the current profit is zero or higher).
- Trade Type Filtering: It specifically targets "Buy" orders. It ignores "Sell" orders or other types of pending requests.
- Execution Logic: Once a profitable Buy order is identified, the script automatically sends a command to close that position at the current market price.
Key Customization Options
The tool provides users with two main modes of operation to control how it interacts with your trades:
-
Scope of Operation (All Symbols vs. Specific Symbol):
- Global Mode: If enabled, the script will scan and close profitable Buy orders across every currency pair or asset you are trading at once.
- Specific Symbol Mode: If restricted, the script will only interact with trades belonging to the specific chart you have open.
-
Profitability Filter: The tool is designed to ensure that it only closes trades that are currently "in the green." This prevents the script from accidentally closing losing positions.
Technical Handling & Reliability
To ensure the tool works accurately in a live market environment, it includes several automated background processes:
- Real-Time Data Refresh: The script automatically refreshes the latest market data to ensure it is viewing the most current prices before attempting to close a trade.
- Dynamic Price Calculation: It calculates the necessary "slippage" (the difference between the expected price and the actual execution price) automatically based on the current market movement.
- Error Reporting: If a trade fails to close for any reason (such as a connection issue or a market freeze), the script generates a detailed error message to help you understand exactly what happened.
- Visual Identification: The script places a small watermark/label on your chart upon execution, identifying the source of the tool for easy reference.
#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()) {
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