Exp-Close-----SelectType-----
Download scriptThis script is a management tool designed for the MetaTrader platform to automate the clearing of trading positions and pending orders based on specific user criteria. Instead of manually closing every individual trade, this utility allows you to execute bulk actions across your account with a single command.
Core Functionality
The primary logic of the script revolves around a selection menu that allows users to define exactly which types of trades should be processed. The script can perform several distinct actions:
- Bulk Closing: You can choose to close all currently open "Buy" positions, all "Sell" positions, or every active trade on your account simultaneously.
- Pending Order Removal: The script includes the ability to identify and delete "Buy Stop" or "Sell Stop" pending orders, clearing them from your workspace without affecting active trades.
- Profit-Based Filtering: A unique feature of this tool is its ability to filter trades based on their financial performance. It can automatically identify and close only those positions that are currently in a profit state or, conversely, those that are currently in a loss.
Operational Logic
The script operates using two main "filters" to ensure it acts only on the data you intend to modify:
- Symbol Scope: You can choose whether the script should scan your entire trading account for matching orders or restrict its actions solely to the specific currency pair (symbol) currently active on your chart.
- Execution Safety: Before performing any action, the script performs a real-time market check. It validates current market prices and calculates the necessary "slippage" (the allowable difference between expected and actual execution price) to ensure that the trades are closed successfully without errors.
User Experience
When executed, the script identifies all active orders and systematically moves through them. If an order matches your chosen criteria (e.g., "All Sell orders on the current symbol"), the script triggers a command to close or delete it. If the market conditions prevent a specific trade from closing, the script is designed to log an error message to the console for your review, providing transparency into the execution process.
#property show_inputs
#property copyright "expforex"
#property link "http://expforex.at.ua"
//+------------------------------------------------------------------+
//| EX4 imports |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#property show_inputs
extern string Ðàçðàáîò÷èê="http://expforex.at.ua";
extern string òåêñò="0-Çàêðûòü âñå îðäåðà";
extern string òåêñò2="1-Çàêðûòü âñå îðäåðà Buy";
extern string òåêñò3="2-Çàêðûòü âñå îðäåðà Sell";
extern string òåêñò4="3-Çàêðûòü âñå îðäåðà BuyStop";
extern string òåêñò5="4-Çàêðûòü âñå îðäåðà Sellstop";
extern string òåêñò6="5-Çàêðûòü âñå îðäåðà ñ ïðîôèòîì";
extern string òåêñò7="6-Çàêðûòü âñå îðäåðà ñ óáûòêîì";
extern int Ñïîñîáçàêðûòèÿ=0;
extern bool AllSymbols=true;
//+------------------------------------------------------------------+
//| global variables to program: |
//+------------------------------------------------------------------+
double Price[2];
int giSlippage;
bool CloseOpenOrders = true;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void init()
{
Stamp();
start();
}
void start() {
int iOrders=OrdersTotal()-1, i;
if (AllSymbols==true){
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà íà ïðîäàæó |
//+------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==2||Ñïîñîáçàêðûòèÿ==0){
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(Ñïîñîáçàêðûòèÿ==1||Ñïîñîáçàêðûòèÿ==0){
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());
}
}
}
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà íà ïîêóïêó Stop |
//+------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==3||Ñïîñîáçàêðûòèÿ==0){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()==OP_BUYSTOP) && !OrderDelete(OrderTicket())) Print(OrderError());
}
}
}
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà íà ïðîäàæó Stop |
//+-------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==4||Ñïîñîáçàêðûòèÿ==0){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()==OP_BUYSTOP) && !OrderDelete(OrderTicket())) Print(OrderError());
}
}
}
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà â ïðîôèòå |
//+--------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==5){
if(CloseOpenOrders) {
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(Ñïîñîáçàêðûòèÿ==6){
if(CloseOpenOrders) {
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(Ñïîñîáçàêðûòèÿ==2||Ñïîñîáçàêðûòèÿ==0){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol()&& (OrderType()<=OP_SELL) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
}
}
}
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà íà ïîêóïêó |
//+------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==1||Ñïîñîáçàêðûòèÿ==0){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol()&& (OrderType()==OP_BUY) && GetMarketInfo() && !OrderClose(OrderTicket(),OrderLots(),Price[1],giSlippage)) Print(OrderError());
}
}
}
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà íà ïîêóïêó Stop |
//+------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==3||Ñïîñîáçàêðûòèÿ==0){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol()&& (OrderType()==OP_BUYSTOP) && !OrderDelete(OrderTicket())) Print(OrderError());
}
}
}
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà íà ïðîäàæó Stop |
//+-------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==4||Ñïîñîáçàêðûòèÿ==0){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol()&& (OrderType()==OP_BUYSTOP) && !OrderDelete(OrderTicket())) Print(OrderError());
}
}
}
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà â ïðîôèòå |
//+--------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==5){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol()&& (OrderProfit() >= 0)) {
if((OrderType()<=OP_SELL) && GetMarketInfo()) {
if(!OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
}
}
}
}
}
//+------------------------------------------------------------------+
//| Çàêðûòü âñå îðäåðà â óáûòêå |
//+--------------------------------------------------------------------+
if(Ñïîñîáçàêðûòèÿ==6){
if(CloseOpenOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol()&& (OrderProfit() < 0)) {
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