Articles on MQL4
Closing and deleting orders

Closing and deleting orders


for example, forex

Now we can consider the mechanism of deleting and closing market orders in MT4. We know that with the help of the function OrderSelect (number in the list , SELECT_BY_POS) we choose the order for which we then receive its parameters. The functions OrderClose() and OrderDelete() require such parameter as the number of order ticket, which we’ll recieve with the help of OrderTicket(). Let’s suppose that we need to close all open and delete all pending orders quickly. We’ll create for this purpose a new script StopTrade. mq4 . The simpliest way to do it is to search the list of orders using the cycle for() and depending on the type of order to close or delete each of them. The type of order we’ll get by the function OrderType(). It is sufficient to know the number of ticket to delete a pending order, but it is necessary to indicate the correct price at which we will close orders of the type OP_BUY or OP_SELL to close an order. As in the terminal a lot of orders for different instrument can be opened, and the script will be executed on the chart of one of them, we require the possibility of getting values of Ask and Bid for any symbols. For this purpose there is the function MarketInfo(). The expression MarketInfo(«GBPUSD», MODE_ASK) means Ask price for the symbol GBPUSD. The value of the symbol we get from OrderSymbol(). As a prompt I’ve made a table of the list of orders, where the selected line for the order on the 8-th position in the list (highlighted with yellow background) means that this order is chosen by the function OrderSelect(), and below I placed names of functions with the help of which it is possible to receive necessary parameters of the order. In the picture the function OrderTicket() used to get the number of ticket for the chosen order is selected.

 
 

We have all necessary data for writing the script. But the first operation in our script will be verification of the account type on which it is executed. We write this script as a training one, that’s why it is necessary to make protection from an accidental use of the script on a live account. For verification of the account type we use the function isDemo(), which returns true for demo-account, otherwise false.

 
 

Exclamation mark (!) is used for logic expression and makes an “overturn”: expression ! true turns into false, and ! false turns into true. Expression ! isDemo() on a demo account will return value false ( true will turn into false), and on a live account it will return true. In this case the script will terminate the work early, previously having given the warning “The work on a live account is prohibited”. It is preferably to use such protection in all scripts (making trading operation) and expert advisors to avoid unpleasant surprises.
While searching orders in the cycle we’ll have to verify the type of order, there are 6 types of orders — 2 open (Buy or Sell) and 4 pending. Buy we’ll close at Bid price, and Sell at Ask price for the symbol for which the order is open. It would be preferably to delete the remaining 4 types of pending orders in a like manner irrespectively of buylimit or sellstop order it is. If we look into help we will see that constants 0 and 1 means open orders, while pending ordes have OrderType()>1(2,3,4 and 5).

 
 

Now we can apply deleting OrderDelete() for orders with OrderType()>1, and two other types of orders we can close with the help of OrderClose(). But it isn’t very good to write a lot of comparison operators if(OrderType equals to n) after OrderSelect(), we’ll do it more gracefully. (Expression a==b makes comparison between a and b, and if they are equal it returns true.) For such cases when equality of expression to a limited number of values is verified the operator switch() suits very well. So we’ve got such variant:

 
 

Now we only have to write out closing and deleting. For deleting we simply write OrderDelete(ticket), where =OrderTicket(). For closing open orders we will receive current closing prices with the help of MarketInfo(OrderSymbol(), …). The size of the position that is closed we will receive from OrderLots(),I assigned slippage equal to 3, and I set the red color for the drawn arrow of closing. If you don’t remember what parameters and in what order it is necessary to transfer them to the function, you can get a prompt by pressing the shortcut key. In the picture I fixed the cursor between brackets in OrderClose():

 
 

Now the script is almost ready, we only have to decide how to close and delete orders — from the end of the list or from the beginning. The second common mistake is made in this place.

 
 

The list of orders in the terminal is dynamic, while a pass in the cycle for() will be static — if it was said to pass from 0(zero) to total-1 (the number of orders in the list before deleting minus one), then it will be so. If there were 10 orders and we will begin from the beginning of the list (from zero order in the list) , then after passing from 0 to 4, there will remain 5 orders in the list, and they will be placed on the positions from 0 to 5. And a pass in the cycle from 5 to 9 will fire blank, the remaining orders won’t be closed or deleted. The correct way is to delete orders from the end of the list, then there will be no collapse effect. The order of deleting orders from the end of the list is shown in the picture, the size of the list after each deleting/closing will reduce, but none of the orders will be missed.

 
 

After passing to the zero position there will be no orders in the terminal left. The final variant of the script will be as follows:

 
 

Ultmately let’s assign the shortcut key for our script. For example pressing Alt+A will initiate our script to execute on the active chart. To do this it is necessary to click right mouse button on any script (indicator, advisor) and choose «Set shortcut key».

 
 

Make a double click opposite to our script in the column «Control» and choose Alt from the list. Then we’ll assign the key (for example А).

 
 

The script is ready for applying on a demo-account. We launched it by the shortcut keys on the chart of USDCHF (I did it intentionally, as there are no orders on this instrument at the moment) and 14 open orders were closed and 1 pending order was deleted in 25 seconds — a good result as for the speed.

 
 

Before the execution of the script there were the following orders:

 
 
 
 

You can take the script StopTrade here


Go to article «Technical indicators calls».

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?