Exp-Close-----BUY------------
Download scriptThis script is designed as a utility for MetaTrader users to quickly manage and clear out active trading positions. Its primary function is to automatically identify and close "Buy" orders currently held in your trading account based on specific user preferences.
Core Functionality
The script scans your active trades and executes the following logic:
- Order Filtering: The tool allows you to choose between two modes of operation. You can set it to manage every single "Buy" order across all currency pairs and assets in your account simultaneously, or you can restrict its actions to only the specific chart (symbol) you are currently viewing.
- Automated Execution: Once activated, the script identifies all open positions of the "Buy" type. It then attempts to close these positions at the current market price immediately.
- Market Awareness: Before attempting to execute a trade, the script performs a real-time check of the market data. It ensures that valid prices are available and calculates the necessary slippage (the difference between the expected and actual execution price) to ensure the order is processed correctly by the server.
Key Features
- Dynamic Error Reporting: If an order fails to close due to a connection issue, insufficient funds, or other market restrictions, the script captures the specific error code provided by the server and prints it in the log. This allows you to see exactly why a specific trade couldn't be finalized.
- Customizable Scope: By toggling the "All Symbols" setting, you have full control over whether this is a global account "clean-up" tool or a local position management tool.
- Visual Branding: The script includes a small visual identification mark on the chart to indicate its presence and source information.
Summary for the User
Essentially, this is a "Bulk Close" tool for Buy positions. Instead of manually right-clicking and closing every individual trade one by one, you can run this script to handle the process automatically, either for your entire portfolio or just for the asset you are currently watching.
#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;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
extern bool AllSymbols=true;
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_BUY) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[1],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_BUY) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[1],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