Exp-Close-----SELL------------
Download scriptThis script is an automated management tool designed for MetaTrader users to quickly manage open trading positions. Its primary purpose is to identify and close specific types of active trades automatically based on user-defined criteria.
Core Functionality
The logic revolves around a "cleanup" action: scanning your current trading account to find active Sell positions and closing them immediately. Depending on how you configure the settings, the script can behave in two different ways:
- Global Mode: If enabled, the script will scan every open trade across all different currency pairs or assets in your account and close any Sell orders it finds.
- Specific Asset Mode: If disabled, the script becomes more selective. It will only look for Sell orders on the specific chart/symbol you currently have open, leaving your trades on other assets untouched.
How it Works Internally
- Market Data Verification: Before attempting to close any trades, the script performs a real-time check of the market. It retrieves the current "Ask" and "Bid" prices to ensure that market data is valid and updated. It also calculates the correct "slippage" (the allowable difference between expected and executed price) based on the specific point value of the asset.
- Sequential Scanning: The script moves through your open orders one by one, starting from the most recent. For every order it encounters, it checks two things: is it an open trade, and is it a Sell position?
- Execution and Error Reporting: If a matching Sell position is found, the script sends a command to the server to close it. If the closing action fails for any reason (such as a connection issue or a price movement), the script is designed to capture that specific error message and display it in your terminal logs so you can troubleshoot the problem.
- Visual Branding: Upon running, the script places a small text label on your chart identifying the source of the tool.
Summary for the User
Think of this tool as an "Emergency Exit" button for Sell positions. It eliminates the need to manually click through every open trade in your portfolio; instead, it performs a rapid sweep of your account to identify and exit Sell trades instantly, ensuring you can react quickly to market changes.
#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 CloseOpenOrders = true;extern bool AllSymbols=true;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void init()
{
Stamp();
start();
}
void start() {
int iOrders=OrdersTotal()-1, i;
if (AllSymbols==true){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()==OP_SELL) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[0],giSlippage)) Print(OrderError());
}
}}
if (AllSymbols==false){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{ if( OrderSymbol()!=Symbol())continue;
if( OrderSymbol()==Symbol() && (OrderType()==OP_SELL) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[0],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