Exp-Close-----ALLORDERS----
Download scriptThis script is designed to act as an automated utility for managing active trading positions within the MetaTrader platform. Its primary objective is to provide a streamlined way to close out open trades quickly, either across your entire portfolio or on a specific currency pair you are currently viewing.
Core Functionality
The script scans your current trading account to identify open orders. Based on how you configure the settings, it performs a "sweep" of these positions to close them instantly. It is built to handle both buying and selling positions systematically, ensuring that every active trade is processed.
Key Features and Logic:
-
Scope Control: You have the flexibility to choose between two modes of operation:
- Global Mode: If enabled, the script will identify and close every single open order across all different symbols (currencies, metals, etc.) in your account simultaneously.
- Specific Symbol Mode: If you prefer to be more selective, the script can be set to only act upon the specific symbol that is currently active on your chart. This prevents it from touching other trades you might want to keep running.
-
Dynamic Market Awareness: Before executing a closure, the script performs a "live check" of the market environment. It automatically fetches the most recent Ask and Bid prices to ensure it has accurate data. This helps the script calculate the correct pricing for the trade at that exact millisecond, reducing the risk of execution errors due to outdated information.
-
Error Handling: The tool is programmed with a feedback mechanism. If a trade fails to close for any reason (such as a sudden loss of connection or a market halt), the script will capture the specific error message from the server and display it in the log, allowing you to see exactly what happened.
-
Visual Branding: Upon execution, the script places a small visual label on your chart, identifying the source and providing a reference point for the tool being used.
Summary for the User
Essentially, this is a "Panic Button" or a "Bulk Closer." It removes the manual effort of clicking every single trade one by one to exit the market. Whether you need to clear out your entire account's exposure in one click or just exit positions on one specific pair, this script automates that repetitive task with precision.
#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_SELL) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],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((OrderType()<=OP_SELL) && GetMarketInfo() && !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