Close All purposes script

Hi folks,

Today we are going to study a very important piece of code that handles all the type of closing your opened orders! I'm saying it's important because of the huge number of private messages I'm receiving everyday asking me about this issue!

Our script today is all purposes closing script which you can use it to close your instant orders (the orders you open at the current market price which they are the normal orders and the opposite of pending orders), pending orders and more which we can see in the code. The most important thing is not the usage of the script (which is a very useful) but the understanding of the code.

CloseAll script:

The script (attached) is all purposes closing script which means you can decide what type orders you want to close; you can choose one of 8 options by its index number by setting the option parameter in the input window you get when you run the script (Figure 1):

 

These are the options:

0- Close all  the orders (instant and pending orders) - this is the default option which mean if you run the script and didn't set any parameters all the orders (instant and pending) will be closed.
1- Close all instant orders; instant orders mean the orders that opened at the market price.
2- Close all pending orders.
3- Close by the magic number. You have to set the magic_number variable.
4- Close by comment. You have to set the comment_text variable.
5- Close orders (instant orders) in profit and leave the orders in loss.
6- Close orders (instant orders) in loss and leave the orders in profit.
7- Close not today orders and leave the orders that opened today.

The other two variables which you can set are:

magic_number: Set it if you'll use closing option 3 - closing by magic number.
comment_text : Set it if you'll use closing option 4 - closing by comment.

Let's give the code a look:

CloseAll Code:


//+------------------------------------------------------------------+
//| CloseAll.mq4 |
//| CodersGuru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+

#property copyright "CodersGuru"
#property link "http://www.forex-tsd.com"
#property show_inputs

extern int option = 0;
//+------------------------------------------------------------------+
// Set this prameter to the type of clsoing you want:
// 0- Close all (instant and pending orders) (Default)
// 1- Close all instant orders
// 2- Close all pending orders
// 3- Close by the magic number
// 4- Close by comment
// 5- Close orders in profit
// 6- Close orders in loss
// 7- Close not today orders
//+------------------------------------------------------------------+

extern int magic_number = 0; // set it if you'll use closing option 3 - closing by magic number
extern string comment_text = ""; // set it if you'll use closing option 4 - closing by comment

int start()
{
CloseAll();
return(0);
}
//+------------------------------------------------------------------+

int CloseAll()
{
int total = OrdersTotal();
int cnt = 0;

switch (option)
{
case 0:
for (cnt = 0 ; cnt <= total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
if(OrderType()>OP_SELL) //pending orders
OrderDelete(OrderTicket());
}

break;
case 1:
for (cnt = 0 ; cnt <= total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
}
break;
case 2:
for (cnt = 0 ; cnt <= total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if(OrderType()>OP_SELL)
OrderDelete(OrderTicket());
}
break;
case 3:
for (cnt = 0 ; cnt <= total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == magic_number)
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
if(OrderType()>OP_SELL)
OrderDelete(OrderTicket());
}
}
break;
case 4:
for (cnt = 0 ; cnt <= total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (OrderComment() == comment_text)
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
if(OrderType()>OP_SELL)
OrderDelete(OrderTicket());
}
}
break;
case 5:
for (cnt = 0 ; cnt <= total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (OrderProfit() > 0)
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
if(OrderType()>OP_SELL)
OrderDelete(OrderTicket());
}
}
break;
case 6:
for (cnt = 0 ; cnt <= total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (OrderProfit() < 0)
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
if(OrderType()>OP_SELL)
OrderDelete(OrderTicket());
}
}
break;
case 7:
for (cnt = 0 ; cnt <= total ; cnt++)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (TimeDay(OrderOpenTime()) < TimeDay(CurTime()))
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5,Violet);
if(OrderType()>OP_SELL)
OrderDelete(OrderTicket());
}
}
break;
}
}

Let's give the code a deeper look:

Code explanation: